selectInput 无法在 Shiny 中填充重复值(使用 uiOutput 和 renderUI)
selectInput can't populate duplicate values (using uiOutput and renderUI) in Shiny
我有一个基于数据框的 Shiny 应用,如下所示:
ID Date result
1 1/1/2010 100
1 12/12/2010 200
2 1/1/2011 300
2 1/1/2011 400
注意双 ID...对于 ID 2,两个日期是相同的。
当 运行 时,应用程序如下所示:
但是当应用程序被迫考虑具有多个相同日期的 ID #2 时,它会感到困惑:
下拉菜单的第一个选项是空白的,但第二个选项是正确的。
我如何更正此问题,以便下拉列表中填充任意数量的相同日期?
(我想得越多,感觉就像是一个错误,而不是简单地从对象中提取功能。不难想到许多重复值会引起兴趣的情况。)
感谢您的关注。
app.R
library('shiny')
DF <- data.frame(ID=c(1,1,2,2), Date=c('1/1/2010', '12/12/2010', '1/1/2011', '1/1/2011'), result=c(100, 200, 300, 400))
DF$Date <- as.character(DF$Date)
server <- function(input, output, session) {
get_id <- reactive({
id <- DF[which(DF$ID == input$ID), ]
return(id)})
output$result <- renderText({
ans <- get_id()
print(ans)
paste("Result: ", ans$result)})
output$dates<-renderUI({
print(get_id()$Date)
selectInput('dates', 'Select date:', choices=get_id()$Date, selected=get_id()$Date[1])})
}
ui <- fluidPage(
fluidRow(column(2,
numericInput(inputId="ID", label="Pick an ID: ", value=1))),
column(2,
uiOutput("dates")),
column(3, mainPanel(textOutput("result")))
)
shinyApp(ui = ui, server = server)
我不确定为什么它没有显示,但是有一个解决方法可以解决您的情况。在 selectInput()
函数中使用参数 selectize = FALSE
。这应该会为您提供所需的功能。
selectInput('dates', 'Select date:', choices=get_id()$Date,selected=get_id()$Date, selectize = FALSE)})
看起来 selectize 最近添加了允许重复值的支持(参见 github 问题 https://github.com/brianreavis/selectize.js/issues/129)
但 shiny 似乎还没有选择那个版本(github 问题 https://github.com/rstudio/shiny/issues/518)
我有一个基于数据框的 Shiny 应用,如下所示:
ID Date result
1 1/1/2010 100
1 12/12/2010 200
2 1/1/2011 300
2 1/1/2011 400
注意双 ID...对于 ID 2,两个日期是相同的。
当 运行 时,应用程序如下所示:
但是当应用程序被迫考虑具有多个相同日期的 ID #2 时,它会感到困惑:
下拉菜单的第一个选项是空白的,但第二个选项是正确的。
我如何更正此问题,以便下拉列表中填充任意数量的相同日期?
(我想得越多,感觉就像是一个错误,而不是简单地从对象中提取功能。不难想到许多重复值会引起兴趣的情况。)
感谢您的关注。
app.R
library('shiny')
DF <- data.frame(ID=c(1,1,2,2), Date=c('1/1/2010', '12/12/2010', '1/1/2011', '1/1/2011'), result=c(100, 200, 300, 400))
DF$Date <- as.character(DF$Date)
server <- function(input, output, session) {
get_id <- reactive({
id <- DF[which(DF$ID == input$ID), ]
return(id)})
output$result <- renderText({
ans <- get_id()
print(ans)
paste("Result: ", ans$result)})
output$dates<-renderUI({
print(get_id()$Date)
selectInput('dates', 'Select date:', choices=get_id()$Date, selected=get_id()$Date[1])})
}
ui <- fluidPage(
fluidRow(column(2,
numericInput(inputId="ID", label="Pick an ID: ", value=1))),
column(2,
uiOutput("dates")),
column(3, mainPanel(textOutput("result")))
)
shinyApp(ui = ui, server = server)
我不确定为什么它没有显示,但是有一个解决方法可以解决您的情况。在 selectInput()
函数中使用参数 selectize = FALSE
。这应该会为您提供所需的功能。
selectInput('dates', 'Select date:', choices=get_id()$Date,selected=get_id()$Date, selectize = FALSE)})
看起来 selectize 最近添加了允许重复值的支持(参见 github 问题 https://github.com/brianreavis/selectize.js/issues/129)
但 shiny 似乎还没有选择那个版本(github 问题 https://github.com/rstudio/shiny/issues/518)