Luis Otavio Fernandes
Luis Otavio Fernandes

Reputation: 161

Using rCharts with Shiny

I am developing a Shiny application and I'm having trouble when integrating with rCharts. I made a simple app with a database "Cars93" that everyone can access to exemplify my problem.

I put a table and a graph in the app, the table is only to test if the data is correct.

My problem is that the graph does not appear.

Thanks in advance!

Luis

ui.R

library(shiny)
library(MASS)
require(rCharts)
options(RCHART_LIB = 'polycharts')
mydata<-Cars93

shinyUI(pageWithSidebar(
  headerPanel('RCharts - Test'),
  sidebarPanel(

    selectInput("Manufacturer2", "Select a Manufacturer", levels(droplevels(mydata$Manufacturer)),
                          selected = "Acura"
    ),
    selectInput("Type2", "Select a Type", levels(droplevels(mydata$Type))[3:4],selected = levels(droplevels(mydata$Type))[3]
    ),




    width = 3

  ),
  mainPanel(
    tabsetPanel(

      tabPanel("Table",dataTableOutput('mytable')),
      tabPanel("Chart",showOutput('myplot', 'polycharts'))




    )
  ))
)

server.R

library(shiny)
library(rCharts)
library(MASS)
options(RCHART_WIDTH = 800)
mydata<-Cars93
data2<-Cars93

shinyServer(function(input, output, session) {

  observe({
    updateSelectInput(session, "Type2", choices = levels(droplevels(mydata$Type[mydata$Manufacturer %in% input$Manufacturer2])),selected = levels(droplevels(mydata$Type[mydata$Manufacturer %in% input$Manufacturer2]))[1]
    )
  })

  output$mytable = renderDataTable({
    MANUFACTURER = input$Manufacturer2
    TYPE = input$Type2
    mydata<- mydata[mydata$Manufacturer %in% MANUFACTURER  & mydata$Type %in% TYPE,]
    mydata<-mydata[,c("Manufacturer","Type","Model","Weight")]
    mydata
  })

  output$myplot<- renderChart({
    MANUFACTURER = input$Manufacturer2
    TYPE = input$Type2
    mydata<- mydata[mydata$Manufacturer %in% MANUFACTURER  & mydata$Type %in% TYPE,]
    p1<-rPlot(Weight ~ Model, color = 'Model', data = mydata, type = 'bar')

    p1$guides(
      color = list(
        numticks = length(factor(mydata$Model))
      ),
      y = list(
        min = 0,
        max = 10
      )
    )
    return(p1)
  })

})

Upvotes: 1

Views: 337

Answers (1)

jdharrison
jdharrison

Reputation: 30445

If you use renderChart in your server function you need to set the dom:

p1$set(dom = "myplot")

where dom should take the same value that output is assigned from renderChart

Alternatively there is a handy renderChart2 that can be used instead of renderChart and then the dom doesnt need to be set.

Upvotes: 2

Related Questions