【问题标题】:Change selection by selectInput OR actionButton通过 selectInput 或 actionButton 更改选择
【发布时间】: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

我的问题是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)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    问题在于input$jump 是一个字符串,而不是一个数字。做:

      observeEvent(input$jump, {
    
        newSelection <- as.integer(input$jump)
        selected(newSelection)
    
      })
    

    【讨论】:

    • 谢谢,解决了!但我不明白 input$jump 是如何变成字符串的,因为 selectInput 下的选项是整数。是在什么阶段被胁迫的?
    • @JeremyB selectInput 总是返回一个字符串。 selectInput 函数将选择强制转换为字符。
    • 太好了,我将阅读文档以获取更多信息。再次感谢!
    【解决方案2】:

    欢迎来到 SO!

    @StéphaneLaurent 稍微快一点 - 我还是会发布我的解决方案:正如他已经提到的那样,您需要 as.integer(input$jump)

    此外,您不需要reactiveVal()“已选择”。这也需要注意限制您的选择:

    library(shiny)
    
    selectInputChoices = 1:10
    
    ui <- fluidPage(mainPanel(
      actionButton("previous_q",
                   "Previous"),
      actionButton("next_q",
                   "Next"),
      selectInput(
        "jump",
        "Jump to Question",
        choices = selectInputChoices,
        selected = 1
      ),
      textOutput("selected")
    ))
    
    server <- function(input, output, session) {
      # Select based on "Previous" and "Next" buttons -------
      observeEvent(input$previous_q, {
        req(input$jump)
        if (as.integer(input$jump) > min(selectInputChoices)) {
          newSelection <- as.integer(input$jump) - 1
          updateSelectInput(session, inputId = "jump", selected = newSelection)
        }
      })
    
      observeEvent(input$next_q, {
        req(input$jump)
        if (as.integer(input$jump) < max(selectInputChoices)) {
          newSelection <- as.integer(input$jump) + 1
          updateSelectInput(session, inputId = "jump", selected = newSelection)
        }
      })
    
      # Display selected
      output$selected <- renderText({
        req(input$jump)
        paste(input$jump)
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 感谢@ismirsehregal 的全面回答,这将有助于我们向前发展!
    猜你喜欢
    • 1970-01-01
    • 2021-10-05
    • 2021-04-20
    • 2010-11-29
    • 2021-06-13
    • 1970-01-01
    • 2021-06-04
    • 2021-09-09
    • 1970-01-01
    相关资源
    最近更新 更多