Permalink
Please sign in to comment.
Showing
with
593 additions
and 0 deletions.
- +2 −0 .Rbuildignore
- +5 −0 .gitignore
- +10 −0 DESCRIPTION
- +4 −0 NAMESPACE
- +26 −0 R/main.R
- +81 −0 R/rCharts.R
- +21 −0 R/renderChart.R
- +15 −0 R/showOutput.R
- +19 −0 R/utils.R
- +74 −0 README.md
- +10 −0 inst/app/server.R
- +18 −0 inst/app/ui.R
- +32 −0 inst/polycharts/examples.R
- +248 −0 inst/polycharts/js/polychart2.standalone.js
- +5 −0 inst/polycharts/layouts/polychart1.html
- +13 −0 inst/polycharts/layouts/polychart2.html
- +6 −0 inst/shiny/server.R
- +4 −0 inst/shiny/ui.R
| @@ -0,0 +1,2 @@ | ||
| +^.*\.Rproj$ | ||
| +^\.Rproj\.user$ |
| @@ -0,0 +1,5 @@ | ||
| +.Rproj.user | ||
| +.Rhistory | ||
| +.RData | ||
| +.DS_Store | ||
| +*.Rproj |
10
DESCRIPTION
| @@ -0,0 +1,10 @@ | ||
| +Package: rCharts | ||
| +Type: Package | ||
| +Title: Interactive Charts using Polycharts.js | ||
| +Version: 0.0.1 | ||
| +Date: 2013-04-09 | ||
| +Author: Ramnath Vaidyanathan | ||
| +Maintainer: Ramnath Vaidyanathan <ramnath.vaidya@gmail.com> | ||
| +Description: Create interactive visualizations with a plotting interface familiar to R users | ||
| + using the Polycharts javascript library | ||
| +License: MIT |
| @@ -0,0 +1,4 @@ | ||
| +exportPattern("^[[:alpha:]]+") | ||
| +import(rjson) | ||
| +import(plyr) | ||
| +import(whisker) |
26
R/main.R
| @@ -0,0 +1,26 @@ | ||
| +#' Main plotting function | ||
| +#' | ||
| +#' @examples | ||
| +#' names(iris) = gsub('\\.', '', names(iris)) | ||
| +#' rPlot(SepalLength ~ SepalWidth | Species, data = iris, type = 'point', color = 'Species') | ||
| +#' | ||
| +#' | ||
| + | ||
| +rPlot <- function(x, ...){ | ||
| + UseMethod('rPlot') | ||
| +} | ||
| + | ||
| +rPlot.default <- function(x, y, data, ..., width = 800, height = 400){ | ||
| + myChart <- PolyChart$new() | ||
| + myChart$addParams(width = width, height = height) | ||
| + myChart$layer2(x = x, y = y, data = data, ...) | ||
| + return(myChart$copy()) | ||
| +} | ||
| + | ||
| +rPlot.formula <- function(x, data, ..., width = 800, height = 400){ | ||
| + myChart <- PolyChart$new() | ||
| + myChart$addParams(width = width, height = height) | ||
| + myChart$layer(x, data, ...) | ||
| + return(myChart$copy()) | ||
| +} | ||
| + |
81
R/rCharts.R
| @@ -0,0 +1,81 @@ | ||
| +PolyChart = setRefClass('PolyChart', list(params = 'list'), methods = list( | ||
| + initialize = function(){ | ||
| + params <<- list(dom = basename(tempfile('chart')), width = 700, height = 300) | ||
| + params$layers <<- list() | ||
| + params$facet <<- list() | ||
| + params$guides <<- list() | ||
| + }, | ||
| + addParams = function(...){ | ||
| + params <<- modifyList(params, list(...)) | ||
| + }, | ||
| + layer = function(x, data, ...){ | ||
| + len = length(params$layers) | ||
| + fml = lattice::latticeParseFormula(x, data = data) | ||
| + params$layers[[len + 1]] <<- list(x = fml$right.name, y = fml$left.name, | ||
| + data = data, ...) | ||
| + if (!is.null(fml$condition)){ | ||
| + params$facet <<- c(params$facet, type = 'wrap', var = names(fml$condition)) | ||
| + # len = length(params$facet) | ||
| + # params$facet[[len + 1]] <<- list(type = 'type', var = facet) | ||
| + } | ||
| + }, | ||
| + layer2 = function(x, y, data, facet = NULL, ...){ | ||
| + len = length(params$layers) | ||
| + params$layers[[len + 1]] <<- list(x = x, y = y, data = data, ...) | ||
| + if (!is.null(facet)){ | ||
| + params$facet <<- c(params$facet, type = 'wrap', var = facet) | ||
| + # len = length(params$facet) | ||
| + # params$facet[[len + 1]] <<- list(type = 'type', var = facet) | ||
| + } | ||
| + }, | ||
| + facet = function(...){ | ||
| + len = length(params$facets) | ||
| + params$facet[[len + 1]] <<- list(...) | ||
| + }, | ||
| + guides = function(...){ | ||
| + params$guides <<- modifyList(params$guides, addGuide(...)) | ||
| + }, | ||
| + html = function(chartId = NULL){ | ||
| + template_file = system.file('polycharts', 'layouts', 'polychart1.html', | ||
| + package = 'rCharts') | ||
| + template = paste(readLines(template_file, warn = F), collapse = '\n') | ||
| + if (is.null(chartId)){ | ||
| + chartId <- params$dom | ||
| + } else { | ||
| + params$dom <<- chartId | ||
| + } | ||
| + chartParams = toJSON(params) | ||
| + html = paste(capture.output(cat(whisker.render(template))), collapse = '\n') | ||
| + return(html) | ||
| + }, | ||
| + printChart = function(chartId = NULL){ | ||
| + writeLines(.self$html(chartId)) | ||
| + }, | ||
| + render = function(chartId = NULL){ | ||
| + if (is.null(chartId)){ | ||
| + chartId <- params$dom | ||
| + } else { | ||
| + params$dom <<- chartId | ||
| + } | ||
| + template_file = system.file('polycharts', 'layouts', 'polychart2.html', | ||
| + package = 'rCharts') | ||
| + template = paste(readLines(template_file, warn = F), collapse = '\n') | ||
| + partials = list(polychart1 = .self$html(chartId)) | ||
| + html = capture.output(cat(whisker.render(template, partials = partials))) | ||
| + }, | ||
| + save = function(destfile = 'index.html'){ | ||
| + writeLines(.self$render(), destfile) | ||
| + }, | ||
| + show = function(static = !("shiny" %in% rownames(installed.packages()))){ | ||
| + if (static){ | ||
| + tf <- tempfile(fileext = 'html'); | ||
| + writeLines(.self$render(), tf) | ||
| + system(sprintf("open %s", tf)) | ||
| + } else { | ||
| + shiny_copy = .self$copy() | ||
| + shiny_copy$params$dom = 'show' | ||
| + assign(".rChart_object", shiny_copy, envir = .GlobalEnv) | ||
| + shiny::runApp(file.path(system.file(package = "rCharts"), "shiny")) | ||
| + } | ||
| + } | ||
| +)) |
| @@ -0,0 +1,21 @@ | ||
| +#' renderChart (use with Shiny) | ||
| +#' | ||
| +#' Use rNVD3 charts as Shiny output. First, use \code{renderChart} in | ||
| +#' \code{server.R} to assign the chart object to an Shiny output. Then create an | ||
| +#' chartOutput with the same name in \code{ui.R}. \code{chartOutput} is | ||
| +#' currently just an alias for \code{htmlOutput}. | ||
| +#' | ||
| +#' @param expr An expression that returns a chart object | ||
| +#' @param env The environment in which to evaluate \code{expr}. | ||
| +#' @param quoted Is expr a quoted expression (with \code{quote()})? This is | ||
| +#' useful if you want to save an expression in a variable. | ||
| +#' | ||
| +#' @export | ||
| +renderChart <- function(expr, env = parent.frame(), quoted = FALSE) { | ||
| + func <- shiny::exprToFunction(expr, env, quoted) | ||
| + | ||
| + function() { | ||
| + rChart_ <- func() | ||
| + rChart_$html() | ||
| + } | ||
| +} |
| @@ -0,0 +1,15 @@ | ||
| +showOutput <- function(outputId) { | ||
| + # Add javascript resources | ||
| + suppressMessages(singleton(addResourcePath("polycharts", | ||
| + system.file('polycharts', package='rCharts')))) | ||
| + | ||
| + div(class="rChart", | ||
| + # Add Javascripts | ||
| + tagList( | ||
| + singleton(tags$head(tags$script(src = "polycharts/js/polychart2.standalone.js", | ||
| + type='text/javascript'))) | ||
| + ), | ||
| + # Add chart html | ||
| + htmlOutput(outputId) | ||
| + ) | ||
| +} |
19
R/utils.R
| @@ -0,0 +1,19 @@ | ||
| +addGuide <- function(...){ | ||
| + UseMethod('addGuides') | ||
| +} | ||
| + | ||
| +addGuides.default <- function(...){ | ||
| + list(...) | ||
| +} | ||
| + | ||
| +addGuides.character <- function(...){ | ||
| + yaml::yaml.load(...) | ||
| +} | ||
| + | ||
| +addLayer <- function(x, ...){ | ||
| + UseMethod('addLayer') | ||
| +} | ||
| + | ||
| +addLayer.default <- function(...){ | ||
| + | ||
| +} |
74
README.md
| @@ -0,0 +1,74 @@ | ||
| +## rCharts | ||
| + | ||
| +This R package provides a familiar plotting interface for R users to create interactive visualizations using [polychart.js](https://github.com/Polychart/polychart2). | ||
| + | ||
| +### Installation | ||
| + | ||
| +You can install `rCharts` from `github` using the `devtools` package | ||
| + | ||
| +``` | ||
| +require(devtools) | ||
| +install_github('rCharts', 'ramnathv') | ||
| +``` | ||
| + | ||
| +### Usage | ||
| + | ||
| +`rCharts` uses a formula interface to specify plots, just like the `lattice` package. Here are a few examples you can try out in your R console. | ||
| + | ||
| +``` | ||
| +## Example 1 Facetted Scatterplot | ||
| +names(iris) = gsub("\\.", "", names(iris)) | ||
| +rPlot(SepalLength ~ SepalWidth | Species, data = iris, color = 'Species', type = 'point') | ||
| + | ||
| +## Example 2 Facetted Barplot | ||
| +hair_eye = as.data.frame(HairEyeColor) | ||
| +rPlot(Freq ~ Hair | Eye, color = 'Eye', data = hair_eye, type = 'bar') | ||
| +``` | ||
| + | ||
| +`rCharts` is also compatible with [Slidify](http://slidify.org). | ||
| + | ||
| +More documentation is underway. | ||
| + | ||
| +### Using with Shiny | ||
| + | ||
| +``` | ||
| +## server.r | ||
| +require(rCharts) | ||
| +shinyServer(function(input, output) { | ||
| + output$myChart <- renderChart({ | ||
| + names(iris) = gsub("\\.", "", names(iris)) | ||
| + p1 <- rPlot(input$x, input$y, data = iris, color = "Species", | ||
| + facet = "Species", type = 'point') | ||
| + p1$addParams(dom = 'myChart') | ||
| + return(p1) | ||
| + }) | ||
| +}) | ||
| + | ||
| +## ui.R | ||
| +require(rCharts) | ||
| +shinyUI(pageWithSidebar( | ||
| + headerPanel("rCharts: Interactive Charts from R using polychart.js"), | ||
| + | ||
| + sidebarPanel( | ||
| + selectInput(inputId = "x", | ||
| + label = "Choose X", | ||
| + choices = c('SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'), | ||
| + selected = "SepalLength"), | ||
| + selectInput(inputId = "y", | ||
| + label = "Choose Y", | ||
| + choices = c('SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'), | ||
| + selected = "SepalWidth") | ||
| + ), | ||
| + mainPanel( | ||
| + showOutput("myChart") | ||
| + ) | ||
| +)) | ||
| +``` | ||
| + | ||
| +### Credits | ||
| + | ||
| +Most of the implementation in `rCharts` is inspired by [rHighcharts](https://github.com/metagraf/rHighcharts) and [rVega](https://github.com/metagraf/rVega). I have reused some code from these packages verbatim, and would like to acknowledge the efforts of its author [Thomas Reinholdsson](https://github.com/reinholdsson). | ||
| + | ||
| +### License | ||
| + | ||
| +`rCharts` is licensed under the MIT License. However, the Polycharts JavaScript library that is included in this package is not free for commercial use, and is licensed under Creative Commons 3.0 Attribution & Non-commercial. Read more about its license at http://polychart.com/js/license. |
| @@ -0,0 +1,10 @@ | ||
| +require(rCharts) | ||
| +shinyServer(function(input, output) { | ||
| + output$myChart <- renderChart({ | ||
| + names(iris) = gsub("\\.", "", names(iris)) | ||
| + p1 <- rPlot(input$x, input$y, data = iris, color = "Species", | ||
| + facet = "Species", type = 'point') | ||
| + p1$addParams(dom = 'myChart') | ||
| + return(p1) | ||
| + }) | ||
| +}) |
| @@ -0,0 +1,18 @@ | ||
| +require(rCharts) | ||
| +shinyUI(pageWithSidebar( | ||
| + headerPanel("rCharts: Interactive Charts from R using polychart.js"), | ||
| + | ||
| + sidebarPanel( | ||
| + selectInput(inputId = "x", | ||
| + label = "Choose X", | ||
| + choices = c('SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'), | ||
| + selected = "SepalLength"), | ||
| + selectInput(inputId = "y", | ||
| + label = "Choose Y", | ||
| + choices = c('SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'), | ||
| + selected = "SepalWidth") | ||
| + ), | ||
| + mainPanel( | ||
| + showOutput("myChart") | ||
| + ) | ||
| +)) |
| @@ -0,0 +1,32 @@ | ||
| +## Example 1 Facetted Scatterplot | ||
| +names(iris) = gsub("\\.", "", names(iris)) | ||
| +rPlot(SepalLength ~ SepalWidth | Species, data = iris, color = 'Species', type = 'point') | ||
| + | ||
| + | ||
| +## Example 2 Facetted Barplot | ||
| +hair_eye = as.data.frame(HairEyeColor) | ||
| +rPlot(Freq ~ Hair | Eye, color = 'Eye', data = hair_eye, type = 'bar') | ||
| + | ||
| +## Example 3 Boxplot | ||
| +data(tips, package = 'reshape2') | ||
| +rPlot(x = 'day', y = 'box(tip)', data = tips, type = 'box') | ||
| + | ||
| +## Example 4 | ||
| + | ||
| +dat = count(mtcars, .(gear, am)) | ||
| +rPlot(x = 'bin(gear, 1)', y = 'freq', data = dat, type = 'bar', facet = 'am') | ||
| + | ||
| +## Example 5 (Heat Map) | ||
| + | ||
| +dat = expand.grid(x = 1:5, y = 1:5) | ||
| +dat = transform(dat, value = sample(1:5, 25, replace = T)) | ||
| +rPlot(x = 'bin(x, 1)', y = 'bin(y, 1)', color = 'value', data = dat, type = 'tile') | ||
| + | ||
| + | ||
| +# Example 6 (NBA Heat Map) | ||
| +nba <- read.csv('http://datasets.flowingdata.com/ppg2008.csv') | ||
| +nba.m <- ddply(melt(nba), .(variable), transform, rescale = rescale(value)) | ||
| +p1 <- rPlot(Name ~ variable, color = 'rescale', data = nba.m, type = 'tile', height = 600) | ||
| +p1$guides("{color: {scale: {type: gradient, lower: white, upper: steelblue}}}") | ||
| +p1 | ||
| + |
Oops, something went wrong.
0 comments on commit
96afb20