2

I really need serious help here. I have been working with R the last month. I have been analyzing data, and plotting many visualizations using ggplot2. I have many .r files which includes data transformations and reading .csv ..etc

  • My instructor wants me to show him my code or at least he want to execute and get all the figures I did. Can i run .r scripts (that includes codes to generate ggplot figures) on my instructor machine? - the good thing, the instructor machine has R too.

PS: The result can be as saving image to .JPEG to the instructor's machine.

Your help is much appreciated.

Thanks!

Lian Ahmad
  • 109
  • 1
  • 10
  • possible duplicate of [How to save a plot as image on the disk?](http://stackoverflow.com/questions/7144118/how-to-save-a-plot-as-image-on-the-disk) – rmuc8 Jun 10 '15 at 11:32
  • What OS are you using? Do you have access to a terminal? – waferthin Jun 10 '15 at 11:32
  • Hello All, thanks for your reply.. Windows 7.. – Lian Ahmad Jun 10 '15 at 11:33
  • If someone know the SVN, it is like Virtualbox. I need to install R there, and then the instructor himself want to execute the code. I was think of just having like .exe or executable. – Lian Ahmad Jun 10 '15 at 11:36
  • Any suggestions, help ? Thanks! – Lian Ahmad Jun 10 '15 at 11:59
  • Can you update your question with some more specifics rather than commenting on your question? It sounds like you just need to send your instructor your .R file(s) that include code to save the plots to local files. – Thomas Jun 10 '15 at 12:09
  • Thanks Thomas! what you are saying is nearly what i want. I also updated my question. – Lian Ahmad Jun 10 '15 at 12:20

1 Answers1

2

First, you need to ensure that the R software where you will run your scripts, has all required packages installed. Second, make sure the data is somehow available. This should usually make all scripts runnable.

If you want to save ggplot-figures, use the ggsave-function after each plot is generated. This will save the last plot into the working directory (unless else specified).

Depending on how you wrote your code, you may create an "executable" script that calls all your relevant r-scripts via source command, so only one r-script needs to be compiled / sourced, and all others are automatically sourced from this "basefile".

An example of an "executable" script, which I usually create when working on new projects:

# --------------------------------
# load required libraries
# --------------------------------
library(sjmisc)
library(sjPlot)
# --------------------------------
# load data
# --------------------------------
david <- read_spss("Pat_Dateneingabe150511_MR_DL.sav")
# --------------------------------
# do all necessary recoding
# --------------------------------
source("recodes.R")
# --------------------------------
# create new variables / scales
# --------------------------------
source("Skalenbildung.R")

In this case, I have two large scripts recodes.R and Skalenbildung.R, which are sourced from this "base"-script. You could then add another script plots.R or whatever you may call it, and this script then contains all ggplot-commands to create the figures (don't forget to add library(ggplot2)). After each ggplot(...) add a ggsave(...) to save each plot.

Instead of using ggsave, you can also easily print all plots to a PDF file:

# --------------------------------
# plot all figures and save them to PDF
# --------------------------------
pdf("all-plots.pdf", width = 9, height = 7)
source("ggplot-figures.R")
dev.off()

In the ggplot-figures.R, you have all your ggplot-code that creates the plots you need, and these are saved to a single PDF.

Daniel
  • 7,252
  • 6
  • 26
  • 38
  • Thanks Daniel, can you please explain your last point, which is creating executable script..etc ? Thank you. – Lian Ahmad Jun 10 '15 at 12:41
  • Thanks Daniel, Everything is great.. And now, on the instructor's PC, we should run `Rscript filename` ? – Lian Ahmad Jun 10 '15 at 12:56
  • I haven't used the basic R, but only RStudio. I'm not sure how to execute an R file from R console or R Software w/o IDE or which commands to be used. Perhaps start R, then `File -> Open Script` and then `Edit -> Run all`? – Daniel Jun 10 '15 at 13:12
  • nevermind, Rscript filename should do everything fine! – Lian Ahmad Jun 10 '15 at 15:26
  • I added an example, as alternativ to using `ggsave`. You can also easily create a PDF including all plots. – Daniel Jun 10 '15 at 20:31