【问题标题】:Apparent incorrect use of renderUI in R Shiny for interactive dashboard在 R Shiny 中明显不正确地使用 renderUI 进行交互式仪表板
【发布时间】:2018-04-14 13:31:37
【问题描述】:

我正在构建一个 Rshiny 仪表板,并正在努力集成 Shiny 的一些更具交互性的功能,并且目前正在使用 renderUI 功能,它(我相信应该)根据另一个输入的值创建额外的小部件/输入参数范围。我遇到了一个简单的错误,但在调试时遇到了困难。下面是一个带有相关代码的演示:

choices = c('All', 'None')
names(choices) = choices
ui <- fluidPage(theme = shinytheme('united'),

     # create permanent input for shot chart type (should be 5 options)
     selectInput(inputId = 'choice.input', label = 'Select a choice', multiple = FALSE,
                 choices = choices, selected = 'All'),

     uiOutput('secondinput')

)

server <- shinyServer(function(input, output) {

  if(input$choice.input == 'All') {

    my.second.input <- c('a', 'b', 'c', 'd', 'e')
    names(my.second.input) <- my.second.input

    # player parameter for player whose shot chart will be shown
    output$secondinput <- renderUI({
      selectInput(inputId = 'another.input', label = 'Check this input', multiple = FALSE,
                  choices = my.second.input, selected = 'a')
    })
  }
})

shinyApp(ui, server)

我不确定这里出了什么问题 - 我认为我在服务器函数中使用 renderUI() 并且名称匹配 (output$secondinput, uiOutput('secondinput')) 是正确的,但这会引发我的错误...

请注意,我的完整代码有多个选项用于choice.input,并且我希望在服务器中为每个 (4-5)choice.input() 值设置一个 if() 案例。感谢您对此处出现问题的任何帮助,谢谢!

编辑 - 澄清一下,带有标签“选择一个选项”的选择输入choice.input 应该始终显示。当此输入设置为“全部”时,我希望显示一个附加输入,即第二个输入。如果choice.input 未设置为“全部”,那么我不希望显示第二个输入。希望这会有所帮助。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    这是您的代码的一个有效版本。我不确定这是否正是你想要的,这有点难以判断,但希望你能从那里得到它。

    choices = c('All', 'None')
    names(choices) = choices
    ui <- fluidPage(
      # create permanent input for shot chart type (should be 5 options)
      selectInput(inputId = 'choice.input', label = 'Select a choice', multiple = FALSE,
                  choices = choices, selected = 'All'),
      uiOutput('secondinput')
    )
    
    server <- shinyServer(function(input, output) {
      # player parameter for player whose shot chart will be shown
      output$secondinput <- renderUI({
        if(input$choice.input == 'All') {
    
          my.second.input <- c('a', 'b', 'c', 'd', 'e')
          names(my.second.input) <- my.second.input
          selectInput(inputId = 'another.input', label = 'Check this input', multiple = FALSE,
                      choices = my.second.input, selected = 'a')    
    
        } else{
          return(NULL)
        }
      })
    })
    
    shinyApp(ui, server)
    

    发生的错误告诉您,您正在尝试在没有响应式上下文的情况下访问响应式值。您试图访问渲染函数或观察函数之外的输入值(反应式),它们是反应式上下文。

    如果您不明白这意味着什么,我强烈建议您阅读我编写的闪亮教程reactivity 101 的本节。

    这里的第二个问题是您试图“嵌套”一个渲染函数,该函数可以工作,但考虑它的方式是错误的,并表明您可能没有真正掌握这个概念反应性和渲染功能充分。请注意,我将渲染功能移到了外部,这通常是使用闪亮编程的正确方法。如果你有一点时间,我建议观看 Joe Cheng(shiny 的作者)的视频“有效的反应式编程 I 和 II”,来自 2016 年闪亮大会https://www.rstudio.com/resources/webinars/shiny-developer-conference/

    【讨论】:

      猜你喜欢
      • 2012-12-07
      • 2018-08-29
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-19
      • 2016-07-12
      相关资源
      最近更新 更多