Author

Tony Duan

1 R version

The version object in R holds information about the current R version you are running.

Code
version
               _                           
platform       aarch64-apple-darwin20      
arch           aarch64                     
os             darwin20                    
system         aarch64, darwin20           
status                                     
major          4                           
minor          5.1                         
year           2025                        
month          06                          
day            13                          
svn rev        88306                       
language       R                           
version.string R version 4.5.1 (2025-06-13)
nickname       Great Square Root           

2 Work with files

2.1 Get current directory

The getwd() function returns your current working directory as a string.

Code
getwd()

2.2 Get all file names under the current directory

The list.files() function returns a character vector of file and directory names in the specified directory. Without an argument, it lists files in the current directory.

Code
list.files()
 [1] "1 basic R_files"               "1 basic R.qmd"                
 [3] "1 basic R.rmarkdown"           "1-basic-R.rmarkdown"          
 [5] "2 probability.qmd"             "3 Statistics.qmd"             
 [7] "5 R boook.qmd"                 "6 data analytic in R book.qmd"
 [9] "7 error handling.qmd"          "hotels.csv"                   
[11] "images"                        "renv"                         

2.3 Get all file names under one level up directory

You can use .. to refer to the parent directory.

Code
list.files('../')
 [1] "_extensions"           "_freeze"               "_publish.yml"         
 [4] "_quarto.yml"           "_site"                 "data manipulation"    
 [7] "docs"                  "foldableCodeBlcok.lua" "images"               
[10] "index.qmd"             "intro"                 "Intro R.Rproj"        
[13] "LICENSE"               "other"                 "Plot"                 
[16] "Publish"               "README.md"             "Rlogo.png"            
[19] "sidebar.png"           "styles.css"           

2.4 Get file info

The file.info() function returns a data frame with information about the specified file, such as its size, creation time, and modification time.

Code
file.info("6 data analytic in R book.qmd")

2.5 Create folder

The dir.create() function creates a new directory (folder).

Code
dir.create('testing_folder')

2.6 Delete folder/file

The file.remove() function can be used to delete files. To delete an empty directory, you can also use unlink().

Code
file.remove('testing_folder')

2.7 Copy file

The file_copy() function from the fs package copies files from one location to another.

Code
library(fs)
file_copy('test.csv', 'test2.csv')

2.8 Download file from the internet

The download.file() function downloads a file from a given URL.

Code
url="https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-02-11/hotels.csv"

download.file(url = url, destfile = "hotels.csv")

2.9 Download table from the internet

This code scrapes a table from a Wikipedia page. It uses rvest to read the HTML content, html_table to extract tables, janitor::clean_names to clean up column names, and dplyr verbs (filter, mutate) to clean the data.

Code
library(tidyverse)
library(rvest)
library(janitor)

nba_wiki_url='https://en.wikipedia.org/wiki/List_of_NBA_champions'
  
nba_wiki_data001=nba_wiki_url %>% read_html() %>% html_table()

nba_wiki_data002=nba_wiki_data001[[2]] %>% clean_names()

nba_wiki_data003=nba_wiki_data002 %>%filter(!row_number() %in% c(1, 5)) %>% mutate(year=str_sub(year, 1, 4))

tail(nba_wiki_data003)
# A tibble: 6 × 8
  year  western_champion      coach result eastern_champion coach_2 finals_mvp_a
  <chr> <chr>                 <chr> <chr>  <chr>            <chr>   <chr>       
1 2020  Los Angeles Lakers (… Fran… 4–2    Miami Heat (5) … Erik S… LeBron James
2 2021  Phoenix Suns (2) (3,… Mont… 2–4    Milwaukee Bucks… Mike B… Giannis Ant…
3 2022  Golden State Warrior… Stev… 4–2    Boston Celtics … Ime Ud… Stephen Cur…
4 2023  Denver Nuggets (1) (… Mich… 4–1    Miami Heat (8) … Erik S… Nikola Jokić
5 2024  Dallas Mavericks (5)… Jaso… 1–4    Boston Celtics … Joe Ma… Jaylen Brown
6 2025  Oklahoma City Thunde… Mark… 4–3    Indiana Pacers … Rick C… Shai Gilgeo…
# ℹ 1 more variable:
#   mw_parser_output_tooltip_dotted_border_bottom_1px_dotted_cursor_help_ref <chr>

3 Handle errors

tryCatch allows you to handle errors gracefully. The code in the first block is executed. If an error occurs, the error function is called.

Code
tryCatch({
  1+who # This will cause an error because 'who' is not defined
},error=function(e){
  message(paste0('Here is some error:',e))
})

print('end of the code chunk')
[1] "end of the code chunk"

4 Condition with if/elif/else

This is a standard conditional statement. It checks a condition and executes different code blocks based on whether the condition is true or false.

Code
x <- -5
if(x > 10){
print("Non-negative number and better than 10")
} else if (x > 0) {
  print("Non-negative number and better than 0")
} else {
print("Negative number")
}
[1] "Negative number"

5 Loops

5.1 for Loop

A for loop iterates over a sequence of values.

Code
for (x in 1:4) {
  print(x)
}
[1] 1
[1] 2
[1] 3
[1] 4

break exits the loop prematurely.

Code
for (x in 1:6) {
  if (x == 4) {break}
  print(x)
}
[1] 1
[1] 2
[1] 3

next skips the current iteration and proceeds to the next one.

Code
for (x in 1:6) {
  if (x == 4) {next}
  print(x)
}
[1] 1
[1] 2
[1] 3
[1] 5
[1] 6

5.2 Using map() function for loops

The map functions from the purrr package (part of tidyverse) provide a more functional approach to iteration. map returns a list, map_dbl returns a numeric vector, and map_chr returns a character vector.

Code
library(tidyverse)
Code
map(
  1:3, 
    \(x) x+2
  )
[[1]]
[1] 3

[[2]]
[1] 4

[[3]]
[1] 5
Code
map_dbl(
  1:3, 
    \(x) x+2
  )
[1] 3 4 5
Code
map_chr(
  1:3, 
    \(x) x+2
  )
[1] "3.000000" "4.000000" "5.000000"

5.3 Error handling in a for loop: printing out errors

You can use tryCatch inside a loop to handle errors on a per-iteration basis.

Code
stuff <- list(12, 9, 2, "cat", 25, 10, "bird")

loop_num=0
for (i in 1:6) {
  loop_num=loop_num+1
  tryCatch (print(1+i),
           error = function(e){
           message(paste("An error occurred for loop num", loop_num,":\n"), e)
         })
}
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7

5.4 while Loop

A while loop continues as long as a condition is true.

Code
i <- 1
while (i < 6) {
  print(i)
  i <- i + 1
}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

5.5 Error handling in a while loop: retrying until the error is gone

This example shows a while loop that attempts to run code that causes an error. The tryCatch block catches the error and modifies a variable, allowing the loop to eventually terminate.

Code
i=0
a=0
while (i < 4) {
  a=a+1
  print(i)
  tryCatch({
  asdfgaergae5gh5hae5h # This will cause an error
    i=i+1
  },error = function(msg){print('eeeeeeee')
    i=i-1
    print(paste0("new i : ",i))
   
    })
   if(a>10){break} # Safety break to prevent an infinite loop
  }
[1] 0
[1] "eeeeeeee"
[1] "new i : -1"
[1] 0
[1] "eeeeeeee"
[1] "new i : -1"
[1] 0
[1] "eeeeeeee"
[1] "new i : -1"
[1] 0
[1] "eeeeeeee"
[1] "new i : -1"
[1] 0
[1] "eeeeeeee"
[1] "new i : -1"
[1] 0
[1] "eeeeeeee"
[1] "new i : -1"
[1] 0
[1] "eeeeeeee"
[1] "new i : -1"
[1] 0
[1] "eeeeeeee"
[1] "new i : -1"
[1] 0
[1] "eeeeeeee"
[1] "new i : -1"
[1] 0
[1] "eeeeeeee"
[1] "new i : -1"
[1] 0
[1] "eeeeeeee"
[1] "new i : -1"

6 Functions

6.1 Without Arguments

This defines and calls a simple function that takes no arguments.

Code
my_function <- function() { 
  print("Hello World!")
}

my_function()
[1] "Hello World!"

6.2 With Arguments

This function takes one argument, x, and returns its value plus 10.

Code
adding_ten <- function(x) { 
  a=x+10
  return(a)
}

adding_ten(5)
[1] 15

6.3 With default Arguments

This function has a default value for x. If the function is called without an argument for x, the default value of 10 is used.

Code
adding_ten <- function(x=10) { 
  a=x+10
  return(a)
}

adding_ten()
[1] 20

6.4 Check function Arguments

The args() function displays the arguments of a function.

Code
args(adding_ten)
function (x = 10) 
NULL

6.5 Warning in function

The warning() function issues a warning message without stopping the execution of the function.

Code
adding_ten <- function(x=10) { 
  a=x+10
  if(a>50){
    warning('its better than 50')
  }
  return(a)
}

adding_ten(100)
[1] 110

6.6 Stop in function

The stop() function halts the execution of the function and prints an error message.

Code
adding_ten <- function(x=10) { 
  a=x+10
  if(a>50){
    stop('its better than 50')
  }
  return(a)
}

6.7 Use try to bypass errors

The try() function is a simplified version of tryCatch. It runs an expression and if an error occurs, it returns an object of class try-error but allows the script to continue.

Code
try(adding_ten(100))
[1] 110
Code
5+10
[1] 15

7 Program running time

This code measures the time it takes to run a piece of code by recording the start and end times.

Code
start_time=Sys.time()

v=matrix(1:100000000)
c=v*v

end_time=Sys.time()

end_time-start_time
Time difference of 0.6208079 secs

8 Packages

8.1 Install R package

8.1.1 Install from CRAN

install.packages() is the standard way to install packages from CRAN.

Code
install.packages('dplyr',repos = "http://cran.us.r-project.org")

8.1.2 Install old version from CRAN

The remotes package allows you to install specific versions of packages.

Code
require(remotes)
install_version("plotly", version = "4.10.2")

8.1.3 Install from Github

The pak package can install packages directly from GitHub repositories.

Code
pak::pkg_install("tidymodels/learntidymodels")

8.2 Check one package version

packageVersion() returns the version of the specified package.

Code
packageVersion("tidyverse")
[1] '2.0.0'

8.3 Check package location

pak::pkg_status() provides detailed information about a package, including its installation path.

Code
pak::pkg_status("ggplot2") |> t()
                  96                                                                                                                
library           "/Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library"                                            
package           "ggplot2"                                                                                                         
version           "3.5.2"                                                                                                           
title             "Create Elegant Data Visualisations Using the Grammar of Graphics"                                                
depends           "R (>= 3.5)"                                                                                                      
imports           "cli, glue, grDevices, grid, gtable (>= 0.1.1), isoband,\n        lifecycle (> 1.0.1), MASS, mgcv, rl" [truncated]
license           "MIT + file LICENSE"                                                                                              
needscompilation  FALSE                                                                                                             
repository        "CRAN"                                                                                                            
built             "R 4.5.0; ; 2025-04-09 11:23:25 UTC; unix"                                                                        
linkingto         "NA"                                                                                                              
suggests          "covr, dplyr, ggplot2movies, hexbin, Hmisc, knitr, mapproj,\n        maps, multcomp, munsell, nlme, p" [truncated]
remotetype        "standard"                                                                                                        
remotepkgref      "ggplot2"                                                                                                         
remoteref         "ggplot2"                                                                                                         
remoterepos       "https://cran.rstudio.com/"                                                                                       
remotepkgplatform "aarch64-apple-darwin20"                                                                                          
remotesha         "3.5.2"                                                                                                           
priority          "NA"                                                                                                              
enhances          "sp"                                                                                                              
remotehost        "NA"                                                                                                              
remoterepo        "NA"                                                                                                              
remoteusername    "NA"                                                                                                              
remotesubdir      "NA"                                                                                                              
md5sum            "NA"                                                                                                              
platform          "*"                                                                                                               
biocviews         "NA"                                                                                                              
sysreqs           "NA"                                                                                                              
ref               "installed::/Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library/ggplot2"                         
type              "installed"                                                                                                       
status            "OK"                                                                                                              
rversion          "R 4.5.0"                                                                                                         
sources           character,0                                                                                                       
repotype          "cran"                                                                                                            
deps              tbl,5                                                                                                             

8.4 Check package dependencies

pak::pkg_deps_tree() shows a tree of the dependencies for a given package.

Code
pak::pkg_deps_tree("tibble")
tibble 3.3.0 ✨🔧 ⬇ (692.99 kB)
├─cli 3.6.5 ✨🔧
├─lifecycle 1.0.4 ✨
│ ├─cli
│ ├─glue 1.8.0 ✨🔧
│ └─rlang 1.1.6 ✨🔧
├─magrittr 2.0.3 ✨🔧
├─pillar 1.10.2 ✨
│ ├─cli
│ ├─glue
│ ├─lifecycle
│ ├─rlang
│ ├─utf8 1.2.6 ✨🔧 ⬇ (209.74 kB)
│ └─vctrs 0.6.5 ✨🔧
│   ├─cli
│   ├─glue
│   ├─lifecycle
│   └─rlang
├─pkgconfig 2.0.3 ✨
├─rlang
└─vctrs

Key:  ✨ new |  ⬇ download | 🔧 compile

8.5 Check all installed packages

installed.packages() returns a matrix of all installed packages. This code filters it and displays it in an interactive table using gt.

Code
library(gt)
ip = as.data.frame(installed.packages()[,c(1,3:4)])
ip = ip[is.na(ip$Priority),1:2,drop=FALSE]
ip |> gt() |> opt_interactive()

8.6 Check currently loaded packages

.packages() returns a character vector of the packages that are currently loaded in your R session.

Code
library(dplyr)
installed_package = as.data.frame(installed.packages()[,c(1,3:4)])
installed_package = installed_package[is.na(installed_package$Priority),1:2,drop=FALSE]
installed_package |> filter(Package %in% (.packages()))|> gt() |> opt_interactive()

8.7 List all packages on CRAN

available.packages() returns a matrix of all packages available on CRAN.

Code
cran_package_num=available.packages(repos = "http://cran.us.r-project.org") |> as.data.frame()

8.8 Check local installed old packages compared with CRAN

old.packages() checks for installed packages that have newer versions available on CRAN.

Code
old.packages(repos = "http://cran.us.r-project.org")
           Package     
curl       "curl"      
data.table "data.table"
Deriv      "Deriv"     
duckdb     "duckdb"    
evaluate   "evaluate"  
ggpubr     "ggpubr"    
infer      "infer"     
keyring    "keyring"   
plotly     "plotly"    
quantmod   "quantmod"  
shiny      "shiny"     
           LibPath                                                               
curl       "/Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library"
data.table "/Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library"
Deriv      "/Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library"
duckdb     "/Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library"
evaluate   "/Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library"
ggpubr     "/Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library"
infer      "/Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library"
keyring    "/Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library"
plotly     "/Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library"
quantmod   "/Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library"
shiny      "/Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library"
           Installed Built   ReposVer
curl       "6.3.0"   "4.5.0" "6.4.0" 
data.table "1.17.4"  "4.5.0" "1.17.6"
Deriv      "4.1.6"   "4.5.0" "4.2.0" 
duckdb     "1.3.0"   "4.5.0" "1.3.1" 
evaluate   "1.0.3"   "4.5.0" "1.0.4" 
ggpubr     "0.6.0"   "4.5.0" "0.6.1" 
infer      "1.0.8"   "4.5.0" "1.0.9" 
keyring    "1.4.0"   "4.5.0" "1.4.1" 
plotly     "4.10.4"  "4.5.0" "4.11.0"
quantmod   "0.4.27"  "4.5.0" "0.4.28"
shiny      "1.10.0"  "4.5.0" "1.11.0"
           Repository                                
curl       "http://cran.us.r-project.org/src/contrib"
data.table "http://cran.us.r-project.org/src/contrib"
Deriv      "http://cran.us.r-project.org/src/contrib"
duckdb     "http://cran.us.r-project.org/src/contrib"
evaluate   "http://cran.us.r-project.org/src/contrib"
ggpubr     "http://cran.us.r-project.org/src/contrib"
infer      "http://cran.us.r-project.org/src/contrib"
keyring    "http://cran.us.r-project.org/src/contrib"
plotly     "http://cran.us.r-project.org/src/contrib"
quantmod   "http://cran.us.r-project.org/src/contrib"
shiny      "http://cran.us.r-project.org/src/contrib"

8.9 Update all installed packages

update.packages() updates all installed packages to their latest versions.

Code
update.packages(ask = FALSE, checkBuilt = TRUE)

8.10 Check package install location

.libPaths() shows the library paths where R packages are installed.

Code
.libPaths()
[1] "/Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library"

9 Version control

The renv package helps manage project-specific package libraries, making your projects more reproducible.

9.1 Initialize renv and create renv.lock with currently loaded packages

renv::init() initializes renv in a project, creating a private library and a renv.lock file that records the package versions used.

Code
renv::init()

9.2 Update lock file

renv::snapshot() updates the renv.lock file to reflect the current state of the project’s library.

Code
renv::snapshot()

9.3 Make all current package versions back to the renv list

renv::restore() restores the project library to the state recorded in renv.lock.

Code
renv::restore()

10 Stop when code runs too long

The withTimeout() function from the R.utils package runs an expression and stops it if it exceeds the specified timeout.

Code
library(R.utils)
foo <- function() {
  print("Tic")
  for (kk in 1:100) {
    print(kk)
    Sys.sleep(0.1)
  }
  print("Tac")
}

withTimeout({
  foo()
}, timeout = 1.5, onTimeout = "warning")
[1] "Tic"
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
[1] 11
[1] 12
[1] 13
[1] 14
[1] 15
[1] 16
[1] 17
[1] 18
[1] 19
NULL

11 Check website connection

The system() function can run shell commands. Here, it uses ping to check if a website is reachable.

Code
url='www.bing.com'
connect_result=system(paste0('ping -c 1 ',url))
connect_result
[1] 2

12 Using Python

The reticulate package allows you to call Python from within R.

12.1 Select Python version

reticulate::py_available() checks if Python is available, and reticulate::py_config() shows the Python configuration that reticulate is using.

Code
reticulate::py_available()
[1] FALSE
Code
reticulate::py_config()
python:         /Users/jinchaoduan/.cache/uv/archive-v0/uSMUkHzA-pLqYxZe5zVrj/bin/python3
libpython:      /Users/jinchaoduan/.local/share/uv/python/cpython-3.11.11-macos-aarch64-none/lib/libpython3.11.dylib
pythonhome:     /Users/jinchaoduan/.cache/uv/archive-v0/uSMUkHzA-pLqYxZe5zVrj:/Users/jinchaoduan/.cache/uv/archive-v0/uSMUkHzA-pLqYxZe5zVrj
virtualenv:     /Users/jinchaoduan/.cache/uv/archive-v0/uSMUkHzA-pLqYxZe5zVrj/bin/activate_this.py
version:        3.11.11 (main, Mar 11 2025, 17:41:13) [Clang 20.1.0 ]
numpy:          /Users/jinchaoduan/.cache/uv/archive-v0/uSMUkHzA-pLqYxZe5zVrj/lib/python3.11/site-packages/numpy
numpy_version:  2.3.1

NOTE: Python version was forced by py_require()

12.2 Run Python in R

source_python() executes a Python script and makes its objects available in the R environment.

Code
source_python("flights.py")
Code
sessionInfo()
sessionInfo()
R version 4.5.1 (2025-06-13)
Platform: aarch64-apple-darwin20
Running under: macOS Sequoia 15.5

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.1

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Asia/Shanghai
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] R.utils_2.13.0    R.oo_1.27.1       R.methodsS3_1.8.2 gt_1.0.0         
 [5] janitor_2.2.1     rvest_1.0.4       lubridate_1.9.4   forcats_1.0.0    
 [9] stringr_1.5.1     dplyr_1.1.4       purrr_1.0.4       readr_2.1.5      
[13] tidyr_1.3.1       tibble_3.3.0      ggplot2_3.5.2     tidyverse_2.0.0  

loaded via a namespace (and not attached):
 [1] gtable_0.3.6       xfun_0.52          htmlwidgets_1.6.4  websocket_1.4.4   
 [5] processx_3.8.6     lattice_0.22-7     callr_3.7.6        tzdb_0.5.0        
 [9] vctrs_0.6.5        tools_4.5.1        pak_0.9.0          crosstalk_1.2.1   
[13] ps_1.9.1           generics_0.1.4     curl_6.3.0         pkgconfig_2.0.3   
[17] Matrix_1.7-3       RColorBrewer_1.1-3 lifecycle_1.0.4    compiler_4.5.1    
[21] farver_2.1.2       chromote_0.5.1     snakecase_0.11.1   htmltools_0.5.8.1 
[25] sass_0.4.10        yaml_2.3.10        later_1.4.2        pillar_1.10.2     
[29] tidyselect_1.2.1   digest_0.6.37      stringi_1.8.7      rprojroot_2.0.4   
[33] fastmap_1.2.0      grid_4.5.1         here_1.0.1         cli_3.6.5         
[37] magrittr_2.0.3     utf8_1.2.6         withr_3.0.2        reactR_0.6.1      
[41] scales_1.4.0       promises_1.3.3     timechange_0.3.0   rmarkdown_2.29    
[45] httr_1.4.7         reticulate_1.42.0  png_0.1-8          hms_1.1.3         
[49] evaluate_1.0.3     knitr_1.50         rlang_1.1.6        Rcpp_1.0.14       
[53] glue_1.8.0         reactable_0.4.4    xml2_1.3.8         rstudioapi_0.17.1 
[57] jsonlite_2.0.0     R6_2.6.1          
Back to top