The following tips were presented at CORA’s (Chester Open Research Alliance) Staff R users group at the University of Chester.
If you have any questions about the staff R users group or CORA, please get in touch with either myself or Dr Suzanne Stewart.
You should aim to include the version of R that you are working on at the top of your script. This is useful information for others when they view your code.
To print the current R Version, include the code:
version$version.string
## [1] "R version 3.6.2 (2019-12-12)"
My second tip refers to how you state which packages are being used in your script. If somebody else was to download your analysis code and data, they might not have the relevant packages installed. Using this short code you can list the packages that will be needed. Then, the second line checks their already-installed packages and determines which ones are needed. The third line then installs any of the packages that are needed but not yet installed.
Lines 3-6 make sure that the installed packages are loaded into the current r session.
list_of_packages<-c("assertr","tidyverse")
new.packages <- list_of_packages[!(list_of_packages %in% installed.packages()[,"Package"])]
if(length(new.packages))install.packages(new.packages)
library(assertr)
library(tidyverse)
As others are might be using your code, you should make sure that they have the latest version of the packages that you have used. There are a few ways to do this but one way is to include this short line after the code from Tip 2.
This code updates the current packages without asking for confirmation. It uses the main website for the repository.
update.packages(ask=FALSE, repos = "https://cloud.r-project.org")
Beep!
There is an awesome package called beepr. You can use it at the end of your long scripts to play an audible sound to indicate the end of script processing. This is particularly useful for scripts that might take longer.
For a list of the various sounds, check out the help text by typing ?beepr::beep into the console.
beepr::beep(1)
Using an r project helps keep your script and data together, negating the need for including long irrelevant file paths in your script. E.g. I have used a project here and so the only path I need is to indicate where in the project the data is stored.
mydata = read.csv("data/example_data.csv")
Once the data is loaded in, you can do your (arbitary) analysis e.g.
p1 = 1
p2 = 0.2
fit = nls(ydata ~ p1*cos(p2*xdata) + p2*sin(p1*xdata), data = mydata, start = list(p1=p1,p2=p2))
new = data.frame(xdata = seq(min(mydata$xdata),max(mydata$xdata),len=200))
plot(mydata$xdata,mydata$ydata,col='red') + lines(new$xdata,predict(fit,newdata=new))
%>%The pipe is used to pass on anything from its left side onto its right side. When reading code it is often best read as being “and then”.
For this example, the code below will bake a cake. This code works (The bare minimum a coder wishes for). But… it is not very human readable.
In this next example, we have the same cake baking code, but now it uses the pipe %>% operator. Try reading this code and where you see the pipe, say “and then”