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.

Learn to be a Pythonista!

Python Playground

Learn to be a Pythonista! Try Python

Python Playground is a collection of fun programming projects that will inspire you to new heights. You'll manipulate images, build simulations, and interact with hardware using Arduino & Raspberry Pi. With each project, you'll get familiarized with leveraging external libraries for specialized tasks, breaking problems into smaller, solvable pieces, and translating algorithms into code.

$ Check price
29.9529.95Amazon 4 logo(14+ reviews)

More Python resources

Take your Linux skills to the next level!

System Admin Handbook

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

This book approaches system administration in a practical way and is an invaluable reference for both new administrators and experienced professionals. It details best practices for every facet of system administration, including storage management, network design and administration, email, web hosting, scripting, and much more.

$ Check price
74.9974.99Amazon 4.5 logo(142+ reviews)

More Linux & UNIX resources