The scipen option

In R programming language, the options() function is used to set or view global options. The scipen option, in particular, controls the display of small numbers in scientific notation.

When you set options(scipen = 999), you are essentially telling R to avoid using scientific notation when displaying numbers. The scipen option stands for “scientific penalty,” and setting it to a high value like 999 essentially disables the use of scientific notation for small numbers.

For example, without setting scipen, R might display a small number like 0.00001 in scientific notation (1e-05).

However, with options(scipen = 999), R will display it in a fixed-point format without scientific notation.

In this example, we have NOT included options(scipen = 999) in the “setup” code chunk at the beginning of this document. If we had included that option, it will apply to all code chunks in the document.

Here’s an example to illustrate:

Code Chunk 1

# Default behavior without setting the scipen option
x <- 0.00001
print(x)
## [1] 1e-05

Code Chunk 2

# Setting scipen to a high value
options(scipen=999)
x <- 0.00001
print(x)
## [1] 0.00001

This can be useful in situations where you want to control the display format of numbers, especially in reports or visualizations where human readability is a priority.

Keep in mind that modifying global options can have an impact on the display of numbers throughout your R session, so use it judiciously.

The options(scipen = ...) setting in RMarkdown applies to all subsequent code chunks in the same document. When you set a global option like scipen in an RMarkdown document, it affects the behavior of R throughout the document.

Here’s an example:

Code Chunk 3

# Code in this chunk will use the scipen setting
x <- 0.00001
print(x)
## [1] 0.00001

In this example, both Code Chunk 2 and Code Chunk 3 use the scipen setting of 999.

The options(scipen = ...) setting in RMarkdown affects the behavior for the duration of the R session that is running the RMarkdown document. It won’t affect other R sessions or instances of RStudio, and it won’t persist beyond the current session.

If you restart R or start a new RStudio session, the scipen setting will revert to its default value unless you explicitly set it again. The setting is not saved between sessions.

Override for specific chunk

If you want to override or modify the global options for specific code chunks, you can do so within those chunks by specifying the options you want for that particular code. This allows you to have flexibility in controlling the display options for different parts of your document.

Code Chunk 4

# Setting scipen for this specific chunk
options(scipen = 0)

# Code in this chunk will use scientific notation
x <- 0.00001
print(x)
## [1] 1e-05

But once we do this, that scipen setting will remain until we either change it or restart our R session.

Code Chunk 5

# Code in this chunk will use scientific notation
x <- 0.00001
print(x)
## [1] 1e-05

To change the scipen setting back, we must include it in a code chunk.

Code Chunk 6

# Code below this chunk will Not use scientific notion
options(scipen=999)
x <- 0.00001
print(x)
## [1] 0.00001