R github渲染js文件以查看googleVis html

R github rendering js files to see googleVis html

本文关键字:googleVis html 文件 github 渲染 js      更新时间:2023-09-26

使用R,我希望能够在github中的repo中呈现包含googleVis图表的.html.md文件(通过knitr或其他方式创建)。

在运行?plot.gvis时,我试图遵循帮助文件,并尝试将gvisData.jsgvisFunctions.js文件推送到repo中,并更改html以引用这些文件,但我有一种感觉,我没有正确的baseURL,以便github能够正确地呈现它。

有没有人有一个简单的URL例子,它引用了Github来呈现googleVis图表?

我试过用这个http://lamages.blogspot.co.uk/2013/07/googlevis-tutorial-at-user2013.html但不知道它将如何与github合作。。。

所以使用?plot.gvis中给出的例子,这就是我尝试

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)

任何建议都是有用的。。。

您不必要地使它复杂化了。当您尝试在knitr文档中使用googleVis图表时,您只需要做一件事,即设置

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

您可以在这里看到一个已发布的示例,源文件可以在这里找到