Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

N B
PLUS
N B
Courses Plus Student 2,367 Points

Implementing R on website

I want to display graphs, generated with R, on my website. How can I do this? Any intelligent ideas?

5 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Our resident data scientist, Christopher Peters, blogged about this a little while back. Check out his post and I'll let him know about this forum question.

Hi N B, there are a couple of ways that I do this. First, the simplest is just to save a plot or graph as a png or some other image file as discussed here. Make sure that you include dev.off() after your code exports the graph, though! Sometimes I'll also do this with graphs made from ggplot2, which is a package for making really nice looking graphs in R! The ggplot2 package has a function called ggsave() that will allow you to save your graphs to a local space. The third way is to use the googleVis package. When you create a graph with this package, you're creating an HTML object that you can save to a local place. So, now you have your graph stored locally on your hard drive, how can you embed it in a website? My experience comes from doing this for the Treehouse internal dashboard, so it's something that needs to be updated frequently. Therefore making the plot, saving once, and then manually uploading by FTP or another service isn't a good solution -- I don't want to have to do that every time I want to update the dashboard. The RCurl package in R has a function called ftpUpload(). This will allow you to put in your FTP server credentials and push a file (either your png, jpeg, or html file) to a certain spot on an FTP server. Now you have a completed loop! Run the R file, parse the data, make the graph, save it, and automatically upload to your FTP server! RCurl can be a bit tricky to install depending on your OS, but give the process a try and let me know if you could use further help!

N B
PLUS
N B
Courses Plus Student 2,367 Points

Hi Christopher,

thanks for your reply! And Ben, thanks for forwarding! I successfully created a test.R - with a dev.off() ;-) - that will read my data via RMySQL and that also saves PNG-barplot. Using the terminal.app (MacOSX) I start it like this: RScript test.R - works just fine. In my use case I was thinking of displaying the data dynamically (#courses = #graphics) when the site is opened - with the up to date data. In your scenario, you probably run the R scrip via a cron like once a day or even manually? I was wondering if a good way to run the R script dynamically exists? I tried php:exec('Rscript test.R') but the very same terminal script from above will not write the image file. Any ideas? -Niklas

N B
PLUS
N B
Courses Plus Student 2,367 Points

I came to the conclusion that I'll go with a cron that generates the charts. So the post above is no longer relevant I guess. But still - if you have any ideas?

Any suggestions how I can draw a stacked bar chart with this data: count course semester 1 7 a1 ss2011 2 1 a1 ws2011 3 3 a2 ss2011 4 3 bp ss2011 5 1 bp ws2010 6 1 bp ws2011

Count being the number of sheets within a course (=y) at various semesters (=x)?

Sorry for the delay! Yes, actually at this point I still run things manually. I essentially have a master file that calls sources and runs things. I've been planning to move it to a chron job for a while now, the problem for me is that since our db changes frequently, I have to kind of babysit the code. It kind of makes moving straight to a chron job one step away.

I personally like the ggplot2 package. It provides options for tons of ways to look at data. Stacked bar charts are one method in that package.

You can find documentation here.