2个conditionalPanel中的相同inputid - Shiny

时间:2017-03-31 21:43:12

标签: r shiny

我是一个闪亮的新手,并试图让一些简单的工作,但无法:(

以下是 ui.R

的一部分
sidebarLayout(
  sidebarPanel(

    radioButtons("market",
                 "Choose a Region to build the Sales file:",
                 c("North America & ANZ" = "NA", "Europe" = "EU"), inline = TRUE),

    conditionalPanel(
      condition = "input.market == 'NA'",
      radioButtons("Locale",
                   "Choose a locale to see the sales Calendar:",
                   c("US and Canada" = "US_CA", "ANZ" = "ANZ"), inline = TRUE),

      numericInput("sale_num", "Choose a Sale Number from the Table below",1,width = '100px' )

    ),

    conditionalPanel(
      condition = "input.market == 'EU'",
      radioButtons("Locale",
                   "Choose a locale to see the sales Calendar:",
                   c("UK" = "UK", "FR and RoE" = "FR_ROE","DE,AT & CH" = "DACH"), inline = TRUE),
      numericInput("sale_num", "Choose a Sale Number from the Table below",1,width = '100px' )),


    dataTableOutput("sales"))

  ),

这是我的 server.R

server <- shinyServer(function(input, output) {
       output$sales <- renderDataTable({
       saleTable(input$Locale)
       },options = list(autoWidth = FALSE,searching = FALSE,pageLength=10))
 })

当触发market单选按钮的更改时,Locale单选按钮不会更新,因此sales输出表仍然具有过时值,并且不会被任何更改反映出来Locale值。

我知道我应该使用像UpdateRadiobuttons这样的东西,但我不知道怎么做。 :(

saleTable只是我的Rscript中生成数据表的函数。

请帮忙!

提前致谢!

2 个答案:

答案 0 :(得分:0)

请发一个最小的例子,即你的功能saleTable。不要在你的应用程序中使用相同的输入ID两次,它的样式很糟糕,在大多数情况下不起作用。这里有两个解决方案:第一个是糟糕的风格,第二个是更好的风格。

1)将第二个Locale重命名为Locale2并将其放入output$sales

output$sales <- renderDataTable({    
    if(input$market == 'NA') data <- input$Locale
    else if(input$market=="EU") data <- input$Locale2
    saleTable(data)
    }, options = list(autoWidth = FALSE,searching = FALSE,pageLength=10))
)

2)将第二个输出创建为UIOutput并使其依赖于第一个输出:

ui <- shinyUI(
  sidebarLayout(
    sidebarPanel(      
          radioButtons("market",
                   "Choose a Region to build the Sales file:",
                   c("North America & ANZ" = "NA", "Europe" = "EU"), inline = TRUE),      
      uiOutput("Locale")),      
      mainPanel(dataTableOutput("sales"))))

server <- function(input, output, session) {
  output$Locale <- renderUI({
    if(input$market == "NA") myChoices <-  c("US and Canada" = "US_CA", "ANZ" = "ANZ")
    else myChoices <- c("UK" = "UK", "FR and RoE" = "FR_ROE","DE,AT & CH" = "DACH")

  radioButtons("Locale","Choose a locale to see the sales Calendar:",
                choices <- myChoices,
                 inline = TRUE)
  })
  output$sales <- renderDataTable({
    saleTable(input$Locale)
  },options = list(autoWidth = FALSE,searching = FALSE,pageLength=10))
}

shinyApp(ui = ui, server = server)

答案 1 :(得分:0)

基于对使用updateRadioButtons表达的兴趣,我将一个带有两个单选按钮和一个表输出的简单示例组合在一起。

第一个单选按钮输入不会改变。第二个单选按钮输入取决于第一个输入的值。显示的表是mtcars数据帧,按两个单选按钮组的值进行过滤。

使用observeEvent可确保每次carb无线电输入更改时cyl无线电输入的值都会更新。这也将在应用程序首次启动时触发,这也是我们没有看到"will be replaced"无线电输入的默认,虚拟,选择carb的原因。

确保将session包含为Shiny服务器函数参数之一。所有Shiny的update*Input函数都要求您将session对象传递给它们。

我希望这证明有用。

library(shiny)

shinyApp(
  ui = fluidPage(
    fluidRow(
      column(
        width = 4,
        radioButtons(
          inputId = "cyl",
          label =  "Choose number of cylinders:",
          choices = unique(mtcars$cyl),
          selected = unique(mtcars$cyl)[1]
        ),
        radioButtons(
          inputId = "carb",
          label = "Choose number of carburetors:",
          choices = "will be replaced"
        )
      ),
      column(
        width = 8,
        tableOutput(
          outputId = "mtcars"
        )
      )
    )
  ),
  server = function(input, output, session) {
    observeEvent(input$cyl, {
      newChoices <- sort(unique(mtcars[mtcars$cyl == input$cyl, ]$carb))

      updateRadioButtons(
        session = session,
        inputId = "carb",
        choices = newChoices,
        selected = newChoices[1]
      )
    })

    output$mtcars <- renderTable({
      req(input$cyl, input$carb)

      mtcars[mtcars$cyl == input$cyl & mtcars$carb == input$carb, ]
    })
  }
)