HertieDataScience / SyllabusAndLectures

Hertie School of Governance Introduction to Collaborative Social Science Data Analysis
MIT License
37 stars 60 forks source link

Showing rCharts output in R? #26

Open oliverbott opened 9 years ago

oliverbott commented 9 years ago

Hi everyone, we are having problems with visualizing our rChart data output in R. Our code can be found (and run) on https://github.com/oliverbott/Final_Project/blob/master/Data/Follower_Selection.R. The nPlot code is taken from http://ramnathv.github.io/rCharts/, which has some amazing visualizations.

How can I view the actual output? What am I doing wrong?

Thanks a lot for your help!

oliverbott commented 9 years ago

I believe I can just type in n1 which is the name of the figure. The result is blank which probably means an error in the code...

christophergandrud commented 9 years ago

Take a look at this:

http://rpubs.com/conniez/ufo_rchart

I think you might want to do something like

n1$show('iframesrc', cdn = TRUE)
oliverbott commented 9 years ago

Thanks I´ll look into this

oliverbott commented 9 years ago

It seems that for interactive rCharts to show up in the output, it is necessary to simply add this code chunk (no changes needed) to the .Rmd file:

## load the package
library(rCharts)

## utility function to add required assets such as CSS and JS libraries
add_lib_assets <- function(lib, cdn = F, css = NULL) {
    assets = get_assets(get_lib(lib), cdn = cdn)
    if (!is.null(css)) {
        assets$css = c(assets$css, css)
    }
    styles <- lapply(assets$css, function(style) {
        sprintf("<link rel='stylesheet' href=%s>", style)
    })

    scripts <- lapply(assets$jshead, function(script) {
        sprintf("<script type='text/javascript' src=%s></script>", script)
    })
    cat(paste(c(styles, scripts), collapse = "\n"))
}

# get assets from online repositories
add_lib_assets("NVD3", cdn = TRUE, css = "http://rawgithub.com/ramnathv/rCharts/master/inst/libraries/nvd3/css/rNVD3.css")

I found this hint on http://rstudio-pubs-static.s3.amazonaws.com/9331_9b5778029d1e46d0b3afcf1d861d7e70.html.

It works for me, hope this helps others too!

christophergandrud commented 9 years ago

That's really helpful. Thanks for posting.

christophergandrud commented 9 years ago

See also: https://github.com/HertieDataScience2014/SyllabusAndLectures/issues/28