Embedding googleVis charts into a web site
R开发者_C百科eading from the googleVis package vignette: "With the googleVis package users can create easily web pages with interactive charts based on R data frames and display them either via the R.rsp package or within their own sites". Following the instructions I was able to see the sample charts, using the plot method for gvis objects. This method by default creates a rsp-file in the rsp/myAnalysis folder of the googleVis package, using the type and chart id information of the object and displays the output using the local web server of the R.rsp package (port 8074 by default).
Could anybody help me (or provide some link) on the procedure someone has to follow in order to embed such charts into an existing web site (e.g. a joomla site)?
Obviously I think that this is too verbose for @gd047, but I put a kind of tutorial since it maybe helpful for other readers who want to use googleVis on their own website.
install googleVis from CRAN
install.packages('googleVis')
pay attention to the messages.
then, create gvis object:
library(googleVis)
M <- gvisMotionChart(Fruits, "Fruit", "Year")
you can find the contents of M by:
> M
and you can find the plot on your browser:
> plot(M)
then, what is necessary to generate the chart is M$html$chart:
> M$html$chart
[1] "<!-- MotionChart ... omitted... \">\n</div>\n"
save it to a file:
> cat(M$html$chart, file="tmp.html")
if you open the "tmp.html" as a file (i.e, address says files:///***/tmp.html), then security warning may occur. What you need is to access the html via http://.
So if you can edit any web page where <script> tag is available (e.g., blogger), you can use it by simply copy and paste the contents of tmp.html, like this:
http://takahashik.blogspot.com/2011/01/googlevis-example.html
here is the famous "iris" version of example:
http://takahashik.blogspot.com/2011/01/googlevis-example-for-data-iris_10.html
Otherwise, if you have a web server, you can use it by uploading the tmp.html on the server.
If you want to copy and paste the chart manually to a CMS (e.g. Joomla/Wordpress site), than you could do it from the 'gvis' object's html list. Just like @kohske suggested:
# demo data from manual
M <- gvisMotionChart(Fruits, "Fruit", "Year")
# write the HTML body to a temporary file without header and footer
cat(M$html$chart, file="temp.html")
# or with caption included:
cat(paste(M$html[c("chart", "caption")], collapse="\n"), file="temp.html")
Then copy and paste the content of temp.html to your Joomla site. You should pay attention to paste the code as HTML content, not in the WYSIWYG editor (e.g. Tiny MCE)!
If you want to show it on a separate page, do not forget to include header and footer also:
# demo data from manual
M <- gvisMotionChart(Fruits, "Fruit", "Year")
# write the HTML to a temporary file with header and footer all included
cat(paste(M$html, collapse="\n"), file="temp.html")
And at last: you can easily upload this document to e.g. an ftp server and reach it via any browser.
Flash content might not work on local machine. I had to change the security restrictions on adobe site in order to make it work.
Now, by following daroczig's instructions one can view the content in the local browser and do not rely on R.rsp altogether.
You could alternatively write the following code
print(M,"chart", file="myfile")
copy and paste the html output to your webpage and the googleVis chart will run
I have a script to run automatically as data updates and place a googleVis chart on a website using RCurl and googleVis. Here is an example where visChart is the chart:
library(RCurl)
library(googleVis)
make visChart
write(visChart$html$chart, file='visChart.html')
ftpUpload('visChart.html', "ftp://username:password@example.com/path/to/'visChart.html")
visChart$html$chart
gives just the html for just the chart with out the footer so then I use an iframe in my HTML to access this chart. If you are using a google site, blogger or want a google gadget you can use createGoogleGadget():
write(createGoogleGadget(visChart), file='visChart.xml')
ftpUpload('visChart.xml', "ftp://username:password@example.com/path/to/'visChart.xml")
精彩评论