No output graphics in Shiny R

1.3k Views Asked by At

R users, and all programmers,

I would like to ask a it of help for my first shiny application. Since I do not have computer science background, my question is probably trivial to many users out there. But if anybody can provide some clues, that would be much appreciated.

I am trying to draw interactive graphics for average temperature in London, Paris, and Berlin. I downloaded the data from www.wunderground.com using weatherData package. I am using examples from Chris Beeley's book and Rstudio in order to design my own application.

In my server.R, I upload three data files first. Then, I have sidebar with controls to select a dataset. I also have date range in the sidebar. Once users choose a location and time range, I am asking R to do some data arrangement and pass the object, passData for the next operation. By the time, R reaches renderplot( ), I am assuming that R has a right data frame and produce a graphic using ggplot2. But, I receive the following error message.

Error in print(theGraph) : object 'theGraph' not found

This makes me think that R may not have a right data frame to generate an output graphic. I wonder if anybody can spot what I am doing wrong here. I also wonder if arranging data in reactive() is a good thing to do. Thank you very much for your attention and support.

I leave my codes here.

### ui.R

library(shiny)

shinyUI(pageWithSidebar(

    headerPanel("Europe temperature 2013"),

    sidebarPanel(

        selectInput("dataset", "Choose a location",
                    choices = c("Berlin Tigel Airport",
                                "London City Airport",
                                "Paris Charles De Gaulle")
                   ),

        dateRangeInput("daterange", "Date Range",
                       start = "2013-01-01",
                       end = "2013-12-31",
                       min = "2013-01-01",
                       max = "2013-12-31"
                      )
                ),

    mainPanel(

        plotOutput("theGraph")

    )
))

server.R

### Weather server.R


library(shiny)
library(weatherData)
library(ggplot2)
library(scales)
library(plyr)

### load weather data.
berlin <- read.csv("berlin.csv", header = T)

london <- read.csv("london.csv", header = T)

paris <- read.csv("paris.csv", header = T)




shinyServer(function(input, output){

    # Return the requested dataset

    datasetInput <- reactive({

        switch(input$dataset,
                "Berlin Tigel Airport" = berlin,
                "London City Airport" = london,
                "Paris Charles De Gaulle" = paris)
    })


    # Prepare data once and then pass around the program.

    passData <- reactive({

        foo <- datasetInput()
        foo$shortdate <- strftime(foo$Time, format = "%Y-%m-%d")
        foo$shortdate <- as.Date(foo$shortdate, format = "%Y-%m-%d")

        foo <- foo[foo$shortdate %in%
                        seq.Date(input$daterange[1],
                        input$daterange[2], by = 1), ]

        foo

    })



    output$theGraph <- renderPlot({

            graphdata <- ddply(passData(), .(shortdate), summarize, mean_C = mean(TemperatureC)) 

            if(input$dataset == "berlin"){

                theGraph <- ggplot(graphdata(), aes(shortdate, mean_C)) +
                                    geom_line() +
                                    scale_x_date(labels = date_format("%Y-%m-%d")) +
                                    xlab("") +
                                    ylab("Mean Temperature (C)") +
                                    ggtitle("2013 Average Daily Temperature in Berlin")
            }

            if(input$dataset == "london"){

                theGraph <- ggplot(graphdata(), aes(shortdate, mean_C)) +
                                    geom_line() +
                                    scale_x_date(labels = date_format("%Y-%m-%d")) +
                                    xlab("") +
                                    ylab("Mean Temperature (C)") +
                                    ggtitle("2013 Average Daily Temperature in London")
            }

            if(input$dataset == "paris"){

                theGraph <- ggplot(graphdata(), aes(shortdate, mean_C)) +
                                    geom_line() +
                                    scale_x_date(labels = date_format("%Y-%m-%d")) +
                                    xlab("") +
                                    ylab("Mean Temperature (C)") +
                                    ggtitle("2013 Average Daily Temperature in Paris")
            }

            print(theGraph)
    })
})

An example of my csv files.

### The files look like this. Three columns (Time, Temperature C, TemperatureF)

Time                 TemperatureC   TemperatureF

2013-01-01 01:00:00        6              NA

Sincerely, Kota

1

There are 1 best solutions below

2
On BEST ANSWER

If you have problems, simplify and make the example self-contained. I have reduced the example to 1 station, used a preloaded sample set to make the example self-contained, and corrected errors, e.g graphdata instead of graphdata(). Use this to restart with additional locations.

server.R

# server.R

### Weather server.R
library(shiny)
library(weatherData)
library(ggplot2)
library(scales)
library(plyr)



### load weather data.
data(Mumbai2013) # we could not reproduce your example


shinyServer(function(input, output){

  # Return the requested dataset
  datasetInput <- reactive({

    switch(input$dataset,
           "Mumbai" = Mumbai2013)
  })


  # Prepare data once and then pass around the program.

  passData <- reactive({

    foo <- datasetInput()
    foo$shortdate <- strftime(foo$Time, format = "%Y-%m-%d")
    foo$shortdate <- as.Date(foo$shortdate, format = "%Y-%m-%d")

    foo <- foo[foo$shortdate %in%
                 seq.Date(input$daterange[1],
                          input$daterange[2], by = 1), ]
    foo

  })



  output$theGraph <- renderPlot({

    graphdata <- ddply(passData(), .(shortdate), summarize, mean_C = mean(Temperature)) 
    theGraph = NULL

    theGraph <- ggplot(graphdata, aes(shortdate, mean_C)) +
        geom_line() +
        scale_x_date(labels = date_format("%Y-%m-%d")) +
        xlab("") +
        ylab("Mean Temperature (C)") +
        ggtitle("2013 Average Daily Temperature in Mumbai")
    if (!is.null(theGraph))
      print(theGraph)
  })
})

ui.R

# ui.R
library(shiny)

shinyUI(pageWithSidebar(

  headerPanel("Europe temperature 2013"),

  sidebarPanel(

    selectInput("dataset", "Choose a location",
                choices = c("Mumbai")
    ),

    dateRangeInput("daterange", "Date Range",
                   start = "2013-01-01",
                   end = "2013-12-31",
                   min = "2013-01-01",
                   max = "2013-12-31"
    )
  ),

  mainPanel(

    plotOutput("theGraph")

  )
))