I was finally able to do an example of a Shiny App using the rCharts scatter plot. Switching this example from a standard R scatter plot to rCharts took me way longer than I expected. Now that I have finished it, I am not exactly sure why it was so challenging because the final code looks pretty simple and clean. I also changed my code from the original app so that instead of two R code files (ui.R and server.R) there is only one (app.R). Even that took a little while because I messed up the commas. For this example I used the mtcars data set and allow the user to select the Y variable, X variable, the color variable and a variable to make multiple plots.
Here is a link to the shiny app with embedded rChart
Also here is the code to create this shiny App:
require(shiny)
require(rCharts)
require(datasets)
server<-function(input,output){
output$myChart<-renderChart({
p1<-rPlot(input$x,input$y, data=mtcars,type="point",color=input$color,facet=input$facet)
p1$addParams(dom="myChart")
return(p1)
})
}
ui<-pageWithSidebar(
headerPanel("Motor Trend Cars data with rCharts"),
sidebarPanel(
selectInput(inputId="y",
label="Y Variable",
choices=names(mtcars),
),
selectInput(inputId="x",
label="X Variable",
choices=names(mtcars),
),
selectInput(inputId="color",
label="Color by Variable",
choices=names(mtcars[,c(2,8,9,10,11)]),
),
selectInput(inputId="facet",
label="Facet by Variable",
choices=names(mtcars[,c(2,8,9,10,11)]),
)
),
mainPanel(
showOutput("myChart","polycharts")
)
)
shinyApp(ui=ui,server=server)
Hopefully you can use my code as a template to speed the time it takes you to learn how to do this.
I blog about world of Data Science with Visualization, Big Data, Analytics, Sabermetrics, Predictive HealthCare, Quant Finance, and Marketing Analytics using the R language.
Subscribe to:
Post Comments (Atom)
I was failing hard until I saw your post. I was unable to show the chart until I added "p1$addParams(dom="myChart")"...
ReplyDeleteThank you!