celestialgod

shiny rCharts test 2

Jul 26th, 2015
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.78 KB | None | 0 0
  1. library(shiny)
  2. library(rCharts)
  3. options(RCHART_WIDTH = 600)
  4. names(iris) = gsub('\\.', '', names(iris))
  5. app = shinyApp(
  6.   ui = shinyUI(pageWithSidebar(
  7.     headerPanel("iris data for bootstrap rcharts layout test"),
  8.     sidebarPanel(
  9.       selectInput("sp",label="choose iris species",
  10.         choices=c("all","setosa","versicolor","virginica"))
  11.     ),
  12.     mainPanel(tabsetPanel(title="",
  13.        tabPanel("Data Table",dataTableOutput("table")),
  14.        tabPanel("Scatter Plots",
  15.          showOutput("scatter1", "highcharts"),
  16.          showOutput("scatter2", "polycharts"),
  17.          showOutput("scatter3", "nvd3"))
  18.     )))
  19.   ), server =  shinyServer(function(input, output) {
  20.   vals = reactiveValues()
  21.   vals$df = iris
  22.   irisSpecies = observe({
  23.     input$sp
  24.     isolate({
  25.     if (input$sp != "all")
  26.       vals$df = subset(iris, iris$Species==input$sp)
  27.     else
  28.       vals$df = iris
  29.     })
  30.   })
  31.   output$table <- renderDataTable({vals$df})
  32.   output$scatter1 = renderChart({
  33.     s1 <- hPlot(x = "PetalLength", y = "PetalWidth", data = vals$df, type = "scatter")
  34.     s1$addParams(height = 400, dom = 'scatter1')
  35.     return(s1)
  36.   })
  37.   output$scatter2 = renderChart({
  38.     s2 <- rPlot(PetalWidth ~ PetalLength, data = vals$df, type = "point")
  39.     xlim = range(vals$df$PetalLength)
  40.     ylim = range(vals$df$PetalWidth)
  41.     xlim = xlim + diff(xlim)/10 * c(-1,1)
  42.     ylim = ylim + diff(ylim)/10 * c(-1,1)
  43.     s2$addParams(height = 400, dom = 'scatter2')
  44.     s2$guides(x = list(min = xlim[1], max = xlim[2]),
  45.         y = list(min = ylim[1], max = ylim[2]))
  46.     return(s2)
  47.   })
  48.   output$scatter3 = renderChart({
  49.     s3 <- nPlot(PetalWidth ~ PetalLength, data = vals$df, type = "scatterChart")
  50.     s3$addParams(height = 400, dom = 'scatter3')
  51.     return(s3)
  52.   })
  53. }))
  54. runApp(app)
Advertisement
Add Comment
Please, Sign In to add comment