Dan Reznik
Dan Reznik

Reputation: 41

flexdashboard and updateSelectInput -- won't work for the life of me

Trying to make a selectInput's range of choices depend on the value of a first selectInput.

Flexboard header looks like:

---
title: "blabla"
output:
   flexdashboard::flex_dashboard:
   orientation: rows
   social: menu
   source_code: embed
   theme: cerulean
runtime: shiny
---

The code below, which compiles but doesn't work, is within the .sidebar chunk. I am attempting to use input$book to interactively change the range of the "chapter" selectInput. Unfortunately, updateSelectInput() seems to do nothing.

# Inputs {.sidebar data-width=150}

```{r}
selectInput("book", label = "libro", choices = c("dq1605", "dq1615"), selected="dq1605")
selectInput("chapter", label = "capítulo",choices = 0:54, selected=0)

observeEvent(input$book, {
   y <- input$book
   if (is.null(y)) y <- "dq1605"
   chs <- if(y=="dq1605") 0:54 else 0:74
   updateSelectInput(session,"chapter",choices = chs)
})
```

I have invested several hrs trying to make this work (I have used the simpler observeEvent({}) as well), but to no avail. Any suggestions?


> sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] compiler_3.4.3      backports_1.1.2     magrittr_1.5        rsconnect_0.8.5    
 [5] rprojroot_1.3-2     htmltools_0.3.6     tools_3.4.3         flexdashboard_0.5.1
 [9] yaml_2.1.16         Rcpp_0.12.15        stringi_1.1.6       rmarkdown_1.9      
[13] knitr_1.20          jsonlite_1.5        stringr_1.2.0       digest_0.6.15      
[17] evaluate_0.10.1

Upvotes: 2

Views: 1514

Answers (1)

Lodewic Van Twillert
Lodewic Van Twillert

Reputation: 782

It works for me actually. All I changed is the 'selected = max(chs)' to make it easier to see if it worked.

Can't see any of your packages being older than mine either.

---
title: "blabla"
output:
   flexdashboard::flex_dashboard:
   orientation: rows
   social: menu
   source_code: embed
   theme: cerulean
runtime: shiny
---

# Inputs {.sidebar data-width=150}

```{r}
selectInput("book", label = "libro", choices = c("dq1605", "dq1615"), selected="dq1605")
selectInput("chapter", label = "capítulo",choices = 0:54, selected=0)

observeEvent(input$book, {
   y <- input$book
   if (is.null(y)) y <- "dq1605"
   chs <- if(y=="dq1605") 0:54 else 0:74
   updateSelectInput(session, "chapter", choices = chs, selected = max(chs))
})
```

Upvotes: 3

Related Questions