Reader small image

You're reading from  Learning R Programming

Product typeBook
Published inOct 2016
Reading LevelBeginner
PublisherPackt
ISBN-139781785889776
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Kun Ren
Kun Ren
author image
Kun Ren

Kun Ren has used R for nearly 4 years in quantitative trading, along with C++ and C#, and he has worked very intensively (more than 8-10 hours every day) on useful R packages that the community does not offer yet. He contributes to packages developed by other authors and reports issues to make things work better. He is also a frequent speaker at R conferences in China and has given multiple talks. Kun also has a great social media presence. Additionally, he has substantially contributed to various projects, which is evident from his GitHub account: https://github.com/renkun-ken https://cn.linkedin.com/in/kun-ren-76027530 http://renkun.me/ http://renkun.me/formattable/ http://renkun.me/pipeR/ http://renkun.me/rlist/
Read more about Kun Ren

Right arrow

Chapter 3. Managing Your Workspace

If the behavior of R objects is compared to game rules, then the workspace can be compared to the playground. To play the game well, you need to familiarize yourself not only with the rules, but also with the playground. In this chapter, I will introduce to you some basic but important skills to manage your workspace. These skills include:

  • Using the working directory

  • Inspecting the working environment

  • Modifying global options

  • Managing the library of packages

R's working directory


An R session always starts in a directory, no matter whether it is launched as an R terminal or in RStudio. The directory in which R is running is called the working directory of the R session. When you access other files on your hard drive, you can use either absolute paths (for example, D:\Workspaces\test-project\data\2015.csv) in most cases or relative paths (for example, data\2015.csv) with the right working directory (in this case, D:\Workspaces\test-project).

The use of relative paths to the working directory does not change the file paths, but the way you specify them is shorter. It also makes your scripts more portable. Imagine you are writing some R scripts to produce graphics according to a bunch of data files in a directory. If you write the directory as an absolute path, then anyone else who wants to run your script on their own computer would have to modify the paths in your code to the location of the data in their hard drives. However, if you write the...

Inspecting the environment


In R, every expression is evaluated within a specific environment. An environment is a collection of symbols and their bindings. When we bind a value to a symbol, call a function, or refer to a name, R will find the symbols in the current environment. If you type commands in the RStudio console, your commands are evaluated in the Global Environment.

For example, when we start a fresh R session in a terminal or RStudio, we start working within an empty global environment. In other words, there is no symbol defined in this environment. If we run x <- c(1, 2, 3), the numeric vector c(1, 2, 3) is bound to symbol x in the global environment. Then, the global environment has one binding that maps x to the vector c(1, 2, 3). In other words, if you evaluate x, then you will get its value.

Inspecting existing symbols

In addition to manipulating vectors and lists as we did in the previous chapter, we need to know some basic functions to inspect and manipulate our working...

Modifying global options


Instead of creating, inspecting, and removing objects in the working environment, R options have effects in the global scale of the current R session. We can call getOption() to see the value of a given option and call options() to modify one.

Modifying the number of digits to print

In RStudio, when you type getOption(<Tab>), you can see a list of available options and their descriptions. A commonly used option, for instance, is the number of digits to display. Sometimes, it is not sufficient when we deal with numbers requiring higher precision. In an R session, the number of digits printed on screen is entirely managed by digits. We can call getOption() to see the current value of digits and call options() to set digits to a larger number:

When an R session starts, the default value of digits is 7. To demonstrate its effect, run the following code:

123.12345678
## [1] 123.1235

It is obvious that the 11-digit number is only shown with 7 digits. This means the...

Managing the library of packages


In R, packages play an indispensable role in data analysis and visualization. In fact, R itself is only a tiny core and is built on several basic packages. A package is a container of predefined functions, which are often designed to be general enough to solve a certain range of problems. Using a well-designed package, we don't have to reinvent the wheel again and again, which allows us to focus more on the problem we are trying to solve.

R is powerful not only because of its rich source of packages, but also because of the well-maintained package archive system called The Comprehensive R Archive Network, or CRAN (http://cran.r-project.org/). The source code of R and thousands of packages is archived in this system. At the time of writing, there are 7,750 active packages on CRAN maintained by more than 4,500 package maintainers around the world. Every week, more than 100 packages will be updated and more than 2 million package downloads happen. You can check...

Summary


In this chapter, you learned about the working-directory concept and tools dealing with it. You also explored functions to inspect the working environment, modify global options, and manage the library of packages. Now, you have the basic knowledge to manage your workspace.

In this next chapter, you will learn several basic expressions, including assignment, condition, and loop. These expressions are the building blocks of program logic. I will show you how to write efficient and robust control-flow expressions in the next chapter.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning R Programming
Published in: Oct 2016Publisher: PacktISBN-13: 9781785889776
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Kun Ren

Kun Ren has used R for nearly 4 years in quantitative trading, along with C++ and C#, and he has worked very intensively (more than 8-10 hours every day) on useful R packages that the community does not offer yet. He contributes to packages developed by other authors and reports issues to make things work better. He is also a frequent speaker at R conferences in China and has given multiple talks. Kun also has a great social media presence. Additionally, he has substantially contributed to various projects, which is evident from his GitHub account: https://github.com/renkun-ken https://cn.linkedin.com/in/kun-ren-76027530 http://renkun.me/ http://renkun.me/formattable/ http://renkun.me/pipeR/ http://renkun.me/rlist/
Read more about Kun Ren