2017-04-23 246 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