【问题标题】:R, Shiny: next/previous button for selectInputR,闪亮:选择输入的下一个/上一个按钮
【发布时间】:2017-04-23 17:04:02
【问题描述】:

我对闪亮还是很陌生,但我没有找到解决方案来做我想做的事。

我的目标是为一个动态创建的选择字段 (selectInput) 提供上一个/下一个按钮(我需要这个按钮用于未来的开发,因为数据集的功能会发生变化)。

我的灵感来自这篇帖子Add back/next button to date range input in shiny

但是在我的情况下我无法做到这一点。

这是我尝试的一个简单示例(目的是将其应用于更复杂的数据集)。

library(shiny)

vector_choices <- seq(5, 50, by = 5)

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            uiOutput("selector"),
            tags$div(class="row",
                     tags$div(uiOutput("prevBin")),
                     tags$div(uiOutput("nextBin")))

      ),

      mainPanel(
         textOutput("text"),
         plotOutput("distPlot")
      )
   )
)

server <- function(input, output, session) {
    output$selector <- renderUI({
        selectInput("bins",
                    "Bins",
                    choices = as.list(vector_choices),
                    selected = 25)
    })

    output$prevBin <- renderUI({
        actionButton("prevBin", 
                     label = "Previous")
    })
    output$nextBin <- renderUI({
        actionButton("nextBin", 
                     label = "Next")
    })

    observeEvent(input$prevBin, {
        current <- which(vector_choices == input$bins)
        if(current > 1){
            updateSelectInput(session, "bins",
                              choices = as.list(vector_choices),
                              selected = vector_choices[current - 1])
        }
    })
    observeEvent(input$nextBin, {
        current <- which(vector_choices == input$bins)
        if(current < length(vector_choices)){
            updateSelectInput(session, "bins",
                              choices = as.list(vector_choices),
                              selected = vector_choices[current + 1])
        }
    })

    output$distPlot <- renderPlot({
      x <- rnorm(100)
      bins <- seq(min(x), max(x), length.out = as.numeric(input$bins))
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })
}

shinyApp(ui = ui, server = server)

如果有人遇到这个问题或者是一位闪亮的专家并且可以指出我正确的方向,我们将不胜感激。

谢谢

编辑:我根据 BigDataScientist 的回答更正了代码,并为列表的第一次和最后一次出现添加了条件。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    你很亲密。

    您想要更新 selectInput() 而不是创建 selectInput()renderUI() ;)

    所以将你的updateSelectInput() "selector" 替换为"bins"

    updateSelectInput(session, "bins", choices = vector_choices,
                          selected = vector_choices[current - 1])
    

    【讨论】:

    • 我认为您甚至可以省略选择,因为它们不需要更新。
    猜你喜欢
    • 1970-01-01
    • 2019-07-15
    • 2019-03-13
    • 2016-04-03
    • 2021-07-16
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    • 2016-03-16
    相关资源
    最近更新 更多