在selectInput()中强制没有默认选择

时间:2014-06-12 03:26:39

标签: r shiny

The Shiny documentation提及selectInput()

  

selected默认情况下应选择的导航项的值(或者,如果没有提供,则为标题)。如果为NULL,则将选择第一个导航。

如果默认情况下我不想从选择列表中选择任何值,该怎么办?

实际上我的选择值默认被选中,应用程序的其余部分会自动执行。但我最初不想选择任何价值。我应该为selected selectInput()中的output$Choose_App <- renderUI({ selectInput("app", "Select App:", choices = as.character(mtrl_name), selected = NULL , multiple = FALSE ) }) 参数提供什么呢?


确实,我不希望自动选择任何内容。我使用下面的代码但仍然从列表中选择第一个可用值。我希望默认情况下没有选择,因此用户可以选择任何选项。

multiple=TRUE

通过documentation我注意到只有选择multiple=TRUE时,选择才能为空。它是否正确?

当我更改为ERROR: bad 'file' argument 时,默认情况下它没有被选中,这就是我想要的。但不幸的是,在做出任何选择之前,我也会收到以下错误消息:

# 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
           )
        })

如果我做错了,有人知道吗?但如果我选择此文件,则错误消失。

enter image description here

我正在使用以下代码:

{{1}}

6 个答案:

答案 0 :(得分:13)

您可以使用选择输入而不是选择输入,使用一些自定义选择选项将初始选择设置为空。 Shiny Gallery中提供了An example。特别是,请参见示例#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(""); }')
  )
)

BTW,对于错误"ERROR: bad 'file' argument",我认为如果没有看到您应用的源代码,任何人都无法帮助您,这可能是一个单独的问题。

答案 1 :(得分:10)

面临类似的问题。我发现的解决方案是基于@MKa的答案。如果您的代码无法处理多个值,则不希望设置multiple = T.我的建议是:

selectInput("molecule",
            "Select Molecule:",
            choices = c("",as.character(mtrl_name)),
            selected = NULL,
            multiple = F
           ) 

并检索所选值:

if(!is.null(input$molecule))
{
  if(nchar(input$molecule)>1)
  {
    #do your thing..
  }
}

解决了我的问题。如果您找到了更好的解决方案,请告诉我。

答案 2 :(得分:7)

我认为你可以通过在你的选择列表中添加一个空字符串来解决它:

selectInput("app", 
"Select App:", 
choices = c("", as.character(mtrl_name)), 
selected = NULL, 
multiple = FALSE)

答案 3 :(得分:1)

6年后的答案,因为我现在遇到了同样的错误。我们可以写 selected = character(0) 强制不选择任何用户输入。

答案 4 :(得分:0)

@Yihui Xie的answer要求selectInput(..., selectize = TRUE)。如果您想要selectize = FALSE,仍然可以达到以下类似效果。

这不是documented

  

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)

enter image description here

答案 5 :(得分:0)

一种解决方法是使用updateSelectInput。这里的重点是您将selected =参数设置为length(your_vector_with_choices)

如果您想在一开始就更新reactiveValues(在这种情况下仅在程序启动时进行观察),则可以创建selectInput

library(shiny)

shinyApp(
  ui = fluidPage(
    selectInput("state", "Choose a state:", NULL),
    textOutput("result")
  ),
  
  server = function(input, output, session) {
    values <- reactiveValues(start = NULL)
    
    observe({
      choiceList <- list(`East Coast` = list("NY", "NJ", "CT"),
                         `West Coast` = list("WA", "OR", "CA"),
                         `Midwest` = list("MN", "WI", "IA"))
      updateSelectInput(session, "state", choices = choiceList, selected = choiceList[length(choiceList)])
    })
    
    output$result <- renderText({
      paste("You chose", input$state)
    })
  }
)
相关问题