Search code examples
rshinyrickshawrcharts

R: rCharts and Shiny: Rickshaw plot won't show


After having a lot of fun getting the basic of shiny down using ggplot2, I'm trying out rCharts. However, I can't get the Rickshaw graph to display. Any help much appreciated; take it easy - I'm just getting use to this ;)

### ui

library(shiny)
require(devtools)
install_github('rCharts', 'ramnathv')
# moved from lower down so call to `showOutput` would not fail
library(rCharts) 
library(rattle)

shinyUI(

  pageWithSidebar(

  headerPanel("Rickshaw test"),

    sidebarPanel(

      selectInput("variable", 
                  "Choice is arbitrary:",
                  c("Choice 1", "Choice 2")
                  )
      ),  

  mainPanel(

    showOutput("plot", "Rickshaw")
    )
  )
)

### server


data(weather)
w = weather

dateToSeconds = function (date) {

  date = as.POSIXct(as.Date(date), origin = "1970-01-01")
  as.numeric(date)
}

w$Date = dateToSeconds(w$Date)

shinyServer(function(input, output) {

  output$mpgPlot = renderChart({

    rs = Rickshaw$new()    
    rs$layer(MinTemp ~ Date,
             data = w,
             type = "line")    
    return(rs)    
  })  
})

Solution

  • The main issue is that showOutput, renderChart and the Shiny call, all need to refer to the same plot id. I modified your code based on this and it works. Here is the code for everyone's reference

    UPDATE. Please make sure that you have the latest version of rCharts installed from github.

    ## server.R
    library(shiny)
    library(rCharts) 
    library(rattle)
    data(weather)
    w = weather
    
    dateToSeconds = function (date) {
      date = as.POSIXct(as.Date(date), origin = "1970-01-01")
      as.numeric(date)
    }
    
    w$Date = dateToSeconds(w$Date)
    shinyServer(function(input, output) {
    
      output$plot = renderChart({  
        rs = Rickshaw$new()    
        rs$layer(MinTemp ~ Date, data = w, type = "line")
        rs$set(dom = "plot")
        return(rs)    
      })  
    })
    
    ## ui.R
    library(shiny)
    library(rCharts) 
    library(rattle)
    
    shinyUI(pageWithSidebar(
      headerPanel("Rickshaw test"),
      sidebarPanel(
        selectInput("variable", "Choice is arbitrary:",
          c("Choice 1", "Choice 2")
        )
      ),  
      mainPanel(    
       showOutput("plot", "Rickshaw")
      )
    ))