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:

Modifying the number of digits to print

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 last few decimal digits are gone; the printer only displays the number with 7 digits. To verify no precision is lost because of digits = 7, see the output of the following code:

0.10000002
## [1] 0.1
0.10000002 -0.1
## [1] 2e-08

If the numbers are rounded to the seventh decimal place by default, then 0.10000002 should be rounded to 0.1 and the second expression should result in 0. However, apparently, this does not happen because digits = 7 only means the number of numeric digits to be displayed rather than rounded up.

However, in some cases, the number before the decimal point can be large, and we don't want to ignore digits following the decimal point. Without modifying digits, the following number will only display the integer part:

1234567.12345678
## [1] 1234567

If we want to see more digits printed, we need to increase digits from the default value 7 to a higher number:

getOption("digits")
## [1] 7
1e10 + 0.5
## [1] 1e + 10
options(digits = 15)
1e10 + 0.5
## [1] 10000000000.5

Note that once, options() is called, the modified options take effect immediately and may affect the behavior of all subsequent commands. To reset options, use this command:

options(digits = 7)
1e10 + 0.5
## [1] 1e + 10

Modifying the warning level

Another options example managing the warning level by specifying the value of the warn option:

getOption("warn")
## [1] 0

By default, the warning level is 0, which means a warning is a warning and an error is an error. In this state, a warning will be displayed but will not stop the code, while an error terminates the code immediately. If multiple warnings occur, they will be combined and displayed together. For example, the following conversion from a string to a numeric vector will produce a warning and result in a missing value:

as.numeric("hello")
## Warning: NAs introduced by coercion
## [1] NA

We can make it completely silent and still get a missing value from the unsuccessful conversion:

options(warn = -1)
as.numeric("hello")
## [1] NA

Then, the warning is gone. Making the warning messages disappear is almost always a bad idea. It will make potential errors silent. You can (or cannot) realize something is wrong from the final result. The recommendation is to be strict in your code and save a lot of time debugging it.

Setting warn to 1 or 2 will make buggy code fail fast. When warn = 0, the default behavior for evaluating a function call is to first return the value and then show all the warning messages together, if any. To demonstrate this behavior, the following function is called with two strings:

f <- function(x, y) {
as.numeric(x) + as.numeric(y)
}

At the default warning level, all warning messages are shown after the function returns:

options(warn = 0)
f("hello", "world")
## [1] NA
## Warning messages:
## 1: In f("hello", "world") : NAs introduced by coercion
## 2: In f("hello", "world") : NAs introduced by coercion

The function coerces two input arguments to numeric vectors. As the input arguments are both strings, two warnings will be produced, but will only appear after the function returns. If the preceding function does some heavy work and takes a considerable period of time, you won't see any warning before you get the final result but in fact the intermediate computing has been way off the correct results for quite a while.

This prompts the use of warn = 1, which forces the warning message to be printed as soon as a warning is produced:

options(warn = 1)
f("hello", "world")
## Warning in f("hello", "world") : NAs introduced by coercion
## Warning in f("hello", "world") : NAs introduced by coercion
## [1] NA

The result is the same, but the warning messages appear ahead of the result. If the function is time-consuming, we should be able to see the warning messages first. So, we can choose to stop the code and check whether something is wrong.

A warning level is even stricter. The warn = 2 argument directly regards any warning as an error.

options(warn = 2)
f("hello", "world")
## Error in f("hello", "world") : 
## (converted from warning) NAs introduced by coercion

These options have effects in the global scale. Therefore, it is convenient to manage common aspects of an R session, but it can also be dangerous to change options. Just like changing the working directory may invalidate all relative paths in the script from running, changing global options may break all subsequent code that is based on incompatible assumptions of the global options.

In general, it is not recommended to modify global options unless absolutely necessary.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset