Ignacio
Ignacio

Reputation: 7928

R shiny app with rCharts

I'm able to create this graph with rCharts:

library(rCharts)
X <- structure(list(Var1 = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
                                       8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 
                                       3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("1", "2", "3", "4", 
                                                                                    "5", "6", "7", "8", "9", "10"), class = "factor"), Var2 = structure(c(1L, 
                                                                                                                                                          1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                                                                                                                          2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("control", 
                                                                                                                                                                                                                          "treatment1", "treatment2"), class = "factor"), Freq = c(0L, 
                                                                                                                                                                                                                                                                                   0L, 3L, 2L, 6L, 9L, 13L, 36L, 50L, 497L, 0L, 2L, 1L, 3L, 6L, 
                                                                                                                                                                                                                                                                                   4L, 11L, 29L, 50L, 499L, 1L, 2L, 0L, 2L, 5L, 6L, 12L, 22L, 63L, 
                                                                                                                                                                                                                                                                                   490L)), .Names = c("Var1", "Var2", "Freq"), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                             -30L), class = "data.frame")
n1<-nPlot(Freq ~ Var1, group = 'Var2', data = X, type = 'multiBarChart')
print(n1)

Now I'm trying to embeded in a Shiny app. I can do a shiny app with ggplot2, but I'm not sure how to print the rCharts graph.

This is the shiny code that I have right now:

#server.R
library(rCharts)
X <- structure(list(Var1 = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
                                       8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 
                                       3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("1", "2", "3", "4", 
                                                                                    "5", "6", "7", "8", "9", "10"), class = "factor"), Var2 = structure(c(1L, 
                                                                                                                                                          1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                                                                                                                          2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("control", 
                                                                                                                                                                                                                          "treatment1", "treatment2"), class = "factor"), Freq = c(0L, 
                                                                                                                                                                                                                                                                                   0L, 3L, 2L, 6L, 9L, 13L, 36L, 50L, 497L, 0L, 2L, 1L, 3L, 6L, 
                                                                                                                                                                                                                                                                                   4L, 11L, 29L, 50L, 499L, 1L, 2L, 0L, 2L, 5L, 6L, 12L, 22L, 63L, 
                                                                                                                                                                                                                                                                                   490L)), .Names = c("Var1", "Var2", "Freq"), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                             -30L), class = "data.frame")

shinyServer(
  function(input, output) {
    output$histogram <- renderPlot({
      # You can access the value of the widget with input$select, e.g.
      output$value <- renderPrint({ input$select })
      n2 <- nPlot(Freq ~ Var1, group = 'Var2', data = X, type = 'multiBarChart')
      n2$set(dom = "histogram")
      return(n2)
      })
  }
)

#ui.R
shinyUI(fluidPage(
  titlePanel("Quiz 3 grades distribution"),

  sidebarLayout(
    sidebarPanel(
      helpText("Quiz 3 grade distribution by treatment group"),    
      selectInput("select", label = h3("Select box"), 
                  choices = list("All" = 0, "Not Perfect" = 1, "Perfect" = 2), 
                  selected = 0)
    ),

    mainPanel(plotOutput("histogram"))
  )
))

What am I doing wrong? Thanks!

Upvotes: 1

Views: 992

Answers (1)

jdharrison
jdharrison

Reputation: 30425

Use renderChart2 and showOutput to display nvd3 plots in shiny. Using renderChart2 doesn't require the using $set(dom = ....

library(rCharts)
library(shiny)
X <- data.frame(Var1 = c(1L, 2L, 3L, 4L, 5L, 6L, 7L,8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L,3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L),
                Var2 = structure(c(1L,1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("control","treatment1", "treatment2"), class = "factor"),
                Freq = c(0L,0L, 3L, 2L, 6L, 9L, 13L, 36L, 50L, 497L, 0L, 2L, 1L, 3L, 6L, 4L, 11L, 29L, 50L, 499L, 1L, 2L, 0L, 2L, 5L, 6L, 12L, 22L, 63L,490L)
)
runApp(
  list(ui = fluidPage(
    titlePanel("Quiz 3 grades distribution"),

    sidebarLayout(
      sidebarPanel(
        helpText("Quiz 3 grade distribution by treatment group"),    
        selectInput("select", label = h3("Select box"), 
                    choices = list("All" = 0, "Not Perfect" = 1, "Perfect" = 2), 
                    selected = 0)
      ),

      mainPanel(
        showOutput("histogram","Nvd3")
      )
      )
  ),
  server = shinyServer(
    function(input, output, session) {
      output$histogram <- renderChart2({
        n2 <- nPlot(Freq ~ Var1, group = 'Var2', data = X, type = 'multiBarChart')
        n2
      })
    }
  )

  )
)

nPlot in shiny

Upvotes: 3

Related Questions