8 Additional R Features

As we conclude this training, I also want to mention a few additional features that you might find useful when analyzing and visualizing data using R.

8.1 Removing Objects from the Workspace

When you define an object (e.g., a dataset) within an R session, it stays in the workspace until you close R. However, if you decide to delete a particular object while still using R, one of the following methods can be used:

x <- 1:100

# Use rm() to remove x entirely
rm(x)

# Or, turn x into NULL (deletes the content of x)
x <- NULL

8.2 Saving Output in R

There are several ways to save the output of your analysis in R (i.e., everything printed on the console section). The following are some of the options to save output in R:

8.2.1 Using the sink Function

Using the sink Function, it is possible to create an empty text document (called my_output.txt in the following example) and capture the output printed in the console. All the output, including warning messages, in the console is saved into the document once the final sink() function is executed.

# Start saving the output
sink("my_output.txt")

fit <- lm(mpg ~ wt, data = mtcars)
summary(fit)

# Stop saving the output
sink()

8.2.2 Saving Objects from an R Session

If you want to save on particular object that you made (e.g., a dataset) for use in R later, you can use the save function.

mydata <- data.frame(person = 1:10,
                     age = sample.int(30, 10))

# Save the data
save(mydata, file="myfile.Rdata")

# Load the data when needed again
load("myfile.Rdata")

Alternatively, you can save the entire R session using save.image(). This saves all of the objects to a file “.RData” in your working directory.

mydata1 <- data.frame(person = 1:10, 
                      age = sample.int(30, 10))

mydata2 <- data.frame(person = 1:10, 
                      weight = rnorm(10, 75, 4))

x <- 1:100

# It saves all objects in one place
save.image(file = "myfile.Rdata")

# Load the data when needed again
load("myfile.Rdata")

8.2.3 Saving the History of an R Session

If you want to save all the commands you used in a session, then you can use the savehistory function. This is helpful because it allows you to browse the history (i.e., all the commands or functions you have entered) when you load the same history into R in the future. You simply press the up-arrow and down-arrow keys to view the commands you typed earlier.

# Save the history
savehistory(file = "my_analysis.Rhistory")

# Load the history when needed again
loadhistory("my_analysis.Rhistory")

8.2.4 Using R Markdown

R Markdown (https://rmarkdown.rstudio.com/) is an excellent way to save all the output from R (e.g., codes, output, data visualizations) in a nice, readable format. R Markdown allows users to turn their analysis into high quality documents, which then can be exported as an HTML file, a PDF, or a Word document. To see how R Markdown works, check out this nice tutorial: https://rmarkdown.rstudio.com/lesson-1.html

8.3 Unloading a Package

Sometimes you may want to deactivate or unload some packages that you have do not need for your analysis anymore. The detach function allows users to remove a particular package from an R session. For example, let’s assume that we activated the dplyr package but realized that we do not need this particular package anymore. The following line will remove the package from our session (i.e., the package will be deactivated):

detach("package:dplyr", unload=TRUE)

8.4 Updating R and R Packages

Every now and then the authors of packages release updated versions of their packages. The updated versions add new functions, fix bugs, and so on. Therefore, it might be a good idea to update your packages periodically. To update a particular package, we can use the update.packages function.

# Update the dplyr package
update.packages("dplyr")

About every six months or so, a new version of R is released. It is not possible to update R from within RStudio. To get the new version, you can go to the CRAN website (https://cran.r-project.org/) and download the most recent version of R. Alternatively, you can update R using the R GUI (not RStudio). The following commands will install the installr package, download and install the latest version of R, and finally carry all of the packages in your library to the new version of R.

# Install and activate the package
install.packages("installr")
library("installr")

# Update R with default options
updateR(TRUE)

# Or, installr asks what you want at each step
updateR()