Download reactive spplot

Hi! Im trying to download a raster as geotif. The reactive function works fine to display using spplot the selected raster, but Im not able to download any file. Im very new in R and shiny. Ill really appreciate any suggestions. BFT_PR, YFT_PR and FAI are stored raster files.

cheers

Alberto Abad

server <- function(input, output) {
  
  output$range <- renderText({paste("Forecast from", timestart, "to", timeend)})
  
  # Reactive value for selected dataset ----
 
  output$model <- renderPlot({
 
    mapInput <- reactive({
    switch(input$model,
           "Bluefin tuna" = BFT_PR,
           "Yellowfin tuna" = YFT_PR,
           "Fishing Aptitude Index"=FAI)})   
    
    spplot(mapInput())
    
  })
  
  
  
  # Downloadable map shapefile  ----
  
  output$downloadData <- downloadHandler(
    
    filename = function() {
      paste(input$model, ".tif", sep = "")
    },
    
    
    content = function(file) {
      
      writeRaster(mapInput(), file, format="GTiff", overwrite=TRUE)
      
    }
  )
  
}

Hi @albertuna, a fully reproducible example would help us identify the issue

If that's not possible, we'll need to know what packages the spplot() and writeRaster() functions belong to.

Hi! Thanks for your answer. I´ve tried to build up a reprex, but without success for the moment. spplot is fron the sp library, and writeRaster from raster library. Also rgdal is needed for the geotiff format.

As I told you, BFT_PR, YFT_PR , and FAI are rasters stored in the working directory. Is there a way to upload this data so you can run the script??

Alberto

###------------------------------GEO-ATUN SHINY INTERFACE ---------------------------------###


library(shiny)
library(leaflet)
library(raster)
library(rgdal)
library (sp)


##-----------------------------Define User Interface-------------------------##

ui <- fluidPage(
  
  # App title ----
  titlePanel((p("TUNAGOM TOOL", style = "color:#3474A7"))),

  
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
    
    # Sidebar panel for inputs ----
   
    sidebarPanel(
      
      helpText("Weekly forecasts.
                Bluefin and Yellowfin tuna catch per unit effort (fish/1000 hooks).
                Fishing Aptitude Index "),
      
      # Distribution maps: Species distribution, Fishing aptitude index. Display menu
      
        # Input: select forecast dataset 
          
         selectInput("model", "Select Forecast:",
                  choices = c("Bluefin tuna", "Yellowfin tuna", "Fishing Aptitude Index")),
      
        # Download data button
        
          downloadButton("downloadData", "Download data")
  
       
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      # Temporal range (text output)
      
      textOutput("range"),
      
      # Output: maps
    
      plotOutput("model"))
    )
  )

      

##------------------------------Define server logic -------------------------##


server <- function(input, output) {
  
  output$range <- renderText({paste("Forecast from", timestart, "to", timeend)})
  
  # Reactive value for selected dataset ----
 
  output$model <- renderPlot({
 
    mapInput <- reactive({
    switch(input$model,
           "Bluefin tuna" = BFT_PR,
           "Yellowfin tuna" = YFT_PR,
           "Fishing Aptitude Index"=FAI)})   
    
    palette <-colorRampPalette(c("blue", "#007FFF", "cyan",
                                      "#7FFF7F", "yellow", "#FF7F00", "red" ))(100)
    
    spplot(mapInput(), col.regions=palette, sp.layout= list(landmask_m, landmask_u, FZ),scales=list(draw = TRUE))
    
  })
  
  
  
  # Download map as geotiff  ----
  
  output$downloadData <- downloadHandler(
    
    filename = function() {paste0(input$model, ".tif")},
    
    
    content = function(file) {writeRaster(mapInput(), filename=filename, file, format="GTiff", overwrite=TRUE)}
  
  )
}
  
##------------------------------Run Shiny App--------------------------------##

runApp(shinyApp(ui, server), launch.browser = TRUE)

As I told you, BFT_PR, YFT_PR , and FAI are rasters stored in the working directory. Is there a way to upload this data so you can run the script??

Yes, you could save each using saveRDS() and upload them in a comment.

writeRaster(mapInput(), filename=filename, file, format="GTiff", overwrite=TRUE)

This line is different from your initial snippet. You likely want it the way you had it the first time

writeRaster(mapInput(), filename=file, format="GTiff")

Hi! Sorry, but I´m not able to attach the raster files, upload don´t accept rds files, sure i´m doing something wrong.
Maybe we can use a dummy raster

rr <- raster(matrix(sample(1:100, 100),35,52), xmn= -97.75, xmx=-84.75, ymn=17.75, ymx=26.5)

This appears to work for me, does it work for you?


library(shiny)
library(raster)
library(rgdal)
library (sp)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(downloadButton("downloadData", "Download data")),
    mainPanel(plotOutput("model"))
  )
)


server <- function(input, output) {
  
  output$model <- renderPlot({
    rr <- raster(matrix(sample(1:100, 100),35,52), xmn= -97.75, xmx=-84.75, ymn=17.75, ymx=26.5)
    spplot(rr)
  })
  
  
  output$downloadData <- downloadHandler(
    filename = function() { "simple.tif" },
    content = function(file) {writeRaster(rr, file, format="GTiff", overwrite=TRUE)}
  )
}

runApp(shinyApp(ui, server), launch.browser = TRUE)

Hi! Yes, the example works fine. But when I use more than one inputs with the reactive function it doesnt work. Send you an example. Thanks again for your help!!

Alberto

r1 <- raster(matrix(sample(1:100, 100),35,52), xmn= -97.75, xmx=-84.75, ymn=17.75, ymx=26.5)
r2 <- raster(matrix(sample(100:200, 100),35,52), xmn= -97.75, xmx=-84.75, ymn=17.75, ymx=26.5)
r3 <- raster(matrix(sample(200:300,100),35,52), xmn= -97.75, xmx=-84.75, ymn=17.75, ymx=26.5)

library(shiny)
library(raster)
library(rgdal)
library (sp)

ui <- fluidPage(

App title ----

titlePanel((p("TUNAGOM DST", style = "color:#3474A7"))),

Sidebar layout with input and output definitions ----

sidebarLayout(

# Sidebar panel for inputs ----

sidebarPanel(
  
  helpText("Weekly forecasts.
           Bluefin and Yellowfin tuna catch per unit effort (fish/1000 hooks).
           Fishing Aptitude Index "),
  
  # Distribution maps: Species distribution, Fishing aptitude index. Display menu
  
  # Input: select forecast dataset 
  
  selectInput("model", "Select Forecast:",
              choices = c("r1", "r2", "r3")),
  
  # Download data button
  
  downloadButton("downloadData", "Download data")
  
  
  ),

# Main panel for displaying outputs ----
mainPanel(
  
  # Temporal range (text output)
  
  textOutput("range"),
  
  # Output: maps
  
  plotOutput("model"))
)

)

server <- function(input, output) {

output$range <- renderText({paste("Forecast from", timestart, "to", timeend)})

Reactive value for selected dataset ----

output$model <- renderPlot({

mapInput <- reactive({
  switch(input$model,
         "r1" = r1,
         "r2" = r2,
         "r3"=r3)})   

palette <-colorRampPalette(c("blue", "#007FFF", "cyan",
                             "#7FFF7F", "yellow", "#FF7F00", "red" ))(100)

spplot(mapInput(), col.regions=palette, scales=list(draw = TRUE))

})

output$downloadData <- downloadHandler(

filename = function() {paste0(input$model, ".tif")},


content = function(file) {writeRaster(mapInput(), file, format="GTiff", overwrite=TRUE)}

)
}

runApp(shinyApp(ui, server), launch.browser = TRUE)

   r1 <- raster(matrix(sample(1:100, 100),35,52), xmn= -97.75, xmx=-84.75, ymn=17.75, 
   ymx=26.5)
  r2 <- raster(matrix(sample(100:200, 100),35,52), xmn= -97.75, xmx=-84.75, ymn=17.75, 
   ymx=26.5)
  r3 <- raster(matrix(sample(200:300,100),35,52), xmn= -97.75, xmx=-84.75, ymn=17.75, 
  ymx=26.5)



 library(shiny)
 library(raster)
 library(rgdal)
 library (sp)

 ui <- fluidPage(

 App title ----
 titlePanel((p("TUNAGOM DST", style = "color:#3474A7"))),

 Sidebar layout with input and output definitions ----
 sidebarLayout(

 # Sidebar panel for inputs ----

 sidebarPanel(

   helpText("Weekly forecasts.
       Bluefin and Yellowfin tuna catch per unit effort (fish/1000 hooks).
       Fishing Aptitude Index "),

 # Distribution maps: Species distribution, Fishing aptitude index. Display menu

 # Input: select forecast dataset 

   selectInput("model", "Select Forecast:",
          choices = c("r1", "r2", "r3")),

 # Download data button

   downloadButton("downloadData", "Download data")


   ),

  # Main panel for displaying outputs ----
 mainPanel(

   # Temporal range (text output)

    textOutput("range"),

  # Output: maps

  plotOutput("model"))
  )
  )

  server <- function(input, output) {

  output$range <- renderText({paste("Forecast from", timestart, "to", timeend)})

  Reactive value for selected dataset ----
  output$model <- renderPlot({

  mapInput <- reactive({
   switch(input$model,
     "r1" = r1,
     "r2" = r2,
     "r3"=r3)})   

  palette <-colorRampPalette(c("blue", "#007FFF", "cyan",
                         "#7FFF7F", "yellow", "#FF7F00", "red" ))(100)

  spplot(mapInput(), col.regions=palette, scales=list(draw = TRUE))
  })

  output$downloadData <- downloadHandler(


     content = function(file) {writeRaster(mapInput(), file, format="GTiff", overwrite=TRUE)}
     )
     }

    runApp(shinyApp(ui, server), launch.browser = TRUE)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.