Pass on object from selectInput()

shiny

#1

Hello, I am having a problem at using the selectInput() menu in shiny:

What I want to do is to select a variable (a raster) in the drop-down menu and sample data from that variable with point data. The point data part is no problem, however, the app always crashes when I try to select the raster.

The selectInput() looks like this in the UI:

selectInput(
  inputId="env.choice", 
  label= "choose parameter", 
  choices = c("bio1", "bio2"), 
  multiple = FALSE, 
  selectize = FALSE
)

On the server site this should happen:

env.input <- reactive({
    switch(input$env.choice,
           "bio1" = rasters$bio1,
           "bio2" = rasters$bio2
           )
  })
  
  
  observeEvent(input$sample.environment,{
    env.histo<-function(point.data, env.raster) {
      pts<-read.csv(point.data)
      pts.sp<-SpatialPoints(coords = pts[, c("lon", "lat")])
      raster <- env.raster
      env.data<-extract(raster, pts.sp)
      renderPlot({hist(env.data)})
    }
    output$histo <- env.histo(input$path, env.input)
   })

With this code I get the error message unable to find an inherited method for function ‘extract’ for signature ‘"reactiveExpr", "SpatialPoints".

So, the object which is used in the function is not the raster (rasters$bio1) but some kind of other object. I also tried like this:

selectInput(
  inputId="env.choice", 
  label= "choose parameter", 
  choices = c("bio1" = "rasters$bio1", "bio2" = "rasters$bio2"), 
  multiple = FALSE, 
  selectize = FALSE
)

but the error message just changed to unable to find an inherited method for function ‘extract’ for signature ‘"character", "SpatialPoints".

Any help would be highly appreciated!


#2

Thank you for the minimal parts to reproduce the issue!

I believe you are missing a () when retrieving you retrieve the value of the reactive.

Current:

output$histo <- env.histo(input$path, env.input)

Updated:

output$histo <- env.histo(input$path, env.input())