Shiny 文档提到selectInput():
selected默认情况下应选择的导航项的值(如果没有提供,则为标题).如果为NULL,则将选择第一个导航.
如果默认情况下我不想从选择列表中选择任何值,该怎么办?
实际上我的选择值默认被选中,应用程序的其余部分自动执行.但我最初不想选择任何价值.我应该为这个selected论点selectInput()提供什么?
实际上,我不希望自动选择任何内容.我使用下面的代码但仍然从列表中选择第一个可用值.我希望默认情况下没有选择,因此用户可以选择任何选项.
output$Choose_App <- renderUI({
selectInput("app",
"Select App:",
choices = as.character(mtrl_name),
selected = NULL ,
multiple = FALSE
)
})
Run Code Online (Sandbox Code Playgroud)
通过文档 我注意到,只有选择时,选择才可以为空multiple=TRUE.它是否正确?
当我改为multiple=TRUE,那么它默认没有被选中,这就是我想要的.但不幸的是,在进行任何选择之前,我也收到以下错误消息:
ERROR: bad 'file' argument
Run Code Online (Sandbox Code Playgroud)
如果我做错了什么,有人知道吗?但如果我选择此文件,则错误消失.

我正在使用以下代码:
# server.R
setwd("/opt/shiny-server/samples/sample-apps/P-Dict_RDS2")
mtrl_name <- try(system("ls | grep -i .rds", intern = TRUE))
shinyServer(function(input, output) {
# First UI input (Service column) filter clientData
output$Choose_Molecule <- renderUI({
selectInput("molecule",
"Select Molecule:",
choices = as.character(mtrl_name),
selected = input$molecule,
multiple = TRUE
)
})
Run Code Online (Sandbox Code Playgroud)
Yih*_*Xie 17
您可以使用选择输入而不是选择输入,使用一些自定义选择选项将初始选择设置为空.Shiny Gallery中提供了一个示例.特别是,请参见示例#6.
# make sure you have shiny >= 0.9.1
selectizeInput(
'e6', '6. Placeholder', choices = state.name,
options = list(
placeholder = 'Please select an option below',
onInitialize = I('function() { this.setValue(""); }')
)
)
Run Code Online (Sandbox Code Playgroud)
顺便说一句,对于错误"ERROR: bad 'file' argument",我认为任何人都可以在没有看到你的应用程序的源代码的情况下帮助你,这可能是一个单独的问题.
roo*_*Joe 11
面临类似的问题.我发现的解决方案基于@MKa的答案.如果您的代码无法处理多个值,则不希望设置multiple = T. 我的建议是:
selectInput("molecule",
"Select Molecule:",
choices = c("",as.character(mtrl_name)),
selected = NULL,
multiple = F
)
Run Code Online (Sandbox Code Playgroud)
并检索所选值:
if(!is.null(input$molecule))
{
if(nchar(input$molecule)>1)
{
#do your thing..
}
}
Run Code Online (Sandbox Code Playgroud)
解决了我的问题.如果您找到了更好的解决方案,请告诉我.
我想你可以通过在你的选择列表中添加一个空字符串来解决它:
selectInput("app",
"Select App:",
choices = c("", as.character(mtrl_name)),
selected = NULL,
multiple = FALSE)
Run Code Online (Sandbox Code Playgroud)
@Yihui Xie 的回答需要selectInput(..., selectize = TRUE). 如果你愿意selectize = FALSE,你仍然可以达到如下类似的效果。
这没有记录:
selected最初选择的值(或多个值,如果multiple = TRUE)。如果未指定,则默认为单选列表的第一个值,多选列表没有值。
但是对于单选列表,如果您可以使用selectize = FALSE, size = 4(任何非NULL大小都可以),那么您可以设置selected = FALSE为强制无默认选择。
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
mainPanel(
uiOutput("Choose_Molecule")
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
# First UI input (Service column) filter clientData
output$Choose_Molecule <- renderUI({
selectInput("molecule",
"Select Molecule:",
choices = rownames(mtcars),
selected = FALSE,
multiple = FALSE
, selectize = FALSE, size = 4 ##needed for `selected = FALSE` to work
)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)
小智 5
一个更简洁的解决方案是使用 selectizeInput,允许多项选择,但随后将最大选择数限制为 1。这意味着默认选择将为 NULL,用户只能选择 1 个选项,并且不必添加空选项字符串到您的选择或前面列出的类似解决方法。
selectizeInput(
"tag",
"Label",
choices = choices,
multiple = TRUE,
options = list(maxItems = 1)
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16865 次 |
| 最近记录: |