我正在构建一个 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,标签为“选择一个选择”,应该始终显示。当此输入设置为“全部”时,我希望显示附加输入 secondinput。如果choice.input 未设置为'All',那么我不希望显示第二个输入。希望这可以帮助。
这是您的代码的一个有效版本。我不确定这是否正是你想要的,这有点难说,但希望你能从那里得到它。
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)
发生的错误告诉您,您正在尝试在没有反应上下文的情况下访问反应值。您试图访问渲染函数或观察函数之外的输入值(它是反应性的),它们是反应性上下文。
如果你不明白这意味着什么,我强烈建议你阅读我写的一个闪亮的教程,反应性 101 的这一部分。
这里的第二个问题是你试图“嵌套”一个渲染函数,它可以工作,但这是错误的思考方式,并表明你可能没有真正完全掌握反应性和渲染函数的概念。请注意,我将渲染函数移到了外部,这通常是在闪亮中编程的正确方法。如果你有一点时间,我建议观看 Joe Cheng(闪亮的作者)2016 年闪亮大会上的视频“有效的反应式编程 I 和 II” https://www.rstudio.com/resources/webinars/shiny-developer -会议/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句