Updating choices select input in Shiny document

I'm working in a shiny document (Rmd) and having trouble making the select choices dynamic. I've made a very simple example and get an error that

Warning: Error in if: argument is of length zero
46: <observer> [<text>#15]
                  3: <Anonymous>
                  1: rmarkdown::run

Here's the stripped down code - note this is in an Rmd document. The goal is that if tract is selected, then you can choose multiple number of bedrooms but if block group is used, you can only choose "All" bedrooms.

---
title: "Untitled"
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


## Inputs and Outputs


```{r br, echo=FALSE}
br_choice_init <- c("All", as.character(0:4), "5 or more")
inputPanel(
  radioButtons("sumlev", label="Geography level:",
               choices=c("Tract"="tract", "Block group"="bg")),
  selectInput("n_br", label = "Number of bedrooms:",
              choices = br_choice_init, 
              selected = br_choice_init[1])
  
  
)

observe({
  x <- input$sumlev
  
  if(is.null(x)|x=="tract"){
    newchoices <- br_choice_init
    newlabel <- "Number of bedrooms:"
  }else{
    newchoices <- "All"
    newlabel <- "Number of bedrooms (only All):"
  }
  
  # Can also set the label and select items
  updateSelectInput(session, "n_br",
                    label = newlabel,
                    choices = newchoices,
                    selected = newchoices[1]
  )
})


```


I think using

 if(is.null(x) || x=="tract")

with the double || fixes your problem

Thanks - that fixed it! I changed the order of the conditions but forgot that still both are checked with a single pipe.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.