Before We Start
What is R?
R is a versatile, open source programming/scripting language available on all platforms that is useful not only for statistics, but also data science. It is inspired by the programming language S.
R is open-source software licensed under the GNU General Public License (GPL). It is superior (if not just comparable) to commercial alternatives, and is widely used both in academia and industry.
What is RStudio?
RStudio is a freely available open-source Integrated Development Environment (IDE). It can be downloaded from the RStudio webite. It is a great alternative to working on R in the terminal for many reasons:
automatic syntax highlighting/formatting in the editor
direct code execution from editor to console
real-time access to environment, plotting, and history
good tool for workspace management
The RStudio interface has four main panels:
Console: where you can type commands and see output
Editor: where you can type out commands and save to file. You can also run in console with Ctrl Enter
Workspace/History: workspace shows all active objects and history keeps track of all commands run in console
Files/Plots/Packages/Help
Best practices
Code and workflow are more reproducible if we can document everything that we do.
Our end goal is not just to “do stuff”, but to do it in a way that anyone can easily and exactly replicate our workflow and results.
All code should be written in the editor and saved to file, rather than working in the console. The R console should be used to inspect objects, test a function or get help.
Use
#
signs to comment. Comment liberally in your R scripts. This will help future you and other collaborators know what each line of code (or code block) was meant to do. Anything to the right of a # is ignored by R. A shortcut for this is Ctrl + Shift + C if you want to comment an entire chunk of text.Organizing your working directory: You should separate the original data (raw data) from intermediate datasets that you may create for the need of a particular analysis. For instance, you may want to create a data/ directory within your working directory that stores the data, and have a results/ directory for intermediate datasets and a figures/ directory for the plots you will generate. The current working directory has a data folder already. Create additional directories called "results" and "figures" respectively. You can do this by navigating to the lower right panel and using the New Folder button in the file tab
Interacting with R
There are two main ways of interacting with R: using the console or by using script files (plain text files that contain your code).
The console window (in RStudio, the bottom left panel) is the place where R is waiting for you to tell it what to do, and where it will show the results of a command. You can type commands directly into the console, but they will be forgotten when you close the session. It is better to enter the commands in the script editor, and save the script. This way, you have a complete record of what you did, you can easily show others how you did it and you can do it again later on if needed. You can copy-paste the code into the R console, but the Rstudio script editor allows you to ‘send’ the current line or the currently selected text to the R console using the Ctrl-Enter
shortcut.
Command prompt
If R is ready to accept commands, the R console shows a > prompt. If it receives a command (by typing, copy-pasting or sent from the script editor using Ctrl-Enter
), R will try to execute it, and when ready, show the results and come back with a new > prompt to wait for new commands.
If R is still waiting for you to enter more data because it isn’t complete yet, the console will show a + prompt. It means that you haven’t finished entering a complete command. This is because you have not ‘closed’ a parenthesis or quotation. If you’re in Rstudio and this happens, click inside the console window and press Ctrl+c
; this should help you out of trouble.
More powerful R computing for genomic analyses
Genomic data can be too big to run on your laptop. BCC/BMC cluster has lots of memory and CPU which enables processing big data in a parallel fashion. However, you need to be trained by our course Introduction to Unix and KI Computational Resources in order to run R scripts on our cluster in appropriate ways.
Last updated