Skip to content

Instantly share code, notes, and snippets.

@ramnathv
Created May 7, 2013 10:40
Show Gist options
  • Save ramnathv/5531732 to your computer and use it in GitHub Desktop.
Save ramnathv/5531732 to your computer and use it in GitHub Desktop.
rCharts NVD3 Shiny App
require(rCharts)
shinyServer(function(input, output) {
output$myChart <- renderChart({
hair_eye = as.data.frame(HairEyeColor)
p6 <- nPlot(Freq ~ Hair, group = 'Eye', data = subset(hair_eye, Sex == input$gender),
type = input$type, dom = 'myChart', width = 800)
p6$chart(color = c('brown', 'blue', '#594c26', 'green'), stacked = input$stack)
return(p6)
})
})
require(rCharts)
shinyUI(pageWithSidebar(
headerPanel("rCharts: Interactive Charts from R | NVD3"),
sidebarPanel(
selectInput(inputId = "gender",
label = "Choose Gender",
choices = c("Male", "Female"),
selected = "Male"),
selectInput(inputId = "type",
label = "Choose Chart Type",
choices = c("multiBarChart", "multiBarHorizontalChart"),
selected = "multiBarChart"),
checkboxInput(inputId = "stack",
label = strong("Stack Bars?"),
value = FALSE)
),
mainPanel(
showOutput("myChart", "nvd3")
)
))
@anujku
Copy link

anujku commented Oct 17, 2014

Wish I could put this code along side my tomcat web app but cant :(

@zahrahnnx
Copy link

I don't understand where you get the data? How can I use my own data (eg,csv or excel file)?
May you help me? I'm new in this area. Regards

@stephenfranklin
Copy link

I'm having trouble implementing a slightly different example where the nplot is a scatterplot that switches between two variables. The plot doesn't appear even if I remove the switch by replacing datasetInput() with Murder in server.R.

Any ideas on how I can make this work?

server.R

require(rCharts)

shinyServer(function(input, output) {   
  datasetInput <- reactive({
    switch(input$crime,
           "assault" = Assault,
           "murder"  = Murder)
  })
  output$arrestPlot <- renderChart({
    w2 <- nPlot(datasetInput() ~ UrbanPop, 
                data = USArrests, 
                type = 'scatterChart') 
    w2$chart(tooltipContent =   "#! function(key, x, y, e){ 
             return e.point.row.names
  } !#")
    w2$set(title = "Water Footprint vs. Protein: California")
    w2$xAxis(axisLabel = 'Protein (%)')
    w2$yAxis(axisLabel = "Water Footprint: California (L/100g)" )
    w2$chart(showControls = F)  ## turn off magnify.
    return(w2)
})
}

ui.R

require(rCharts)

shinyUI(pageWithSidebar(
  headerPanel("rCharts: Interactive Charts from R | NVD3"),

  sidebarPanel(
    selectInput(inputId = "crime",
                label = "Crime:",
                choices = c("assault","murder"),
                selected = "assault")
  ),
  mainPanel(
    showOutput("arrestPlot", "nvd3")
  )
))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment