3.4 Housekeeping

3.4.1 Working directory

getwd() returns the current working directly. setwd(new_directory) sets a specified working directory.

3.4.2 R session

sessionInfo() shows the current session information. In RStudio, .rs.restartR() restarts a session.

3.4.3 Save & load

R objects can be saved and loaded by save(object1, object2, ..., file="file_name.RData") and load(file="file_name.RData"). A ggplot object can be save by ggsave("file_name.png").

3.4.4 Input & Output

A common method to read and write data files is read.csv("file_name.csv") and write.csv(data_frame, file = "file_name.csv"). scan() is a more general function to read data files and interact with user keyboard inputs. file() is also a general function for reading data through connections, which refer to R’s mechanism for various I/O operations. dir() returns the file names in your working directory.

A useful function is cat(), which can print a cleaner output to the screen, compared to print().

print("example")
## [1] "example"
cat("example\n")  # end with \n
## example
cat("some string", c(1:4), "more string\n")
## some string 1 2 3 4 more string
cat("some string", c(1:4), "more string\n", sep="_")
## some string_1_2_3_4_more string

3.4.5 Updating

R needs regular updates for R distribution, individual R packages, and RStudio. Generally, updating once or twice a year would suffice. For updating RStudio, go to Help and then Check for Updates. Also, RStudio also makes it easy to update packages; go to Tools and the Check for Package Updates. Do these updates when you have time or you know that you need to update a particular package; updating R and R packages can be trickier than it seems.

# check R version
getRversion()
## [1] '3.4.4'
version
##                _                           
## platform       x86_64-apple-darwin15.6.0   
## arch           x86_64                      
## os             darwin15.6.0                
## system         x86_64, darwin15.6.0        
## status                                     
## major          3                           
## minor          4.4                         
## year           2018                        
## month          03                          
## day            15                          
## svn rev        74408                       
## language       R                           
## version.string R version 3.4.4 (2018-03-15)
## nickname       Someone to Lean On
# check installed packages
## installed.packages()

# list all packages where an update is available
## old.packages()

# update all available packages of installed packages 
## update.packages()

# update, without prompt
## update.packages(ask = FALSE)

For windows users, one can automate the process using installr package.

## --- execute the following ---
## install.packages("installr") # install
## setInternet2(TRUE) # only for R versions older than 3.3.0
## installr::updateR() # updating R.

Sometimes, you can accidentally corrupt sample datasets that come with packages. To restore the original datasets, you have to remove the package by remove.packages() and then install it again. Use class(), attributes(), and str() to check for any unrecognized attributes attached to the dataset. Also, if you suspect that you have accidentally corrupted R itself, you should re-install the R distribution.