2017-04-23 182 views
2

下一个/上一个按钮,我很新的闪亮,我没有找到一个解决办法做我想要的东西。R,闪亮的:用于selectInput

我的目标是有一个选择栏(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的答案代码,我加了一个条件列表中的第一个和最后出现次数。

回答

1

你接近。

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

在这样同时替换你的updateSelectInput()"selector""bins"

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

完美这工作就像我所料,谢谢! – Alexis