I am learning to build interactive web apps using Shiny (I am completely new to developing shiny apps).
The test app that I am building is very simple: "One has to add a raster image and load it so that it should be rendered on a map".
I have developed both the UI and the server. In the server, I added embedded an "eventReactive" to trigger loading of the raster file from the input to the map that is supposed to be on the right side of the screen.
While that this appears so simple, but my attempts are not working. I am encountering this specific error “ERROR: Object test_reactive is neither from class sf, stars, Spatial, Raster, nor SpatRaster”.
But when I load the raster file directly without using the button which I created for loading the raster file, everything works perfectly. This of course tells me that there is a problem with how I have implemented the "eventReactive", and my questions are: "why are my file input and the reactive button not working as expected? How can the two be improved to work as indicated in my description"
Below is the code
#==========================================================#
library(sp)
library(raster)
library(tmap)
library(leaflet)
#Composite <- raster(#“a link to my any raster stored in your hard drive, but I had an RGB composite which I was using”)
#######
ui <- fluidPage(
titlePanel("Shiny Test"),
sidebarLayout(
sidebarPanel(
fileInput("filedata", "Add Raster File: "),
actionButton("load", "View on the sidemap"),
),
mainPanel(
tmapOutput("map")
#replace with map2 if you are testing the direct approach
server <- function(input,output, session){
test_reactive <- eventReactive(input$load,{
input$filename
})
output$map <- renderTmap({
tm_shape(test_reactive)+
tm_rgb()
})
#To test this, do not forget to change the output in the UI
# Direct approach
output$map2 <- renderTmap({
tm_shape(Composite)+
tm_rgb()
})
}
shinyApp(ui = ui, server = server)