06. Starting R up

When running R, your environment configuration is loaded. These are all the preferences you may set to customize your R session.

To customize these configurations, you must edit your startup file, .Rprofile. This file is available in your home folder. If you're on a mac, you can get your home folder from the command line, use cd ~ (~ is the shortcut for your home folder, and cd is the command for change directory).

Since this file starts with a ".," it means that it is hidden. Configuration files, by default, are hidden. To list hidden files use ls -a. ls lists folder contents, and the -a option shows hidden files. To learn more about the Linux Command Line, check out our tutorial series.

If no configuration files exist, create one.

Setting your text editor

You may set the default editor used within R with the options().

> options(editor="/usr/bin/vim")

Now when you want to edit a file within R, you can pass in the edit() function, with the name of the file.

> edit('myfile.txt')

Remember that this only works if you're working through the command line, and not if you're on an IDE such as RStudio!

Getting/setting your current working directory

We have already seen how we can run R scripts from outside R, or directly from the command line. To run scripts within your current R session, use the source() command.

> source("test.r")
[1] "hello!"

Note that you must have R's current working directory as the folder which contains test.r or R won't be able to find the file. In this case, the error output would look something like this:

> source("test.r")
Error in file(filename, "r", encoding = encoding) :
  cannot open the connection
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
  cannot open file 'test.r': No such file or directory

To fix this error message, we may check our working directory with setwd().

> getwd()
[1] "/Users/johndoe"

It looks like we set our working directory to our home page. If our test scripts are within the folder test, then we may set it to this with the setwd() command.

> setwd("/Users/johndoe/test")
source("test.r")
[1] "hello!"

Great! Now you should be able to run R commands from a file straight from the command line and even within R.

Become a Bioinformatics Whiz!

Introduction to Bioinformatics Vol. 2

Become a Bioinformatics Whiz! Try Bioinformatics

This is Volume 2 of Bioinformatics Algorithms: An Active Learning Approach. This book presents students with a light-hearted and analogy-filled companion to the author's acclaimed course on Coursera. Each chapter begins with an interesting biological question that further evolves into more and more efficiently solutions of solving it.

$ Check price
49.9949.99Amazon 5 logo(5+ reviews)

More Bioinformatics resources

Learn to be a Pythonista!

Learn Python in One Day

Learn to be a Pythonista! Try Python

Have you always wanted to learn computer programming but are afraid it'll be too difficult for you? Or you're familiar with some programming but are interested in learning Python fast? Then this book is for you. You no longer have to waste your time and money learning Python from lengthy books, expensive online courses or complicated Python tutorials.

$ Check price
11.9711.97Amazon 4 logo(185+ reviews)

More Python resources

Ad