0

I'm dealing with a problem to download user requested data. I think I don't get to understand (I'm new in R and Shiny) the downloadHandler behavior. I have no problem to plot the selected file (already stored in R as a raster file BFT_PR, YFT_PR, and FAI), but when trying to download it I always get the same downloadData.txt and Error: server problem. I`ll really appreciate any help with this.

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(), file)}

  )
}
Wil
  • 3,076
  • 2
  • 12
  • 31
  • Hi there and welcome to SO! It's really hard to address problems with `downloadHandler` without sample data and a working app to try out. Since you appear to have no problems with plotting, can you provide some dummy data generated in the app (and the rest of a working app) to use as a test? (see here for guidelines for an [mcve](https://stackoverflow.com/questions/48343080/how-to-convert-a-shiny-app-consisting-of-multiple-files-into-an-easily-shareable#comment84299408_48343080) for shiny apps.) – phalteman May 09 '19 at 16:56

2 Answers2

0

Been traveling last month, sorry to answer so late. Here goes an example of my code with some dummy data

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"))),

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)
0

mapInput() needs to return raster, you are getting html file because its failing. If you open the html file, you can read the error.

Aji
  • 133
  • 1
  • 5