Pregunta

Using R, I would like to be able to render an .html or .md file (that has either been created via knitr or some other manner) that contains googleVis charts in a repo in github.

I was trying to follow the help file when running ?plot.gvis, and I have tried pushing up the gvisData.js and gvisFunctions.js files into the repo and altered the html to reference these, but I have a feeling that I dont quite have the correct baseURL in order for github to be able to render it correctly.

Does anybody have a simple example of a URL that references Github that renders googleVis charts?

I have tried using this http://lamages.blogspot.co.uk/2013/07/googlevis-tutorial-at-user2013.html but didn't see how it would work with github...

So using the example given in ?plot.gvis this is what I tried

myChartID <- "mtnc"
baseURL <- "https://raw.github.com/USER/REPO"
wwwdir <- getwd() ## the working directory is the local directory of the repo


## Create a motion chart
M <- gvisMotionChart(Fruits, "Fruit", "Year", chartid=myChartID)



## Write the data and functions into separate files:
cat(M$html$chart['jsData'], file=file.path(wwwdir, "gvisData.js"))
cat(M$html$chart[c('jsDrawChart', 'jsDisplayChart', 'jsChart')], 
                file=file.path(wwwdir, "gvisFunctions.js"))


## Create a html page with reference to the above
## JavaScript files 

html <- sprintf('
<html>
  <head>
  <script type="text/javascript" src="http://www.google.com/jsapi">
  </script>
  <script type="text/javascript" src="%s/gvisFunctions.js"></script>
  <script type="text/javascript" src="%s/gvisData.js"></script>
  <script type="text/javascript">
  displayChart%s()
  </script>
  </head>
  <body>
  <div id="%s" style="width: 600px; height: 500px;"></div>
  </body>
  </html>
  ', baseURL, baseURL, myChartID, myChartID)

## Write html scaffold into a file
cat(html, file=file.path(wwwdir, paste("Chart", myChartID, ".html", sep="")))

### from this point I push up to the repo the following files 
### gvisData.js, gvsiFunctions.js and Chartmtnc.html

## Display the result via
URL <- paste(baseURL,"/Chart", myChartID, ".html", sep="")
browseURL(URL)

Any suggestions would be useful...

¿Fue útil?

Solución

You are complicating it unnecessarily. You just need to do one thing, when you are trying to use a googleVis chart in a knitr document, which is to set

options(gvis.plot.tag = 'chart')

You can see a published example here and the source file can be found here

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top