【发布时间】: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 的回答更正了代码,并为列表的第一次和最后一次出现添加了条件。
【问题讨论】: