These are simple instructions for preparing a reproducible R script you can share with colleagues/friends/teachers in a way that will make it easy to collaborate on.
A reproducible script is one you can open and run to get the same result you got when you created it. Reproducible //and portable is a script which others can also open, run, and get the same result as you did, with no (or minimal) need for interference with the code. Remember that a script could be fully reproducible on your computer (with your structure of file folders, your R version, and all libraries already installed), but it may not run on others' computers. If you make the script just for yourself, it's fine (you are likely to open it on the same computer with the same files stored on the same file paths, so it should run; just make sure that your future you will know what the script is doing). But if you want to share the script with others, make sure it is also portable, i.e., others can run it on their computer. If your script starts with setwd ('c:\\Users\\Me\\This\\Folder\\Only\\I\\Have'), than it is most likely not portable.
Below are a few issues you may need to pay attention to when producing a reproducible and portable R script.
Quick summary:
library (myLovelyPackage)), but do not include an installation script (or comment it, e.g. #install.packages ('myLovelyPackage')).#), but not necessarily for every trivial action.setwd referring to local directories on your computer (e.g., by setwd ('c:\\Users\\Me\\This\\Folder\\Only\\I\\Have')); this makes it definitely not portable. If you run the code on your computer, but also share it with others, you can include setwd to your folder structure, but again comment it (by adding # at the beginning of that line).myData <- read.delim ('clipboard') - you don't disclose what was on the clipboard when you run the script, and next time you may copy something else there. Better is myData <- read.delim ('c:\\Users\\Me\\This\\Folder\\Only\\I\\Have\\myData.txt'), which is reproducible on your computer, but not portable to other computers. To make the script portable, the best is to load data from somewhere (a dataset in R, Dropbox, a Gist on GitHub), or directly include data in the script (e.g., using the dput function). If using Dropbox, do not forget to change the end of the link from dl=0 to dl=1 (otherwise, you load nonsense; see below why). Always safe is to include data as a file together with the script (perhaps zipped together into an archive), but make sure that the script contains the part telling the user how to load the data from that file;#; R ignores everything what is written on the line after #; source on your R script instead of running it line-by-line via the command line. In RStudio, this can also be done by pressing the Source button in the head of the code panel (when the file you want to source is open).Preferably a *.r file, which can be directly opened in R or RStudio and sent to the command line. For more advanced users, consider using a commented script with comments written in R Markdown and saved into *.rmd format (ensure that the file can also knit on other computers, or include a PDF version of the file for sure).
Use plenty of annotations, but don't comment on everything, just things that aren't immediately apparent or require the user's attention. Start the script with a header including a brief title and your name (also include an email address if you want feedback). If the script produces some files (e.g., figure files), it may be good to mention this in the script header, so that users know what to expect.
In most cases, you are using some data on which you do editing/plotting/analysis or whatever else. There are several options for how to share data, depending on how large the data are and whether they can be made public or need to remain private:
myData <- data.frame (spe = c(2,45,2), ele = c(345, 456, 678)). Alternatively, use the function dput to store R objects in a text representation that can be sourced back into R. Example: dput (myData) will return structure(list(spe = c(2, 45, 2), ele = c(345, 456, 678)), .Names = c(“spe”,
“ele”), row.names = c(NA, -3L), class = “data.frame”), and you can use this to create dataset myData_2 <- structure(list(spe = c(2, 45, 2), ele = c(345, 456, 678)), .Names = c(“spe”, “ele”), row.names = c(NA, -3L), class = “data.frame”).read.delim or readr::read_delim functions.read.delim or readr::read_delim functions. If you use the Dropbox share functionality, you need to replace dl=0 at the end of the link with dl=1 (this triggers the direct download of the file instead of opening it in the website browser). Example: temp <- read.delim (file = 'https://www.dropbox.com/s/8svpdyq959a8bx6/temperature.txt?dl=1') (note that there is dl=1 at the end of the dropbox link).setwd) to the same directory where s/he saved your data. This may need to be mentioned in the R code. Do not include files in uncommon formats that may require special software; e.g., reading Excel files is not recommended, since it is a commercial format and can change anytime. Preferably store data as plain text in a *.txt file with cells separated by tabs.A few suggestions for good practice:
library (thatPackage)), but do not include install.packages (“thatPackage”) command - and if yes, please comment it (by adding # at the beginning of the command). You don't want others to reinstall libraries that may already be installed and working on their computer.View or fix or edit or head or str commands).