2014-03-28 48 views
1

我试图应用this question节省情节给出的答案和this question下载无功输出没有成功。我不确定我的反应函数是否输出了错误的数据类型,或者我的downloadHandler()写入不正确。包含反应性对象在闪亮的出口

此外,链接的问题通过function() s到reactive()我被警告已被弃用,所以我在这里避免它。 (该行为并没有改变,虽然)。

ui.R:

library(shiny) 

# Define UI for application 
shinyUI(pageWithSidebar(

    # Application title 
    headerPanel("App"), 

    sidebarPanel(
    downloadButton("savemap", label="Download Map (Hires)") 
), 

    mainPanel(
    tabsetPanel(
     tabPanel("Map", 
       plotOutput("myworld", height="650px",width="750px", 
          clickId="plotclick") 
    ) 
    ) 
) 
)) 

server.R

library(shiny) 
library(maps) 
library(mapdata) 
library(rworldmap) 
library(gridExtra) 

shinyServer(function(input, output) { 

    theworld <- reactive({ 
    myplot <- map("world2", wrap=TRUE, plot=TRUE, 
     resolution=2) 
    }) 

    output$myworld <- renderPlot({ 
    print(theworld()) 
    }) 

    output$savemap <- downloadHandler(
    filename = function() { 
     paste('fuzzymap-', Sys.Date(), '.png', sep="") 
    }, 
    content = function(file) { 
     # function to actually write the image file 
     # https://stackoverflow.com/questions/14810409/save-plots-made-in-a-shiny-app?rq=1 
     png(file) 
     print(theworld()) 
     dev.off() 
    }) 

}) 

中的活性功能的地图在启动时成功地绘制。下载提示生成并且一个png文件下载,但它不包含任何数据。此外,将返回以下非致命错误,但搜索结果显示,没有线索:

Error opening file: 2 
Error reading: 9 

回答

4

我觉得你无所适从map()回报。你称它为 绘制图的副作用,而不是返回值。而不是使其成为一个反应性值,只需将其保留为匿名函数即可。

下面是一个简化版本,为我的作品:

library(shiny) 
library(maps) 

ui <- bootstrapPage(
    plotOutput("myworld", height = 450, width = 750), 
    downloadButton("savemap", "Download map") 
) 

theworld <- function() { 
    map("world2", wrap = TRUE, resolution = 2) 
} 

server <- function(input, output) { 
    output$myworld <- renderPlot({ 
    theworld() 
    }) 

    output$savemap <- downloadHandler(
    filename = function() { 
     paste0("fuzzymap-", Sys.Date(), ".png") 
    }, 
    content = function(file) { 
     png(file) 
     theworld() 
     dev.off() 
    } 
) 
} 

runApp(list(server = server, ui = ui)) 
+0

@haddley:我有在一个zip下载多个文件一些困难。你介意看看http://stackoverflow.com/questions/27067975/shiny-r-zip-multiple-pdfs-for-download/27068514#27068514 –