私は、ユーザー定義のサブ分析に従って、きれいにフォーマットされたPDFレポートをダウンロードできる素晴らしいアプリケーションを作成しようとしています。この要点最小限の例が含まれていますが、問題なく動作します。しかし、プロットを追加しようとしたとき、「マイル/ガロン」の例Rstudio ギャラリーからコードを適用しようとしたときに、いくつかのエラーが発生しました。
これが私の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'
、スクリプトから渡される input$variable の変更を検出していないことがわかりますui.R
。この問題を解決するのにご協力いただきありがとうございます。
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
ベストアンサー1
私のコメントで示唆されているように、 を使用しaes_string()
てプログラム的に引数を に渡す必要がありますggplot
。 は引用符で囲まれていない名前を期待しているのに対し、 'input' は文字値であるため、エラーが発生します。aes()
の呼び出しを に置き換える方法は次のとおりですaes(mpg, input$variable)
。
myPlot1 <- reactive({
p <- print(ggplot(mpgData, aes_string('mpg', input$variable)) +
geom_line())
})