2017-03-11 123 views
1

我希望用户能够选择哪个变量位于图形的x轴上。我的数据帧是这样的:闪亮 - 根据用户输入在图形上更改变量

> linegraphdata 
    Year E_coli Rain_Intensity Water_Temperature Dew_Point Humidity Wind_Speed Barometric_Pressure Visibility Cloud_Cover 
1 2006 181.4173 0.004077910   79.51720 60.08460 0.6747836 8.092537   1015.748 9.108940 0.5146045 
2 2007 212.3498 0.005503364   80.36213 60.06608 0.6597432 6.074101   1015.417 9.173431 0.5019472 
3 2008 127.0755 0.003195506   77.77268 58.15240 0.6559700 7.855993   1014.482 9.590509 0.5037079 
4 2009 151.2129 0.004728056   74.56725 55.57986 0.6704676 6.278484   1014.511 9.389780 0.5461991 
5 2010 142.9604 0.005628907   80.73180 61.37344 0.6662863 6.263983   1014.919 9.536397 0.4672130 
6 2011 121.5559 0.005370608   79.44271 60.18298 0.6682699 6.879726   1013.199 9.467782 0.4891249 
7 2012 126.4385 0.002568511   80.99142 57.04450 0.5765744 7.416897   1014.416 9.767905 0.3841238 
8 2013 142.2380 0.004249872   76.72413 58.13510 0.6703193 7.058155   1015.610 9.451731 0.4211622 
9 2014 139.4780 0.006628609   74.93213 58.53608 0.7117897 6.342423   1014.847 9.318628 0.3082917 
10 2015 127.3011 0.004109462   75.38712 58.15503 0.7069821 7.058145   1014.534 9.378120 0.3086044 
11 2016 123.7218 0.003886790   80.46863 63.90303 0.7176918 6.508438   1014.887 9.648991 0.2763920 

的问题是,有光泽,走的是输入$预测面值,只是绘图变量名称,而不是连接到该变量name值的列表。它不认识到所选变量名称与包含值的数据框相关联。

注意:我想要第一个图形是静态的。我只想要第二张图改变。

这是应用程序的外观时,它的运行,如: Static image of app

这里是我的应用程序代码:

library(shiny) 
library(ggplot2) 
library(gridExtra) 

linegraphdata = read.csv("linegraphdata.csv") 

predictor_options <- c("Rain_Intensity", "Water_Temperature", "Dew_Point", "Humidity", "Wind_Speed", 
        "Barometric_Pressure", "Visibility", "Cloud_Cover") 

ui <- fluidPage(fluidRow(
         column(12, offset=0, tags$h1("Pick a Predictor:") 
         ), 
         column(12, offset=0, tags$h4("Which elements of the environment trend with ", tags$i("E. coli"), " levels? Look for 
                 the predictor whose line graph peaks and valleys with the average", tags$i("E. coli"), "levels. ") 
         ), 
         column(4, offset=0, 
    ########################## HERE IS RELEVANT CODE: 
           wellPanel(selectInput("predictor", "Select a Predictor", choices = predictor_options 
           ) 
           ) 
         ), 
         column(10, 
           plotOutput("graph3") 
           ) 
           ) 
) 

server <- function(input, output) { 
    observeEvent(input$predictor, { 
    plot1 <- ggplot(data= linegraphdata, aes(x=Year, y=E_coli)) + geom_line() + theme_bw() 
########################## HERE IS RELEVANT CODE: 
    plot2 <- ggplot(data = linegraphdata, aes(x=Year, y=input$predictor)) + geom_line() + theme_bw() 
    output$graph3 <- renderPlot({ 
    grid.arrange(plot1, plot2, ncol=1) 
    }) 
    }) 
} 

shinyApp(ui = ui, server = server) 

回答

0

我找到了答案!我只需要添加一些将输入连接回数据框(服务器端)的行。我将输入存储在一个reactive()函数中,并删除了observeEvent()函数。

server <- function(input, output) { 

    data <- reactive({ 
if ("Rain_Intensity" %in% input$predictor) return(linegraphdata$Rain_Intensity) 
if ("Water_Temperature" %in% input$predictor) return(linegraphdata$Water_Temperature) 
if ("Dew_Point" %in% input$predictor) return(linegraphdata$Dew_Point) 
if ("Humidity" %in% input$predictor) return(linegraphdata$Humidity) 
if ("Wind_Speed" %in% input$predictor) return(linegraphdata$Wind_Speed) 
if ("Barometric_Pressure" %in% input$predictor) return(linegraphdata$Barometric_Pressure) 
if ("Visibility" %in% input$predictor) return(linegraphdata$Visibility) 
if ("Cloud_Cover" %in% input$predictor) return(linegraphdata$Cloud_Cover) 

    }) 
plot1 <- ggplot(data= linegraphdata, aes(x=Year, y=E_coli)) + geom_line() + theme_bw() 
plot2 <- ggplot(data = linegraphdata, aes(x=Year, y=data())) + geom_line() + theme_bw() 
output$graph3 <- renderPlot({grid.arrange(plot1, plot2, ncol=1)}) 

} 
+0

尽管重复,这工作。 –

0

如果您input$predictor正是符合您linegraphdata data.frame的列名,你也可以使用aes_string从ggplot(使用字符串或引用名称/调用定义审美映射):

plot2 <- ggplot(data = linegraphdata, aes_string(x = "Year", y = input$predictor)) + geom_line() + theme_bw() 

这样您就不需要具有多个if语句的反应函数。

+0

当我运行它,我得到以下错误: 警告:错误aes_string:对象 '年' 未找到 堆栈跟踪(最前): 46:aes_string 45:继承 44:ggplot.data.frame 43:ggplot 42:server [C:/ Users/Renel/Documents/Fake Desktop/App/sampleapp.R#49] 1:runApp aes_string(x = Year,y = datainput $ predictor)中的错误: 找不到对象“年” –

+1

您需要在“年份”中大约年份的报价 - 您有这个吗? –

+0

是的,这工作!谢谢!但是,你仍然需要反应函数。 –