我正在设计一个 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)
最佳答案
问题是input$jump
是一个字符串,不是一个数字。做:
observeEvent(input$jump, {
newSelection <- as.integer(input$jump)
selected(newSelection)
})
关于r - 通过 selectInput 或 actionButton 更改选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54771990/