r使用JavaScript的闪亮自定义卡程序 - 第二个标签不渲染

发布于 2025-02-07 18:25:59 字数 2601 浏览 0 评论 0原文

我正在尝试使用Plain JavaScript在Shiny中构建自己的TABSETS功能。 这部分是我学习JavaScript和闪亮的内部工作以及创建功能的一种练习

。 a href =“ https://stackoverflow.com/a/62623395/3048453”>此答案但适用于shiny等),它在jsfiddle中有效(请参阅 https://jsfiddle.net/qavoeyju/ ),但是闪亮的示例不起作用。

Shiny不认识到第二个选项卡被激活,因此不会触发反应性元素,也不会创建图。

我该如何告诉Shiny也对第二页做出反应,或者如何以Shiny与它们配合使用的方式制作这些自定义标签?

js <- shiny::HTML('
<script>
const tabs = document.querySelectorAll(".tab");
const contents = document.querySelectorAll(".tab-content");

// "activate" the first content
contents[0].classList.add("show-content");
tabs[0].classList.add("active-nav");

tabs.forEach(tab => tab.addEventListener("click", function() {
  // remove active-nav and show-content classes
  tabs.forEach(tab => tab.classList.remove("active-nav"));
  contents.forEach(c => c.classList.remove("show-content"));
  
  // highlight the contents
  document.getElementById(tab.id).classList.add("active-nav");
  document.getElementById(tab.id + "-content").classList.add("show-content");
}));
</script>
')
css <- shiny::HTML(
'
<style>
ul {
  margin: 0;
  padding: 0;
}

.tab {
  display: inline-block;
  border: 1px solid lightgrey;
  padding: 10px;
  cursor: pointer;
}

.active-nav {
  background: lightgrey;
}

.tab-content {
  display: none;
  padding: 10px;
}

.show-content {
  display: block;
  background: lightgray;
}
</style>
'
)

ui <- div(
  tags$ul(
    tags$li(class = "tab", id = "tab1", "data-value" = "Tab 1 CAP", "Tab 1 CAP"),
    tags$li(class = "tab", id = "tab2", "data-value" = "Tab 2 CAP", "Tab 2 CAP")
  ),
  
  div(class="tab-content", id="tab1-content", "data-value" = "Tab 1 CAP",
      p("1 first tab"),
      plotOutput("plot1")
      ),
  
  div(class="tab-content", id="tab2-content", "data-value" = "Tab 2 CAP",
      p("2 second tab"),
      plotOutput("plot2")),
  
  css,
  js
)



server <- function(input, output, session) {
  output$plot1 <- renderPlot({
    plot(1:10, rnorm(10))
  })
  output$plot2 <- renderPlot({
    plot(1:100, rnorm(100))
  })
}

shinyApp(ui, server)

选项卡1-好

选项卡2-糟糕(绘图未渲染)

I am trying to build my own tabsets functionality in shiny using plain javascript.
This is partly an exercise for me to learn javascript and the inner workings of shiny as well as to create functions which give the user greater flexibility when it comes to styling of tabsets.R

So far I have come up with the following (based on this answer but adapted to shiny etc), which works in jsfiddle (see https://jsfiddle.net/qavoeyju/) but the shiny example does not work.

Shiny does not recognize that the second tab is activated and therefore does not trigger the reactive elements and no plot is created.

How can I tell shiny to react to the second page as well, or how can I make these custom tabs in such a way that shiny works with them?

js <- shiny::HTML('
<script>
const tabs = document.querySelectorAll(".tab");
const contents = document.querySelectorAll(".tab-content");

// "activate" the first content
contents[0].classList.add("show-content");
tabs[0].classList.add("active-nav");

tabs.forEach(tab => tab.addEventListener("click", function() {
  // remove active-nav and show-content classes
  tabs.forEach(tab => tab.classList.remove("active-nav"));
  contents.forEach(c => c.classList.remove("show-content"));
  
  // highlight the contents
  document.getElementById(tab.id).classList.add("active-nav");
  document.getElementById(tab.id + "-content").classList.add("show-content");
}));
</script>
')
css <- shiny::HTML(
'
<style>
ul {
  margin: 0;
  padding: 0;
}

.tab {
  display: inline-block;
  border: 1px solid lightgrey;
  padding: 10px;
  cursor: pointer;
}

.active-nav {
  background: lightgrey;
}

.tab-content {
  display: none;
  padding: 10px;
}

.show-content {
  display: block;
  background: lightgray;
}
</style>
'
)

ui <- div(
  tags$ul(
    tags$li(class = "tab", id = "tab1", "data-value" = "Tab 1 CAP", "Tab 1 CAP"),
    tags$li(class = "tab", id = "tab2", "data-value" = "Tab 2 CAP", "Tab 2 CAP")
  ),
  
  div(class="tab-content", id="tab1-content", "data-value" = "Tab 1 CAP",
      p("1 first tab"),
      plotOutput("plot1")
      ),
  
  div(class="tab-content", id="tab2-content", "data-value" = "Tab 2 CAP",
      p("2 second tab"),
      plotOutput("plot2")),
  
  css,
  js
)



server <- function(input, output, session) {
  output$plot1 <- renderPlot({
    plot(1:10, rnorm(10))
  })
  output$plot2 <- renderPlot({
    plot(1:100, rnorm(100))
  })
}

shinyApp(ui, server)

Tab 1 - Good
enter image description here

Tab 2 - Bad (plot not rendered)
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月打碎记忆 2025-02-14 18:25:59

在您的选项卡内容上触发<代码>显示的事件。

library(shiny)
js <- shiny::HTML('
<script>
const tabs = document.querySelectorAll(".tab");
const contents = document.querySelectorAll(".tab-content");

// "activate" the first content
contents[0].classList.add("show-content");
tabs[0].classList.add("active-nav");

tabs.forEach(tab => tab.addEventListener("click", function() {
  // remove active-nav and show-content classes
  tabs.forEach(tab => tab.classList.remove("active-nav"));
  contents.forEach(c => c.classList.remove("show-content"));
  
  // highlight the contents
  document.getElementById(tab.id).classList.add("active-nav");
  let showEl = document.getElementById(tab.id + "-content")
  showEl.classList.add("show-content");
  $(showEl).trigger("shown");
}));
</script>
')
css <- shiny::HTML(
    '
<style>
ul {
  margin: 0;
  padding: 0;
}

.tab {
  display: inline-block;
  border: 1px solid lightgrey;
  padding: 10px;
  cursor: pointer;
}

.active-nav {
  background: lightgrey;
}

.tab-content {
  display: none;
  padding: 10px;
}

.show-content {
  display: block;
  background: lightgray;
}
</style>
'
)

ui <- div(
    tags$ul(
        tags$li(class = "tab", id = "tab1", "data-value" = "Tab 1 CAP", "Tab 1 CAP"),
        tags$li(class = "tab", id = "tab2", "data-value" = "Tab 2 CAP", "Tab 2 CAP")
    ),
    
    div(class="tab-content", id="tab1-content", "data-value" = "Tab 1 CAP",
        p("1 first tab"),
        plotOutput("plot1")
    ),
    
    div(class="tab-content", id="tab2-content", "data-value" = "Tab 2 CAP",
        p("2 second tab"),
        plotOutput("plot2")),
    
    css,
    js
)



server <- function(input, output, session) {
    output$plot1 <- renderPlot({
        plot(1:10, rnorm(10))
    })
    output$plot2 <- renderPlot({
        plot(1:100, rnorm(100))
    })
}

shinyApp(ui, server)

闪亮的绘图绑定到显示的事件。

Trigger the shown event on your tab content.

library(shiny)
js <- shiny::HTML('
<script>
const tabs = document.querySelectorAll(".tab");
const contents = document.querySelectorAll(".tab-content");

// "activate" the first content
contents[0].classList.add("show-content");
tabs[0].classList.add("active-nav");

tabs.forEach(tab => tab.addEventListener("click", function() {
  // remove active-nav and show-content classes
  tabs.forEach(tab => tab.classList.remove("active-nav"));
  contents.forEach(c => c.classList.remove("show-content"));
  
  // highlight the contents
  document.getElementById(tab.id).classList.add("active-nav");
  let showEl = document.getElementById(tab.id + "-content")
  showEl.classList.add("show-content");
  $(showEl).trigger("shown");
}));
</script>
')
css <- shiny::HTML(
    '
<style>
ul {
  margin: 0;
  padding: 0;
}

.tab {
  display: inline-block;
  border: 1px solid lightgrey;
  padding: 10px;
  cursor: pointer;
}

.active-nav {
  background: lightgrey;
}

.tab-content {
  display: none;
  padding: 10px;
}

.show-content {
  display: block;
  background: lightgray;
}
</style>
'
)

ui <- div(
    tags$ul(
        tags$li(class = "tab", id = "tab1", "data-value" = "Tab 1 CAP", "Tab 1 CAP"),
        tags$li(class = "tab", id = "tab2", "data-value" = "Tab 2 CAP", "Tab 2 CAP")
    ),
    
    div(class="tab-content", id="tab1-content", "data-value" = "Tab 1 CAP",
        p("1 first tab"),
        plotOutput("plot1")
    ),
    
    div(class="tab-content", id="tab2-content", "data-value" = "Tab 2 CAP",
        p("2 second tab"),
        plotOutput("plot2")),
    
    css,
    js
)



server <- function(input, output, session) {
    output$plot1 <- renderPlot({
        plot(1:10, rnorm(10))
    })
    output$plot2 <- renderPlot({
        plot(1:100, rnorm(100))
    })
}

shinyApp(ui, server)

Shiny plotting is bound to the shown event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文