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")
[1] "hello!"

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

Take your Linux skills to the next level!

How Linux Works

Take your Linux skills to the next level! Try Linux & UNIX

In this completely revised second edition of the perennial best seller How Linux Works, author Brian Ward makes the concepts behind Linux internals accessible to anyone curious about the inner workings of the operating system. Inside, you'll find the kind of knowledge that normally comes from years of experience doing things the hard way.

$ Check price
39.9539.95Amazon 5 logo(114+ reviews)

More Linux & UNIX resources

Become a Bioinformatics Whiz!

Bioinformatics Data Skills

Become a Bioinformatics Whiz! Try Bioinformatics

Learn the best practices used by academic and industry professionals. Bioinformatics Data Skills give a great overview to the Linux Command Line, Github, and other essential tools used in the trade. This book bridges the gap between knowing a few programming languages and being able to utilize the tools to analyze large amounts of biological data.

$ Check price
49.9949.99Amazon 4.5 logo(7+ reviews)

More Bioinformatics resources