【问题标题】:tab dependent input for shiny dashboard闪亮仪表板的选项卡相关输入
【发布时间】:2015-06-01 07:42:09
【问题描述】:

我正面临闪亮仪表板的问题。我正在尝试创建一个简单的仪表板,左侧有两个 tabItems。每个 tabItem 都有其特定的控件集和绘图。但是我可能在服务器端遗漏了一些东西来将输入链接到选项卡,因为第二个选项卡的控件表现得很奇怪。任何帮助将非常感激。这是我的代码

library(shiny)
library(shinydashboard)
library(data.table)
library(ggplot2)
data = data.table(group = rep(c(1, 3, 6), each = 10), x = rep(1:10, times = 3), value = rnorm(30))

sidebar <- dashboardSidebar(
  sidebarMenu(id = 'sidebarMenu',
    menuItem("tab 1", tabName = "tab1", icon = icon("dashboard")),
    menuItem("tab 2", icon = icon("th"), tabName = "tab2")
  )
)

body <- dashboardBody(
          tabItems(
            tabItem(tabName = "tab1",
              fluidRow(
                box(title = "Controls",
                    checkboxGroupInput('group', 'group:', c(1, 3, 6), selected = 6, inline = TRUE), width = 4),
                box(plotOutput("plot1"), width = 8)
             )
           ),

          tabItem(tabName = "tab2",
             fluidRow(
               box(title = "Controls",
                   checkboxGroupInput('group', 'group:', c(1, 3, 6), selected = 6, inline = TRUE), width = 4),
               box(plotOutput("plot2"), width = 8)
             )
          )
       )
    )

 # Put them together into a dashboardPage
 ui <- dashboardPage(
 dashboardHeader(title = "test tabbed inputs"),
 sidebar,
 body,
 skin = 'green'
 )

server <- function(input, output) {
   output$plot1 <- renderPlot({
   plotData <- data[group %in% input$group]
   p <- ggplot(plotData, aes(x = x, y = value, colour = factor(group))) + geom_line() + geom_point()
   print(p)
   })
   output$plot2 <- renderPlot({
   plotData <- data[group %in% input$group]
   p <- ggplot(plotData, aes(x = x, y = value, colour = factor(group))) + geom_line() + geom_point()
   print(p)
   })
}

shinyApp(ui, server)

当我在第一个选项卡中更改输入时,它也会在第二个选项卡中更改,然后当我尝试将其更改回来时,通常什么都没有发生,或者它只是表现得很奇怪。我想我需要以某种方式指定将输入绑定到 tabItems 但找不到这样做的好例子。任何帮助将不胜感激。

谢谢, 阿信

【问题讨论】:

  • 您是否尝试过仅调用 checkboxGroupInput 调用不同的名称,然后将 server.R 中的代码复制两次,这样就不会混淆您正在操作的变量?
  • 是的,这就是我最终解决问题的方法。但是在我的实际问题中,每个选项卡都有多个输入,因此每次重命名似乎都会有点麻烦。希望看看是否还有其他我错过的解决方案。
  • 我不是一个闪亮的忍者,但我不相信有办法做到这一点。如果您想要多个不同的输入,它们必须是多个不同的变量。也许其他人会出现并知道方法。
  • 谢谢科林,看起来就是这样。

标签: r shiny


【解决方案1】:

要处理动态数量的选项卡或其他小部件,请在 server.RrenderUI 中创建它们。使用list 存储选项卡,使用do.call 函数应用tabItems 函数。侧边栏也是如此。

我认为我下面的代码会产生您的期望。

library(shiny)
library(shinydashboard)
library(data.table)
library(ggplot2)
data = data.table(group = rep(c(1, 3, 6), each = 10), x = rep(1:10, times = 3), value = rnorm(30))

sidebar <- dashboardSidebar(
  uiOutput("Sidebar")
)

body <- dashboardBody(
  uiOutput("TABUI")
)

# Put them together into a dashboardPage
ui <- dashboardPage(
  dashboardHeader(title = "test tabbed inputs"),
  sidebar,
  body,
  skin = 'green'
)

server <- function(input, output) {

  ntabs <- 3
  tabnames <- paste0("tab", 1:ntabs) # "tab1", "tab2", ...
  checkboxnames <- paste0(tabnames, 'group') # "tab1group", "tab2group", ...
  plotnames <- paste0("plot", 1:ntabs) # "plot1", "plot2", ...

  output$Sidebar <- renderUI({
    Menus <- vector("list", ntabs)
    for(i in 1:ntabs){
      Menus[[i]] <-   menuItem(tabnames[i], tabName = tabnames[i], icon = icon("dashboard"), selected = i==1)
    }
    do.call(function(...) sidebarMenu(id = 'sidebarMenu', ...), Menus)
  })

  output$TABUI <- renderUI({
    Tabs <- vector("list", ntabs)
    for(i in 1:ntabs){
      Tabs[[i]] <- tabItem(tabName = tabnames[i],
                     fluidRow(
                       box(title = "Controls", 
                           checkboxGroupInput(checkboxnames[i], 'group:', c(1, 3, 6), selected = 6, inline = TRUE), 
                           width = 4),
                       box(plotOutput(paste0("plot",i)), width = 8)
                     )
      )
    }
    do.call(tabItems, Tabs)
  })

  RV <- reactiveValues()
  observe({
    selection <- input[[paste0(input$sidebarMenu, 'group')]]
    RV$plotData <- data[group %in% selection]
  })

  for(i in 1:ntabs){
    output[[plotnames[i]]] <- renderPlot({
      plotData <-  RV$plotData 
      p <- ggplot(plotData, aes(x = x, y = value, colour = factor(group))) + 
        geom_line() + geom_point()  
      print(p)
    })
  }

}

shinyApp(ui, server)

请注意,我将“绘图数据”放在反应列表中。否则,如果我这样做:

output[[plotnames[i]]] <- renderPlot({
   selection <- input[[paste0(input$sidebarMenu, 'group')]]
   plotData <- data[group %in% selection]
   ...

每次您返回标签时,情节都会发生反应(尝试了解我的意思)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 2016-11-24
    • 2020-07-24
    • 2016-01-06
    相关资源
    最近更新 更多