0

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

www
  • 38,575
  • 12
  • 48
  • 84
jazzurro
  • 23,179
  • 35
  • 66
  • 76
  • Looks like a [weird] scoping problem to me... try initializing `theGraph` outside of the if (e.g. `theGraph <- NULL`) and see if that works. – nico Feb 13 '14 at 07:04
  • Try putting some print statements in your function and see what value gets printed in the R terminal for input$dataset. That seem the most likely cause. There have been some changes in Shiny and you might have to select on the label not the value (i.e., "Berlin Tigel Airport" rather than "berlin". Also have it print the head(graphdata) to see what it there. I tend to put dataSets in a reactiveValue(s). That way if you change something to the data, these data will be available elsewhere in your app as well. – Vincent Feb 13 '14 at 07:25
  • Hi nico and Vincent, thank you very much for your suggestions. They are very helpful to further learn programming. Thank you very much. – jazzurro Feb 18 '14 at 11:23
  • Hi Vincent, I have played with my scripts again. Adding print(graphdata) let me confirm that server.R was picking up a right data set and doing things I asked. The problem was if(input$dataset == "berlin"). This if() should be if(input$dataset == "Berlin Tegel Airport"). So what you suggested was right in this case. – jazzurro Feb 18 '14 at 14:04
  • Just a reminder. It has been a while since I posted this message. This may not be helpful by now, shiny has been changing a lot. – jazzurro Jul 14 '14 at 02:17

1 Answers1

4

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

  )
))
Dieter Menne
  • 10,076
  • 44
  • 67