Create cascading/dynamic selectInput() in many steps with dplyr::filter() doesn't work

93 views
Skip to first unread message

Alexandre Santos

unread,
Jun 3, 2021, 4:50:58 PM6/3/21
to Shiny - Web Framework for R
In my shiny example below I have 3 variables (PEST, DATE_S2 and PROJECT). I would like that when I select Project, the variables  DATE_S2  and PEST would only be those contained in the selection made in Project in the input using cascading/dynamic selectInput() with filters. Here is my detaild example:

```
# Packages
library(rgdal)
library(shiny)
library(leaflet)
library(leaflet.providers)
library(ggplot2)
library(shinythemes)
library(sf)
library(lubridate)
library(dplyr)


# get AOI
download.file(
  zip_path <- tempfile(fileext = ".zip")
)
unzip(zip_path, exdir = tempdir())

# Open the files
setwd(tempdir())
stands_extent <- readOGR(".", "stands_target") # Border
stands_ds <- read.csv("pred_target_stands.csv", sep=";") # Data set
stands_ds <- stands_ds %>%
  mutate(DATA_S2 = ymd(DATA_S2))

# Create the shiny dash
ui <- fluidPage(
  theme = shinytheme("cosmo"),
  titlePanel(title="My Map Dashboard"),  
  sidebarLayout(
    sidebarPanel(
      uiOutput("selectedvariable0"),
      uiOutput("selectedvariable1"),
      uiOutput("selectedvariable2"),
    ),
    mainPanel(
      textOutput("idSaida"),
      fluidRow(
        splitLayout(plotOutput("myplot"))),
      dateInput(inputId = "Dates selection", label = "Time"),
      leafletOutput("map") 
    )
  )
)

server <- function(input, output, session){
  
  
  # Importing data and save it temporary in data variable
  data <- reactive({stands_ds})
  
  currentvariable0 <- reactive({input$selectedvariable0})
  currentvariable1 <- reactive({input$selectedvariable1})
  currentvariable2 <- reactive({input$selectedvariable2})
  currentvariable3 <- reactive({input$selectedvariable3})
  currentvariable4 <- reactive({input$selectedvariable4})
  
  # Creating filters
  output$selectedvariable0 <- renderUI({
    selectInput(inputId = "selectedvariable0", "Type A",choices = var_pest(), selected = TRUE)
  })
  output$selectedvariable1 <- renderUI({
    selectInput(inputId = "selectedvariable1", "Type B",choices = var_moni(), selected = TRUE)
  })
  output$selectedvariable2 <- renderUI({
    selectInput(inputId = "selectedvariable2", "Type C",choices = var_proj(), selected = TRUE)
  })
  
  # Creating reactive function to subset data
  pest_function <- reactive({
    file1 <- data()
    pest <- req(input$selectedvariable0)
    file2 <- file1 %>% dplyr::filter(PEST==pest)
    return (file2)  
  })
  
  var_moni <- reactive({
    file1 <- pest_function()
    if(is.null(file1)){return()}
    as.list(unique(file1$DATA_S2))
  })
  
  moni_function <- reactive({
    file1 <- pest_function()
    moni <- req(input$selectedvariable1)
    file2 <- file1 %>% dplyr::filter(DATA_S2==as.Date(moni))
    return (file2)  
  })
  
  var_proj <- reactive({
    file1 <- moni_function()
    if(is.null(file1)){return()}
    as.list(unique(file1$PROJETO))
  })
  
  proj_function <- reactive({
    file1 <- moni_function()
    proj <- req(input$selectedvariable2)
    file2 <- file1 %>% dplyr::filter(PROJETO==proj)
    return (file2) 
  })
  
  var_tal <- reactive({
    file1 <- proj_function()
    if(is.null(file1)){return()}
    as.list(unique(file1$CD_TALHAO))
  })
  
  
  output$myplot <- renderPlot({
    
    #Subset stand
    stands_sel <- subset(stands_extent, stands_extent@data$ID_UNIQUE==currentvariable4())
    
    #Subset for input$var and assign this subset to new object, "fbar"
    ds_sel<- stands_ds[stands_ds$ID_UNIQUE==currentvariable4(),]
    
    #Create a map
    polys <- st_as_sf(stands_sel)
    ggplot() +
      geom_sf(data=polys) +
      geom_point(data=ds_sel,
                 aes(x=X, y=Y), color="red") +
      xlab("Longitude") + ylab("Latitude") +
      coord_sf() +
      theme_bw() +
      theme(text = element_text(size=10)) 
  })
  
  output$map <- renderLeaflet({
    
    stands_actual<-stands_ds[stands_ds$ID_UNIQUE==currentvariable4(),]
    lng <- mean(stands_actual$X)
    lat <- mean(stands_actual$Y)
    
    leaflet() %>%
      setView(lng = lng, lat = lat, zoom=17) %>%
      addProviderTiles(providers$Esri.WorldImagery) %>%                   
      addMarkers(lng=stands_actual$X, lat=stands_actual$Y, popup="Location")
    
  })   
}
shinyApp(ui, server)
##
```
But not a way to make this work. Please any help for me fix it?

Thanks in advance!!!
Reply all
Reply to author
Forward
0 new messages