2014-07-21 31 views
34

我想创建一个闪亮的应用程序,它可以让你下载一个很好的格式化的PDF报告,根据用户定义的子分析PDF格式的报告。我发现this gist包含一个最小的例子,它工作得很好。但是,当我尝试添加一个基于Rstudio画廊的'Miles-per-gallon' example的情节时,我尝试调整代码时遇到了一些错误。生产使用knitr(胶乳)从闪亮的应用

这里是我的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脚本传入的输入$变量更改。感谢您的帮助解决这个

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 
+7

这很有趣。你可以使用基础图形替换ggplot2图形,看看它是否有效?例如'图(as.formula(formulaText()),数据= mpgData)' –

+1

@Yihui谢谢您,您的建议的工作,用'情节更换'打印(myplot1())'(as.formula(formulaText()),数据= mpgData)'in'input.Rnw'。我试图适应这GGPLOT2,加入'ggPlotText < - 反应({膏( 'MPG,',输入$变量)})''到server.R'然后'ggplot(mpgData,AES(ggPlotText()) )+ geom_line()'转换为'input.Rnw',它似乎遵循'formulaText()'的逻辑,但是产生的错误信息是“Error:could not find function”ggPlotText“'。是否有可以用于ggplot2的'aes(x,y)'惯例的'as.formula()'的等价物? 'toString'不起作用 –

+3

这听起来像一个ggplot2变量范围问题。然而,在你向ggplot2作者报告这个问题之前,请确保你有最新版本的ggplot2(做'update.packages()')。 –

回答

3

如我的评论所说,你应该使用aes_string()在参数编程进入ggplot。你得到的错误是因为'输入'是一个字符值,而aes()期望没有引号的名字。这里是你应该如何取代呼叫aes(mpg, input$variable)

myPlot1 <- reactive({ 
    p <- print(ggplot(mpgData, aes_string('mpg', input$variable)) + 
    geom_line()) 
}) 
+0

酷!感谢您解决@ niczky12 –

+0

@ niczky12,我试过你的解决方案。但是当我点击下载时,同一页面打开另一个选项卡。[这是我用过的代码](https://rcloud.social/edit.html?notebook=b19713397aed79e0a9aa570c4c30025f)。 [这是个闪亮的页面](https://rcloud.social/shiny.html?notebook=b19713397aed79e0a9aa570c4c30025f) – GANA