使用闪亮应用程序中的knitr(乳胶)生成PDF报告
根据用户定义的子分析,我正在尝试创建一个闪亮的应用程序,允许您下载格式良好的PDF报告。我发现其中包含一个最小的示例,效果很好。然而,当我试图添加一个基于Rstudio图库的绘图时,我在尝试修改代码时遇到了一些错误 这是我的使用闪亮应用程序中的knitr(乳胶)生成PDF报告,r,ggplot2,knitr,shiny,R,Ggplot2,Knitr,Shiny,根据用户定义的子分析,我正在尝试创建一个闪亮的应用程序,允许您下载格式良好的PDF报告。我发现其中包含一个最小的示例,效果很好。然而,当我试图添加一个基于Rstudio图库的绘图时,我在尝试修改代码时遇到了一些错误 这是我的服务器。R代码: library(knitr) library(datasets) library(ggplot2) mpgData <- mtcars mpgData$am <- factor(mpgData$am, labels = c("Automatic
服务器。R
代码:
library(knitr)
library(datasets)
library(ggplot2)
mpgData <- mtcars
mpgData$am <- factor(mpgData$am, labels = c("Automatic", "Manual"))
shinyServer(function(input, output) {
formulaText <- reactive({
paste("mpg ~", input$variable)
})
# Return the formula text for printing as a caption
output$caption <- renderText({
formulaText()
})
# Generate a plot of the requested variable against mpg and only
# include outliers if requested
output$mpgPlot <- renderPlot({
boxplot(as.formula(formulaText()),
data = mpgData,
outline = input$outliers)
})
myPlot1 <- reactive({
p <- print(ggplot(mpgData, aes(mpg, input$variable)) +
geom_line())
})
myPlot2 <- reactive({
#renderPlot({
p <- print(
boxplot(as.formula(formulaText()),
data = mpgData,
outline = input$outliers)
)
})
output$report = downloadHandler(
filename = 'myreport.pdf',
content = function(file) {
out = knit2pdf('input.Rnw', clean = TRUE)
file.rename(out, file) # move pdf to file for downloading
},
contentType = 'application/pdf'
)
})
而input.Rnw
文件如下所示:
\documentclass{article}
\begin{document}
<<names>>=
input$firstname
input$lastname
@
<<>>=
#output$mpgPlot ## N.B. This threw an error! Cannot call an object like this from shiny
print(myPlot1())
@
<<>>=
print(myPlot2())
@
\end{document}
正如我在评论中所建议的,您应该使用
aes\u string()
以编程方式将参数传入ggplot
。您得到的错误是因为“input”是一个字符值,而aes()需要不带引号的名称。下面是如何替换对aes(mpg,input$variable)的调用。
:
myPlot1这很有趣。能否将ggplot2图形替换为基本图形并查看其是否有效?e、 g.plot(as.formula(formulaText()),data=mpgData)
@Yihui谢谢你,你的建议奏效了,在input.Rnw
中将print(myplot1())
替换为plot(as.formulaText(),data=mpgData)
。我已经尝试将其应用于ggplot2,通过添加ggPlotText,这听起来像是ggplot2变量范围的问题。但是,在向ggplot2作者报告此问题之前,请确保您拥有最新版本的ggplot2(doupdate.packages()
)。您是否尝试过使用aes_string()的ggplot?以下是文档:@niczky12请对您的建议做出答复。我已经这样做了,然后会删除它。我也猜这就是解决办法。酷!谢谢你解决这个问题@niczky12@niczky12,我试过你的解决办法。但当我点击下载时,同一页打开了另一个标签。。
\documentclass{article}
\begin{document}
<<names>>=
input$firstname
input$lastname
@
<<>>=
#output$mpgPlot ## N.B. This threw an error! Cannot call an object like this from shiny
print(myPlot1())
@
<<>>=
print(myPlot2())
@
\end{document}
sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-apple-darwin13.1.0 (64-bit)
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_0.9.3.1.99 knitr_1.6 shiny_0.10.0
loaded via a namespace (and not attached):
[1] bitops_1.0-6 caTools_1.17 colorspace_1.2-4 digest_0.6.4 evaluate_0.5.5 formatR_0.10 grid_3.1.0 gtable_0.1.2
[9] highr_0.3 htmltools_0.2.4 httpuv_1.3.0 labeling_0.2 MASS_7.3-33 munsell_0.4.2 plyr_1.8.1 proto_0.3-10
[17] RColorBrewer_1.0-5 Rcpp_0.11.2 reshape2_1.4 RJSONIO_1.2-0.2 scales_0.2.4 stringr_0.6.2 tools_3.1.0 xtable_1.7-3
myPlot1 <- reactive({
p <- print(ggplot(mpgData, aes_string('mpg', input$variable)) +
geom_line())
})