Re: A Problem Associated with Rendering Tmaps in Shiny

183 views
Skip to first unread message

Patrick Kalonde

unread,
Jan 1, 2022, 11:05:13 PM1/1/22
to Shiny - Web Framework for R
Hello Shiny Community,

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"

Thank you
Below is the code
#==========================================================#
#TEST 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)

Luca Valnegri

unread,
Jan 2, 2022, 8:52:47 AM1/2/22
to Shiny - Web Framework for R
Hi there,

there are a few problems there:
1) you need to use sf and not sp (and possibly terra instead of raster, sp and raster are "old" and deprecated in a while)
2)  to read the file you need a different syntax:
      test_reactive <- eventReactive(input$load,{
           st_read(input$filedata$datapath)
)}

3) keep in mind that reactive objects are functions, so when you call any of them you need parenthesis:
tm_shape(test_reactive())+
tm_rgb()
and don't get too disappointed, I've been writing shiny apps for nearly ten years now and I still do mistakes like this on a daily basis (like forgetting commas!)

hope this helps,
Luca

Luca Valnegri

unread,
Jan 3, 2022, 9:35:15 AM1/3/22
to Shiny - Web Framework for R
oh, I forgot you should add a check on the existence of the objects at start time to avoid NULL errors, something like this:

  test_reactive <- eventReactive(input$load,{
     req(input$filedata)
    st_read(input$filedata$datapath)
  })
  output$map <- renderTmap({
      req(test_reactive())
    tm_shape(test_reactive())+
      tm_rgb()
  })

obviously, in a prod env you should also add some checks on the nature of the file to avoid user mistakes

Luca

Patrick Kalonde

unread,
Jan 3, 2022, 5:01:36 PM1/3/22
to Luca Valnegri, Shiny - Web Framework for R
Thanks Luca. I used the code with req() and replaced st-read with raster and now my code works perfectly. Thanks so much.

Patrick

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/ba8ef245-29df-4ec4-b099-c3b1d863ac0en%40googlegroups.com.


--


Patrick Ken Kalonde

GIS Student: MS Geographical Information Science (Fulbright)

St Cloud State University, Department of Geography and Planning 

Phone: +1(320) 339-5898 


Co founder: Nyasa Aerial Data Solutions Ltd   Facebook  Linkedin  Website

Community Service: Youth for Environmental Development  Facebook  Linkedin 

 

 




Reply all
Reply to author
Forward
0 new messages