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 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.
You may set the default editor used within R with the
Now when you want to edit a file within R, you can pass in the
edit() function, with the name of the file.
Remember that this only works if you're working through the command line, and not if you're on an IDE such as RStudio!
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("test.r")  "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
> getwd()  "/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("/Users/johndoe/test") source("test.r")  "hello!"
Great! Now you should be able to run R commands from a file straight from the command line and even within R.
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
Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.$ Check price