【发布时间】:2015-01-08 21:12:09
【问题描述】:
我制作了一个小应用程序来尝试学习使用闪亮将绘图导出到文件。目前我正在尝试使用观察者,并且我已经成功地导出了图,但不是以我想要的方式。在大多数情况下,我制作了这个应用程序,复制了闪亮画廊中的观察者演示,并更新以尝试制作 pdf。 UI 只有两个操作按钮(input$ex 和 input$plot)和一个用于更改样本大小的滑块。它输出两个随机法线相互对比的图。我尝试使用以下代码输出绘图。
pdf.check<-reactive({
#'this should change when you hit the plot button, or when you change the sample size,
#'but not when you hit the export button. Whenever you hit the export button
#'pdf.check!=input$ex
input$n
input$plot
isolate({input$ex})
})
obs <- observer({
if(input$ex>0 & input$ex!=isolate({pdf.check()})){
#'when you hit the export button, this should start a pdf. it shouldn't do it
#'for any other input, or when you open the app
pdf(file=logfilename, width=6, height = 5)
}
#'I included the input$plot to make this run whenever you hit the plot button, and
#'it should just be making these graphs.
input$plot
#'isolated input$n so this code doesn't run when you change n
isolate({plot(rnorm(input$n), rnorm(input$n), pch=20, xlab="X", ylab = "Y", main= input$plot)})
})
这实际上是在我每次点击绘图按钮时导出为 pdf,但在我移动 n 滑块后它不会再导出。此外,如果我已经移动了 n 滑块,我必须按两次导出按钮才能启动 pdf。我对观察者的了解不够,无法知道我做错了什么。
我遇到的另一个问题是应用程序在关闭时无法运行dev.off()。我可以包含一些手动运行dev.off() 的内容,但如果有人不小心关闭了应用程序而没有这样做,我希望 pdf 仍然关闭。我尝试使用以下内容。
session$onSessionEnded(function() {
dev.off()
unlink(logfilename)
})
老实说,我对观察者仍然很缺乏经验,而且我不完全确定我理解它是如何运作的。任何可以提供的帮助将不胜感激。
【问题讨论】:
标签: r pdf-generation shiny