我正在设计一个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)
答案 0 :(得分:1)
问题在于input$jump
是一个字符串,而不是数字。做:
observeEvent(input$jump, {
newSelection <- as.integer(input$jump)
selected(newSelection)
})
答案 1 :(得分:0)
欢迎您!
@StéphaneLaurent快一点-无论如何我都会发布解决方案:正如他已经提到的那样,您将需要.h
此外,您不需要as.integer(input$jump)
“已选择”。这也要注意限制您的选择:
reactiveVal()