3

Using the code below

library(shiny)
library(rCharts)    
hair_eye_male <- subset(as.data.frame(HairEyeColor), Sex == "Male")
n1 <- nPlot(Freq ~ Hair, 
            group = "Eye", 
            data = hair_eye_male, 
            type = 'multiBarChart')
n1

I got this plot in RStudio

enter image description here

I want to create a shiny app for this plot. I created the app using the code below

library(shiny)
library(rCharts)
ui <- fluidPage(
  mainPanel(uiOutput("tb")))
server <- function(input,output){

  output$hair_eye_male <- renderChart({

    hair_eye_male <- subset(as.data.frame(HairEyeColor), Sex == "Male")
    n1 <- nPlot(Freq ~ Hair, group = "Eye", data = hair_eye_male, 
                type = 'multiBarChart')
    return(n1) 
  })
  output$tb <- renderUI({
    tabsetPanel(tabPanel("First plot", 
                         showOutput("hair_eye_male")))
  })
}
shinyApp(ui = ui, server = server)

However, shiny app didn't render the plot. It appeared empty. Any suggestions would be appreciated.

shiny
  • 3,380
  • 9
  • 42
  • 79

1 Answers1

1

When using rCharts package with r shiny you need to specify which javascript library you are using.

By adding line n1$addParams(dom = 'hair_eye_male') to renderChart() and specifying which library you are using in showOutput(), code will work.

library(shiny)
library(ggplot2)
library(rCharts)

ui <- fluidPage(
  mainPanel(uiOutput("tb")))
server <- function(input,output){

  output$hair_eye_male <- renderChart({

    hair_eye_male <- subset(as.data.frame(HairEyeColor), Sex == "Male")
    n1 <- nPlot(Freq ~ Hair, group = "Eye", data = hair_eye_male, 
                type = 'multiBarChart')
    n1$addParams(dom = 'hair_eye_male') 
    return(n1) 
  })
  output$tb <- renderUI({
    tabsetPanel(tabPanel("First plot", 
                         showOutput("hair_eye_male", lib ="nvd3")))
  })
}
shinyApp(ui = ui, server = server)
Mikael Jumppanen
  • 2,436
  • 1
  • 17
  • 29