Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rCharts: plot is working fine in RStudio but empty in shiny app

Tags:

r

shiny

rcharts

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.

like image 794
shiny Avatar asked Jan 27 '26 15:01

shiny


1 Answers

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)
like image 198
Mikael Jumppanen Avatar answered Jan 30 '26 06:01

Mikael Jumppanen