我试图找出如何使用downloadButton保存闪亮的曲线图。 在包装中的实施例证实downloadButton / downloadHandler保存一个.csv。 我打算做基于一个可重复的例子。
对于ui.R
shinyUI(pageWithSidebar(
headerPanel('Downloading Data'),
sidebarPanel(
selectInput("dataset", "Choose a dataset:",
choices = c("rock", "pressure", "cars")),
downloadButton('downloadData', 'Download Data'),
downloadButton('downloadPlot', 'Download Plot')
),
mainPanel(
plotOutput('plot')
)
))
对于server.R
library(ggplot2)
shinyServer(function(input, output) {
datasetInput <- reactive({
switch(input$dataset,
"rock" = rock,
"pressure" = pressure,
"cars" = cars)
})
plotInput <- reactive({
df <- datasetInput()
p <-ggplot(df, aes_string(x=names(df)[1], y=names(df)[2])) +
geom_point()
})
output$plot <- renderPlot({
print(plotInput())
})
output$downloadData <- downloadHandler(
filename = function() { paste(input$dataset, '.csv', sep='') },
content = function(file) {
write.csv(datatasetInput(), file)
}
)
output$downloadPlot <- downloadHandler(
filename = function() { paste(input$dataset, '.png', sep='') },
content = function(file) {
ggsave(file,plotInput())
}
)
})
如果你回答这个问题,你可能熟悉这一点,但得到这个工作,保存到上述两个脚本( ui.R
和server.R
放到一个文件夹( foo
工作目录内)。为了运行闪亮应用程序,运行runApp("foo")
使用ggsave
,我得到这表明ggsave不能使用的错误信息filename
功能(我认为)。 如果我使用标准的图形设备(如下图所示),在Download Plot
工作没有错误,但它不写图形。
任何提示,让downloadHandler书写绘图工作,将不胜感激。
不知道,如果这个问题仍然有效,但它是想出了寻找“闪亮的应用节省自留地”时,所以我想快速添加如何让ggsave沿原题的线条与downloadHandler工作的第一个。
通过使用直接输出,而不是ggsave和替代战略朱巴提出的替代策略通过alexwhan自己都工作很好的建议,这仅仅是为那些谁绝对要使用在downloadHandler ggsave)。
通过alexwhan报告的问题是由ggsave尝试匹配的文件扩展名来正确的图形设备引起的。 临时文件,但并不那么匹配失败分机。 这可以通过专门设置的装置,按来补救ggsave
在原始代码中示例的函数调用,像这样(为一个PNG):
output$downloadPlot <- downloadHandler(
filename = function() { paste(input$dataset, '.png', sep='') },
content = function(file) {
device <- function(..., width, height) grDevices::png(..., width = width, height = height, res = 300, units = "in")
ggsave(file, plot = plotInput(), device = device)
}
)
这个调用基本上采用的device
功能的png
该ggsave
内部分配(你可以看看ggsave
功能代码,请参阅语法jpg
, pdf
等)。 或许,理想情况下,人们可以指定文件扩展名(如果与文件名不同-比如这里的情况下临时文件)作为ggsave
参数,但该选项目前不在可用ggsave
。
一个最小的自包含的工作示例:
library(shiny)
library(ggplot2)
runApp(list(
ui = fluidPage(downloadButton('foo')),
server = function(input, output) {
plotInput = function() {
qplot(speed, dist, data = cars)
}
output$foo = downloadHandler(
filename = 'test.png',
content = function(file) {
device <- function(..., width, height) {
grDevices::png(..., width = width, height = height,
res = 300, units = "in")
}
ggsave(file, plot = plotInput(), device = device)
})
}
))
sessionInfo()
# R version 3.1.1 (2014-07-10)
# Platform: x86_64-pc-linux-gnu (64-bit)
#
# locale:
# [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
# [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
# [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
# [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
# [9] LC_ADDRESS=C LC_TELEPHONE=C
# [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
#
# attached base packages:
# [1] stats graphics grDevices utils datasets methods base
#
# other attached packages:
# [1] ggplot2_1.0.0 shiny_0.10.1
#
# loaded via a namespace (and not attached):
# [1] bitops_1.0-6 caTools_1.17 colorspace_1.2-4 digest_0.6.4
# [5] formatR_1.0 grid_3.1.1 gtable_0.1.2 htmltools_0.2.6
# [9] httpuv_1.3.0 labeling_0.2 MASS_7.3-34 munsell_0.4.2
# [13] plyr_1.8.1 proto_0.3-10 Rcpp_0.11.2 reshape2_1.4
# [17] RJSONIO_1.3-0 scales_0.2.4 stringr_0.6.2 tools_3.1.1
# [21] xtable_1.7-3
更新
作为GGPLOT2版本2.0.0,所述ggsave
功能支持的字符输入device
的参数,这意味着由downloadHandler创建的临时文件现在可以直接调用保存到ggsave
通过指定扩展到使用应该是例如"pdf"
(而不是传递在设备功能)。 这简化了上面的例子中,以下面的
output$downloadPlot <- downloadHandler(
filename = function() { paste(input$dataset, '.png', sep='') },
content = function(file) {
ggsave(file, plot = plotInput(), device = "png")
}
)
下面是允许使用ggsave保存闪亮地块的解决方案。 它使用一个逻辑复选框和文本输入调用ggsave()
这增加了ui.R
内部文件sidebarPanel
:
textInput('filename', "Filename"),
checkboxInput('savePlot', "Check to save")
然后将其添加到server.R
文件,而不是当前的output$plot
reactivePlot功能:
output$plot <- reactivePlot(function() {
name <- paste0(input$filename, ".png")
if(input$savePlot) {
ggsave(name, plotInput(), type="cairo-png")
}
else print(plotInput())
})
然后,用户可以输入文本框(不扩展)所需的文件名,并勾选复选框在应用目录保存。 取消选中该复选框再次打印的情节。 我敢肯定有这样做的简洁的方式,但至少我现在可以使用Windows ggsave和开罗好得多PNG图形。
请新增你可能有任何建议。
我没有管理,使之与工作ggsave
,而是一个标准的呼叫png()
这似乎是好的。
我只是改变了output$downloadPlot
你的一部分server.R
文件:
output$downloadPlot <- downloadHandler(
filename = function() { paste(input$dataset, '.png', sep='') },
content = function(file) {
png(file)
print(plotInput())
dev.off()
})
请注意,我有一些问题,与0.3版本的光泽,但它从GitHub的最新作品:
library(devtools)
install_github("shiny","rstudio")
这是老了,但还是当有人网上搜寻“R闪亮保存ggplot”,所以我将有助于另一个解决办法的最高命中。 很简单......在显示你的图形,将图形保存在服务器上的文件相同功能的调用ggsave。
output$plot <- renderPlot({
ggsave("plot.pdf", plotInput())
plotInput()
})
然后,使用downloadHandler并使用file.copy()
从现有文件数据写入到“档案”参数。
output$dndPlot <- downloadHandler(
filename = function() {
"plot.pdf"
},
content = function(file) {
file.copy("plot.pdf", file, overwrite=TRUE)
}
)
我的作品。