Search code examples
rshinyrcharts

How to create an empty plot in rCharts?


I'm trying to add an rCharts output to a shiny app. If the inputs are not valid, then I don't want to output anything - so I'd like to be able to just return NULL or somehow have an "empty plot" in the output. But I can't figure out how to create an empty plot in rCharts.

Here's the code I have, the example is taken straight out of the README but I commented out the plot code and returned NULL instead

library(shiny)
library(rCharts)

ui <- pageWithSidebar(
  headerPanel("rCharts: Interactive Charts from R using polychart.js"),

  sidebarPanel(
    selectInput(inputId = "x",
                label = "Choose X",
                choices = c('SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'),
                selected = "SepalLength"),
    selectInput(inputId = "y",
                label = "Choose Y",
                choices = c('SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'),
                selected = "SepalWidth")
  ),
  mainPanel(
    showOutput("myChart", "polycharts")
  )
)

server<- function(input, output) {
  output$myChart <- renderChart({
    # names(iris) = gsub("\\.", "", names(iris))
    # p1 <- rPlot(input$x, input$y, data = iris, color = "Species", 
    #             facet = "Species", type = 'point')
    # p1$addParams(dom = 'myChart')
    # return(p1)
    return(NULL)
  })
}

shinyApp(ui, server)

Solution

  • showOutput expects an object back right, so why do you give back an empty object of that class.

    output$myChart <- renderChart({
            mychart <- Polycharts$new()
            return(mychart)
    })