gpt4 book ai didi

r - 通过 selectInput 或 actionButton 更改选择

转载 作者:行者123 更新时间:2023-12-04 15:48:44 24 4
gpt4 key购买 nike

我正在设计一个 Shiny 应用程序来分析调查结果,我希望用户能够从 selectInput 下拉菜单(“跳转到选择”)或通过单击 actionButtons(“上一个”、“下一个”)。这些文章是一个有用的起点:

https://shiny.rstudio.com/reference/shiny/1.0.4/reactiveVal.html

https://shiny.rstudio.com/articles/action-buttons.html

我的问题是 selectInputactionButtons 的结果冲突,因为两者控制的是同一个对象。我如何让他们一起工作?我不想将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com