R Shiny下载选项

时间:2019-05-09 11:08:27

标签: r shiny shiny-reactivity

我正在处理下载用户请求的数据的问题。我想我不了解downloadHandler行为(我是R和Shiny的新手)。我可以轻松地绘制选定的文件(已经作为光栅文件BFT_PRYFT_PRFAI存储在R中),但是当尝试下载它时,我总是得到相同的{ {1}}和错误:服务器问题。我会很感激与此有关的任何帮助。

downloadData.txt

1 个答案:

答案 0 :(得分:0)

上个月开始旅行,很抱歉回答得这么晚。这是我的代码示例,其中包含一些伪数据

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)