embed rChart in Markdown - r

I am trying to embed a NVD3 chart in a Markdown document. I am on a Ubuntu64 system with RStudio 0.98.932, R 3.1.0, rCharts 0.4.2, the browser is Chrome.
The instructions/code from this link:
```{r}
library(rCharts)
library(knitr)
opts_chunk$set(comment = NA, results = "asis", comment = NA, tidy = F)
hair_eye_male = subset(as.data.frame(HairEyeColor), Sex == "Male")
n1 <- nPlot(Freq ~ Hair, group = 'Eye',
data = hair_eye_male, type = 'multiBarChart'
)
n1$set(width = 600)
# n1$show('iframesrc', cdn = TRUE) # option 1
# n1$show('inline', include_assets = TRUE, cdn = TRUE) # option 2
```
Neither of the options [n1$show] work, I just get code in a browser. Is there another way of including NVD3 documents in Markdown?

To get rCharts to work with knit2html, you will need to use the print method with the argument include_assets = TRUE. This is because knitr will not add the js and css assets required by an rCharts plot automatically. Here is a minimal working example.
## MorrisJS with Knit2HTML
```{r results = 'asis', comment = NA}
require(rCharts)
data(economics, package = 'ggplot2')
econ <- transform(economics, date = as.character(date))
m1 <- mPlot(x = 'date', y = c('psavert', 'uempmed'), type = 'Line',
data = econ)
m1$set(pointSize = 0, lineWidth = 1)
m1$print('chart2', include_assets = TRUE)
```
Note that you need to use m1$print('chart2', include_assets = TRUE, cdn = TRUE) if you intend to publish your chart on RPubs, for otherwise the JS and CSS assets will be served from your local library.
Source:Knitr HTML in R Markdown

This code is working for me. I am using ubuntu64 and same config you mentioned.
```{r, echo=FALSE,results='asis',comment=NA}
library(rCharts)
hair_eye_male <- subset(as.data.frame(HairEyeColor), Sex == "Male")
n1 <- nPlot(Freq ~ Hair, group = "Eye", data = hair_eye_male, type = "multiBarChart")
n1$show('iframesrc',cdn=TRUE)
```
Note : you must write results='asis' and comment = NA in chunk options and not use opts_chunk$set as you have in your code block and what you pasted to copy.com.

Related

Pixiedust table in loop in r markdown not rendering

Relevant to the problem, I have a dataset with factors of states ("Massachusetts", "California", etc) and 2 fields of values. I would like to create a graph for each state with a table below it showing the associated fields and the difference between those fields.
I found that using a loop seems to require a results = 'asis' option and a cat(" \n") at the end of the loop to print the images. That works OK. However, the only way I can seem to get a table is if I use xtable or kable. I would like to use pixiedust to color and otherwise beautify the table.
Here is a minimal example:
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(pixiedust)
library(ggplot2)
library(knitr)
library(xtable)
df <- data.frame(state = c("MA", "CA"), last_year = c(105, 90), this_year = c(110, 85))
```
# Here is the loop
```{r loops, results = 'asis', echo = FALSE}
for (i in 1:nrow(df)){
state_dat <- df[i,]
p1 <- ggplot(state_dat, aes(last_year, this_year)) +
geom_point()
print(p1)
cat(" \n")
tab <- data.frame(last_year = state_dat$last_year, this_year = state_dat$this_year, yoy_percent = 100*(state_dat$this_year - state_dat$last_year)/state_dat$last_year)
dust(tab) %>%
sprinkle(rows = 1, bg = "orchid")
cat(" \n")
print(kable(tab, row.names = FALSE, align = "c"))
cat(" \n")
print(xtable(tab, auto = TRUE),type = "html", comment = FALSE, include.rownames = F)
cat(" \n")
}
```
I also tried assigning the result of the dust commands to an object and printing that:
pixie <- dust(tab) %>%
sprinkle(rows = 1, bg = "orchid")
print(pixie)
cat(" \n")
to no avail.
Can pixiedust tables be produced as html in a chunk with option asis? Is there another workaround to produce a table and a graph in a loop?
Yes, this can be done. To get there, you have to turn off the asis printing in the print.dust method. This can be done with:
dust(tab) %>%
sprinkle(rows = 1, bg = "orchid") %>%
print(asis = FALSE) %>%
cat()
In time, I hope to come up with a better solution.

rCharts to Wordpress using knit2wp

I have been trying to publish an rChart into my blog using the knit2wp function. However, locally I can get the chart to render but when I publish it the chart becomes just text...
I have a Rmd file that goes like this:
library(rCharts)
r1 <- rPlot(mpg ~ wt, data = mtcars, type = 'point')
r1$print('inline', cdn = TRUE,include_assets = TRUE)
or
r1$print('iframesrc', cdn = TRUE)
To publish I use this:
knit2wp('test.Rmd',
title = 'test',
options(WordpressLogin = c(LOGIN = 'PASS'),
WordpressURL = 'https://dadosdadosdados.wordpress.com/xmlrpc.php'),
shortcode = c(TRUE, TRUE),
publish = F)

After upgrading: rCharts not working in rmarkdown

after I upgraded to rCharts version 0.4.5 I am not able to see a plot in my rmarkdown file anymore. I have produced a minimum example rmd where I cannot see the plots anymore (if I open the output in my browser). I have absolutely no clue which package is responsible for this. Note that it works fine if I use the $save mode and just import the created html file as in example number 2.
Do you have any idea?
I created a minimum example from a highcharts demo. This thread also helped me.
```{r echo = F, message = F, cache = F}
# Set options for plots.
library(knitr)
opts_chunk$set(results = 'asis', comment = NA, message = F, tidy = F, echo=FALSE, cache=FALSE)
```
```{r, echo=FALSE}
library(rCharts)
a <- hPlot(Pulse ~ Height, data = MASS::survey, type = "bubble", title = "Zoom demo", subtitle = "bubble chart", size = "Age", group = "Exer")
a$chart(zoomType = "x")
a$exporting(enabled = T)
a$show('iframesrc', cdn = TRUE)
```
```{r, echo=FALSE}
library(rCharts)
a <- hPlot(Pulse ~ Height, data = MASS::survey, type = "bubble", title = "Zoom demo", subtitle = "bubble chart 2", size = "Age", group = "Exer")
a$chart(zoomType = "x")
a$exporting(enabled = T)
a$save('plot2.html', standalone = TRUE)
```
<iframe src="plot2.html" height="450" width="850" frameBorder="0"></iframe>
The answer is a new "feature", that transforms links of e.g. sources starting with http://www. as //www. to make it also work with https://. On webservers, this is interpreted correctly but if you open a local html file, //www. will not be recognized. The issue is reported on the github page.

Interactive R Markdown Document with ggmap

I'm working with the ggmap tutorial by Manuel Amunategui over at http://amunategui.github.io/ggmap-example/. It is a wonderful introduction to the ggmap package and thankfully I understand his tutorial.
However, I am also trying to make this material interactive through R markdown. When I run the below document, for some reason the rendering of the map is of very low quality. In my standard .R script the image produced is way better. Any thoughts as to what might cause the drastic difference in quality?
Also, in R Markdown, is it possible to have custom sizing of the images as well as placement? I am specifically interested in making the map larger and/or displaying another map with it side-by-side.
This first block of code is just to get your hands on the data if desired.
#install.packages("RCurl"); install.packages("xlsx"); install.packages("zipcode"); install.packages("ggmap")
library(RCurl)
library(xlsx)
# NOTE if you can't download the file automatically, download it manually at:
#'http://www.psc.isr.umich.edu/dis/census/Features/tract2zip/'
urlfile <-'http://www.psc.isr.umich.edu/dis/census/Features/tract2zip/MedianZIP-3.xlsx'
destfile <- "census20062010.xlsx"
download.file(urlfile, destfile, mode="wb")
census <- read.xlsx2(destfile, sheetName = "Median")
#census <- read.xlsx2(file = "census20062010.xlsx", sheetName = "Median")
head(census)
# clean up data
# census <- census[c('Zip','Median..', 'Pop')]
names(census) <- c('Zip','Median', 'Pop')
census$Median <- as.character(census$Median)
census$Median <- as.numeric(gsub(',','',census$Median))
census$Pop <- as.numeric(gsub(',','',census$Pop))
head(census)
# get geographical coordinates from zipcode
library(zipcode)
data(zipcode)
census$Zip <- clean.zipcodes(census$Zip)
census <- merge(census, zipcode, by.x='Zip', by.y='zip')
census$location <- paste0(census$city, ", ", census$state)
names(census) <- sapply(names(census), tolower)
# saved census to census.rdata at this point...
The next chunk of code below is what is in the markdown file.
```{r, message=FALSE, echo=FALSE}
library(ggmap)
library(ggplot2)
load("census.rdata")
inputPanel(
textInput("loc", label = "Location", value = "Orlando, FL"),
sliderInput("zoom", label = "Zoom Level",
min = 1, max = 12, value = 10, step = 1)
)
renderPlot({
census2 <- census[census$location == input$loc,]
map <- get_map(location = input$loc,
zoom = input$zoom,
maptype = 'roadmap',
source = 'google',
color = 'color',
filename = "ggmapTemp")
print(ggmap(map) +
geom_point(
aes(x=longitude, y=latitude,
show_guide = TRUE, size=Median),
data=census2, colour = I('red'), na.rm = T)
)
})
```
Thanks for your help!

Setting rChart width and height

In the (few so far) learning-oriented examples on the web of integrating knitr and rCharts we often see a line of code within an R chunk as follows:
options(RCHART_WIDTH = 800, RCHART_HEIGHT = 500)
When I include this sort of variable setting in an R chunk of an R markdown document it seems to make no difference at all to the actual size of the eventual chart in html. What extra thing am I missing that will let me control the width and height of rCharts? Reproducible *.rmd file is below.
```{r echo = F, message = F, cache = F}
opts_chunk$set(results = 'asis', comment = NA, message = F, tidy = F, echo=FALSE, cache=FALSE)
require(rCharts)
options(RCHART_WIDTH = 600, RCHART_HEIGHT = 400)
```
## Example plot with un-customised dimensions
```{r}
data(economics, package = 'ggplot2')
econ <- transform(economics, date = as.character(date))
m1 <- mPlot(x = 'date', y = c('psavert', 'uempmed'), type = 'Line',
data = econ)
m1$set(pointSize = 0, lineWidth = 1)
m1$print('chart2', include_assets = TRUE, cdn=TRUE)
```
The issue is that the print method does not include the css that sizes the charts. Easiest fix would be to add the following css to your Rmd file .rChart{width: 600px; height: 400px}. The print method is being rewritten and we are trying to figure out the most flexible yet useful way to do it.

Resources