根据用户定义的子分析,我正在try 创建一个shiny 的应用程序,允许您下载格式良好的PDF报告.我发现this gist包含一个最小的例子,效果很好.然而,当我试图添加一个基于Rstudio gallery 'Miles-per-gallon' example的绘图时,我在try 修改代码时遇到了一些错误.

这是我的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脚本传递的输入$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

推荐答案

正如我在 comments 中所建议的,应该使用aes_string()以编程方式将参数传入ggplot.您得到的错误是因为"input"是一个字符值,而aes()需要不带引号的名称.以下是您应该如何替换aes(mpg, input$variable)的通话:

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

R相关问答推荐

如何在四进制仪表板值框中显示值(使用shiny 的服务器计算)

咕噜中的元素列表:map

有没有一个R函数允许你从一个数字变量中提取一个数字,而不考虑它的位置(不仅仅是第一个或最后一个数字?

用值序列对行进行子集化,并标识序列开始的列

在另存为PNG之前隐藏htmlwidget绘图元素

用关联字符串替换列名的元素

从多个线性回归模型中提取系数

合并后返回列表的数据帧列表

您是否可以将组添加到堆叠的柱状图

使用RSelenium在R中抓取Reddit时捕获多个标签

从非重叠(非滚动)周期中的最新数据向后开窗并在周期内计数

派生程序包| ;无法检索';return()';的正文

正则表达式在第二个管道和第二个T之后拆分R中的列

为R中的16组参数生成10000个样本的有效方法是什么?

对R中的列表列执行ROW Mean操作

使用LAG和dplyr执行计算,以便按行和按组迭代

通过比较来自多个数据框的值和R中的条件来添加新列

组合名称具有模式的列表的元素

列间序列生成器的功能

向数据添加标签