2014-07-21 5 views
34

사용자 정의 하위 분석에 따라 멋지게 형식화 된 PDF 보고서를 다운로드 할 수있는 반짝이는 응용 프로그램을 만들려고합니다. 최소의 예제가 포함 된 this gist을 찾았습니다. 제대로 작동합니다. 그러나 Rstudio 갤러리의 'Miles-per-gallon' example을 기반으로 플롯을 추가하려고 시도했을 때 코드를 적용 할 때 몇 가지 오류가 발생했습니다. 내가 놀았 던Shiny 응용 프로그램에서 knitr (LaTeX)을 사용하여 PDF 보고서 작성

\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.Rnw 파일은 다음과 같습니다 동안

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") 

    ) 
) 
)) 

입니다 : 여기

server.R 코드 이제 몇 시간 동안 이것으로 주위에, 나는 꽤 붙어 있어요. 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 그래픽을 기본 그래픽으로 대체하고 작동하는지 확인할 수 있습니까? 예 : –

+1

@Yihui'print (myplot1())'을'plot (as.formula (formulaText()) '로 대체하여 제안 해 주신데 감사드립니다.'plot (as.formula (formulaText()), data = mpgData) data = mpgData)'를'input.Rnw'에 저장합니다. 나는 이것을 ggplot2에 적용하기 위해'server.R'에'ggPlotText <- reactive ({paste ('mpg,', input $ variable)})'를 추가하고'ggplot (mpgData, aes (ggPlotText())) + geom_line()'을'input.Rnw'에''formulaText()'의 논리를 따르는 것으로 보이지만 생성 된 에러 메시지는''에러 : 함수 "ggPlotText"를 찾을 수 없습니다. gsplot2의'aes (x, y)'규칙에 사용할 수있는 as.formula()와 동일한 기능이 있습니까? 'toString'이 작동하지 않았습니다 –

+3

ggplot2 변수 범위 문제 인 것 같습니다. 그러나이 문제를 ggplot2 작성자에게보고하기 전에 최신 버전의 ggplot2 (update.packages()를 수행하십시오)를 확인하십시오. –

답변

3

해결을 도와 주셔서 감사합니다, 당신은 프로그래밍 ggplot에 인수를 전달할 aes_string()를 사용해야합니다. 당신이 받고있는 오류는 '입력'이 문자 값이기 때문에 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

관련 문제