【发布时间】:2014-09-12 10:41:58
【问题描述】:
我正在尝试创建一个闪亮的应用程序,它允许您根据用户定义的子分析下载格式良好的 PDF 报告。我发现this gist 包含一个最小的示例,它工作得很好。但是,当我尝试根据 Rstudio 库中的 'Miles-per-gallon' example 添加绘图时,在尝试调整代码时遇到了一些错误。
这是我的server.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'
)
})
这是我的ui.r 代码
library(shiny)
library(datasets)
shinyUI(fluidPage(
# Application title
titlePanel("Miles Per Gallon"),
# Sidebar with controls to select the variable to plot against mpg
# and to specify whether outliers should be included
sidebarLayout(
sidebarPanel(
textInput('firstname', 'First name', value = 'Adam'),
textInput('lastname', 'Last name', value = 'Smith'),
downloadButton('report'),
selectInput("variable", "Variable:",
c("Cylinders" = "cyl",
"Transmission" = "am",
"Gears" = "gear")),
checkboxInput("outliers", "Show outliers", FALSE)
),
# Show the caption and plot of the requested variable against mpg
mainPanel(
h3(textOutput("caption")),
plotOutput("mpgPlot")
)
)
))
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}
我已经玩了几个小时了,但我很困惑。 input$names 部分工作正常,但我不知道如何引入 reactive 情节。错误/警告消息是 'Error: object ’input’ not found',所以我知道它没有检测到从 ui.R 脚本传递的 input$variable 更改。感谢您帮助解决此问题
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
【问题讨论】:
-
这很有趣。你能用基本图形替换ggplot2图形,看看它是否有效?例如
plot(as.formula(formulaText()), data = mpgData) -
@Yihui 谢谢您,您的建议奏效了,将
input.Rnw中的print(myplot1())替换为plot(as.formula(formulaText()), data = mpgData)。我已经尝试将其调整为 ggplot2,将ggPlotText <- reactive({paste('mpg,', input$variable)})添加到server.R,然后将ggplot(mpgData, aes(ggPlotText())) + geom_line()添加到input.Rnw,这似乎遵循formulaText()的逻辑,但产生的错误消息显示Error: could not find function "ggPlotText"。是否有一个等效于as.formula()的方法可以用于ggplot2 的aes(x, y)约定?toString没用 -
这听起来像是 ggplot2 变量范围的问题。但是,在您向 ggplot2 作者报告此问题之前,请确保您拥有最新版本的 ggplot2(请
update.packages())。 -
你试过 ggplot 和 aes_string() 吗?这是文档:docs.ggplot2.org/0.9.3/aes_string.html
-
@niczky12 请写下您的建议的答案。我已经这样做了,然后将其删除。我也猜这是解决方案。