0

I am trying to use a basin, and then update the possible choices of sub-basins within that basin. However, my code is not working. I cannot make it work neither with observe, nor with reactive, nor with observeEvent nor without all of them.

My ui side is as:

selectInput(inputId = 'countyType_id',
            label = '1. Select a basin',
            choices = all_basins
            ),

selectizeInput(inputId = 'subbasins_id',
               label = '2. Select subbasins', 
               choices = subbasins, 
               selected = head(subbasins, 1),
               multiple = TRUE)

and the server side looks like :

observe({
    #
    # from 
    # https://shiny.rstudio.com/reference/shiny/latest/updateSelectInput.html
    #
    subbasins <- sort(unique(curr_spatial$subbasin))

    # Can also set the label and select items
    updateSelectizeInput(session, 
                         server = FALSE, 
                         "subbasins_id",
                         label = "2. Select subbasins",
                         choices = subbasins,
                         selected = head(subbasins, 1)
                         )
    #  It seems the followin has no effect:
    # and when it is outside observe, it produces errors!
    curr_spatial <- curr_spatial %>% 
                    filter(subbasin %in% input$subbasins_id) %>% 
                    data.table()
  })

Any input? please. I did put the data and the whole code in google drive: https://drive.google.com/file/d/1qaZG6-VmBhIgMsxs5dffX9PmagkMhuB8/view?usp=sharing

OverFlow Police
  • 861
  • 6
  • 23
  • do youb miss an if-else clause with curly bracktes here? `subbasins` would always be overwritten from one line to the other.. – mnist Nov 25 '19 at 23:28
  • you mean in `if (is.null(input$subbasins_id)) subbasins <- character(0)` ? I had copied that from some code of R-documentation (https://shiny.rstudio.com/reference/shiny/latest/updateSelectInput.html). So, i thought since it is one-liner there is no need for curly bracket. At any rate, I got rid of that part. – OverFlow Police Nov 25 '19 at 23:31
  • no need for curly brackets if it is indeed in a single line, not two. Furthermore, if you assign another value just afterwards it gets overwritten anyway. – mnist Nov 25 '19 at 23:36
  • so, I do not know what you meant by your first comment: "do youb miss an if-else clause with curly bracktes here? subbasins would always be overwritten from one line to the other" – OverFlow Police Nov 25 '19 at 23:37
  • 1
    Besides that, please try to provide a [reproducible minimal example](https://stackoverflow.com/q/5963269/8107362). This requires effort to boil the code down and to the bare bones of the problem but it might even help you to figure out on yourself what the actual problem is – mnist Nov 25 '19 at 23:38
  • Since you have now deleted this part, it is no longer applicable, of course. Prior to that, it should have been like `if(TRUE) {x <- 3} else {x <- 5}` – mnist Nov 25 '19 at 23:40
  • The code shared on google drive is minimal. nothing complicated there. – OverFlow Police Nov 25 '19 at 23:42

1 Answers1

0

The second selectInput should render from the server and not from the UI to be interactive.

ui.R

selectInput(inputId = 'countyType_id',
            label = '1. Select a basin',
            choices = all_basins
            ),

uiOutput('subbasins_id')

server.R



output$subbasins_id <- renderUI({

## add some code to filter subbasin based on the selected basin, i.e. input$countyType_id

curr_spatial <- curr_spatial %>% 
                    filter(subbasin %in% input$subbasins_id) %>% 
                    data.table()
subbasins <- sort(unique(curr_spatial$subbasin))

selectizeInput(inputId = 'subbasins_id',
               label = '2. Select subbasins', 
               choices = subbasins, 
               selected = head(subbasins, 1),
               multiple = TRUE)

}) 


maede rayati
  • 756
  • 5
  • 10