【发布时间】:2019-07-13 07:05:57
【问题描述】:
我正在设计一个 Shiny 应用程序来分析调查结果,我希望用户能够从 selectInput 下拉菜单(“跳转到选择”)或单击 actionButtons(“上一个” , “下一个”)。这些文章是一个有用的起点:
https://shiny.rstudio.com/reference/shiny/1.0.4/reactiveVal.html
https://shiny.rstudio.com/articles/action-buttons.html
我的问题是selectInput 与actionButtons 的结果发生冲突,因为两者都控制同一个对象。我如何让他们一起工作?我不想将isolate() 与selectInput 一起使用并让用户单击其他按钮;我希望选择在他们选择后立即更改。谢谢!
library(shiny)
ui <- fluidPage(
mainPanel(
actionButton("previous_q",
"Previous"),
actionButton("next_q",
"Next"),
selectInput("jump",
"Jump to Question",
choices = 1:10),
textOutput("selected")
)
)
server <- function(input, output) {
# Select based on "Previous" and "Next" buttons -------
selected <- reactiveVal(1)
observeEvent(input$previous_q, {
newSelection <- selected() - 1
selected(newSelection)
})
observeEvent(input$next_q, {
newSelection <- selected() + 1
selected(newSelection)
})
# Jump to selection (COMMENTED OUT SO APP DOESN'T CRASH) ----------------
#observeEvent(input$jump, {
#newSelection <- input$jump
#selected(newSelection)
#})
# Display selected
output$selected <- renderText({
paste(selected())
})
}
shinyApp(ui = ui, server = server)
【问题讨论】: