Author

Tony Duan

1 R version

Code
version
               _                           
platform       aarch64-apple-darwin20      
arch           aarch64                     
os             darwin20                    
system         aarch64, darwin20           
status                                     
major          4                           
minor          4.1                         
year           2024                        
month          06                          
day            14                          
svn rev        86737                       
language       R                           
version.string R version 4.4.1 (2024-06-14)
nickname       Race for Your Life          

2 work with file

2.1 get current directory

Code
getwd()

2.2 get all file name under current directory

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

2.3 get all file name under currrents parent directory

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

2.4 get file info

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

2.5 create folder

Code
dir.create('testing_folder')

2.6 delete folder/file

Code
file.remove('testing_folder')

2.7 copy file

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

2.8 download file from internet

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 internet

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 2019  Golden State Warrior… Stev… 2–4    Toronto Raptors… Nick N… Kawhi Leona…
2 2020  Los Angeles Lakers (… Fran… 4–2    Miami Heat (5) … Erik S… LeBron James
3 2021  Phoenix Suns (2) (3,… Mont… 2–4    Milwaukee Bucks… Mike B… Giannis Ant…
4 2022  Golden State Warrior… Stev… 4–2    Boston Celtics … Ime Ud… Stephen Cur…
5 2023  Denver Nuggets (1) (… Mich… 4–1    Miami Heat (8) … Erik S… Nikola Jokić
6 2024  Dallas Mavericks (5)… Jaso… 1–4    Boston Celtics … Joe Ma… Jaylen Brown
# ℹ 1 more variable:
#   mw_parser_output_tooltip_dotted_border_bottom_1px_dotted_cursor_help_ref <chr>

3 handle error

using tryCatch to continues the code chunk.

Code
tryCatch({
  1+who
},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

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 Loop

5.1 for Loop

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

with break statement

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

with next statement

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

5.2 Error handling on for Loop:print out error

Code
stuff <- list(12, 9, 2, "cat", 25, 10, "bird")
#stuff
Code
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.3 while Loop

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

with break statement

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

with next statement

Code
i =1

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

5.4 Error handling on whie Loop: try when the error gone

Code
i=0
a=0
while (i < 4) {
  a=a+1
  print(i)
  tryCatch({
  asdfgaergae5gh5hae5h
    i=i+1
  },error = function(msg){print('eeeeeeee')
    i=i-1
    print(paste0("new i : ",i))
   
    })
   if(a>10){break}
  }
[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 function

6.1 without Arguments

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

my_function()
[1] "Hello World!"

6.2 with Arguments

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

adding_ten(5)
[1] 15

6.3 with default Arguments

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

#if not define x, then x=10

adding_ten()
[1] 20

6.4 check function Arguments

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

Many functions exhibit variadic behavior. That is, they can accept any num- ber of arguments, and it’s up to the user to decide how many arguments to provide. The functions c, data.frame, and list are all like this. When you call a function like data.frame, you can specify any number of members as arguments.

Code
args(data.frame)
function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, 
    fix.empty.names = TRUE, stringsAsFactors = FALSE) 
NULL

6.5 warning in function

print out warning

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

6.6 stop in function

print out stop error message

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

6.7 use try to by pass error

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

7 program running time

Code
start_time=Sys.time()

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

end_time=Sys.time()
Code
start_time
[1] "2025-03-11 19:47:01 CST"
Code
end_time
[1] "2025-03-11 19:47:02 CST"
Code
end_time-start_time
Time difference of 0.613564 secs

8 Package

8.1 install R package

8.1.1 install from Cran

99% of the time will install pacakge from The Comprehensive R Archive Network(cran).https://cran.r-project.org/

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

8.1.2 install old version from Cran

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

8.1.3 install from Github

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

8.1.4 install from .tar.gz

Code
install.packages("https://cran.r-project.org/src/contrib/Archive/maptools/maptools_1.1-8.tar.gz")

8.1.5 install from Bioconductor

Code
pak::pkg_install("text2vec")

8.1.6 install from local

Code
pak::local_install("cli")

8.2 check one package version

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

8.3 check pacakge relationship

Code
pak::pkg_deps_explain("tibble", "rlang")
tibble -> lifecycle -> rlang
tibble -> pillar -> lifecycle -> rlang
tibble -> pillar -> rlang
tibble -> pillar -> vctrs -> lifecycle -> rlang
tibble -> pillar -> vctrs -> rlang
tibble -> rlang
tibble -> vctrs -> lifecycle -> rlang
tibble -> vctrs -> rlang

8.4 check pacakge dependencies

Code
pak::pkg_deps_tree("tibble")
tibble 3.2.1 ✨🔧 ⬇ (688.89 kB)
├─fansi 1.0.6 ✨🔧 ⬇ (383.06 kB)
├─lifecycle 1.0.4 ✨ ⬇ (124.78 kB)
│ ├─cli 3.6.4 ✨🔧 ⬇ (1.45 MB)
│ ├─glue 1.8.0 ✨🔧 ⬇ (173.70 kB)
│ └─rlang 1.1.5 ✨🔧 ⬇ (1.90 MB)
├─magrittr 2.0.3 ✨🔧 ⬇ (233.52 kB)
├─pillar 1.10.1 ✨ ⬇ (657.46 kB)
│ ├─cli
│ ├─glue
│ ├─lifecycle
│ ├─rlang
│ ├─utf8 1.2.4 ✨🔧 ⬇ (206.91 kB)
│ └─vctrs 0.6.5 ✨🔧 ⬇ (1.89 MB)
│   ├─cli
│   ├─glue
│   ├─lifecycle
│   └─rlang
├─pkgconfig 2.0.3 ✨ ⬇ (18.45 kB)
├─rlang
└─vctrs

Key:  ✨ new |  ⬇ download | 🔧 compile

8.5 check all installed package

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 current loaded package

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

8.7 list all packages on cran

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

As 2025-03-11 ,there are total library 22161 on cran

Code
cran_pacakge_num|> gt() |> opt_interactive()

8.8 check local installed old package compare with cran

Code
old.packages(repos = "http://cran.us.r-project.org")
           Package     
httr2      "httr2"     
plotly     "plotly"    
quantreg   "quantreg"  
RcppTOML   "RcppTOML"  
readxl     "readxl"    
reticulate "reticulate"
units      "units"     
           LibPath                                                               
httr2      "/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library"
plotly     "/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library"
quantreg   "/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library"
RcppTOML   "/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library"
readxl     "/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library"
reticulate "/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library"
units      "/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library"
           Installed Built   ReposVer  
httr2      "1.1.0"   "4.4.1" "1.1.1"   
plotly     "4.10.2"  "4.4.1" "4.10.4"  
quantreg   "6.00"    "4.4.1" "6.1"     
RcppTOML   "0.2.2"   "4.4.0" "0.2.3"   
readxl     "1.4.4"   "4.4.1" "1.4.5"   
reticulate "1.41.0"  "4.4.1" "1.41.0.1"
units      "0.8-5"   "4.4.0" "0.8-6"   
           Repository                                
httr2      "http://cran.us.r-project.org/src/contrib"
plotly     "http://cran.us.r-project.org/src/contrib"
quantreg   "http://cran.us.r-project.org/src/contrib"
RcppTOML   "http://cran.us.r-project.org/src/contrib"
readxl     "http://cran.us.r-project.org/src/contrib"
reticulate "http://cran.us.r-project.org/src/contrib"
units      "http://cran.us.r-project.org/src/contrib"

8.9 update all installed package

Update packages which are currently out-of-date. Currently supports CRAN, Bioconductor, other CRAN-like repositories, GitHub, GitLab, Git, and BitBucket.

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

8.10 check package install loaciton

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

8.11 uninstall pacakge

Code
remove.packages('xxxxx')

9 sleep

The time interval to suspend execution for, in seconds.

Code
Sys.sleep(3)

10 open two R session on Mac

Code
Terminal
open -n /Applications/RStudio.app

11 version control

The renv package helps you create reproducible environments for your R projects

11.1 inital renv and create renv.lock with current loaded pacakge

Code
renv::init()

11.2 show all installed pacakge

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

11.3 show all installed pacakge and loaded pacakge

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

11.4 when using renv and install new pakcage

Code
# it will not work
# library(lubridate)

11.5 install new package with renv::install

Code
renv::install('lubridate')
Code
library(lubridate)

11.6 check current package and renv package

Code
renv::status()

11.7 update lock file

Code
renv::snapshot()

11.8 check status again

Code
renv::status()

11.9 make all current pakcage version back to renv list

Code
renv::restore()

11.10 update all pakcage in renv. recommand do it once a year to keep package updated.

Update packages which are currently out-of-date. Currently supports CRAN, Bioconductor, other CRAN-like repositories, GitHub, GitLab, Git, and BitBucket.

Code
renv::update()

11.11 update renv itself only

Code
renv::upgrade()

11.12 close renv in a project

Code
renv::deactivate()

11.13 re enable renv in a project

Code
renv::activate()

12 reccord code run time

Code
start_time=Sys.time()

for (kk in 1:5) {
    print(kk)
    Sys.sleep(1)
  }
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
Code
end_time=Sys.time()
time_used=end_time-start_time
time_used
Time difference of 5.020004 secs

13 stop when code run too long

stop the count down at 1.5 sec

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
NULL

14 check website connection

when connect_result= 2 it means connection is ok

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

when connect_result= 68 it means connection is fail

Code
url='www.bingxxxxx.com'
connect_result=system(paste0('ping -c 1 ',url))
connect_result
[1] 68
Code
sessionInfo()
sessionInfo()
R version 4.4.1 (2024-06-14)
Platform: aarch64-apple-darwin20
Running under: macOS 15.3.1

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

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.0       R.methodsS3_1.8.2 gt_0.11.1        
 [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.2.1      ggplot2_3.5.1     tidyverse_2.0.0  

loaded via a namespace (and not attached):
 [1] gtable_0.3.6      xfun_0.51         htmlwidgets_1.6.4 websocket_1.4.2  
 [5] processx_3.8.6    callr_3.7.6       tzdb_0.4.0        vctrs_0.6.5      
 [9] tools_4.4.1       pak_0.8.0.1       crosstalk_1.2.1   ps_1.9.0         
[13] generics_0.1.3    curl_6.2.1        pkgconfig_2.0.3   lifecycle_1.0.4  
[17] compiler_4.4.1    munsell_0.5.1     chromote_0.4.0    snakecase_0.11.1 
[21] htmltools_0.5.8.1 sass_0.4.9        yaml_2.3.10       later_1.4.1      
[25] pillar_1.10.1     tidyselect_1.2.1  digest_0.6.37     stringi_1.8.4    
[29] fastmap_1.2.0     grid_4.4.1        colorspace_2.1-1  cli_3.6.4        
[33] magrittr_2.0.3    utf8_1.2.4        withr_3.0.2       reactR_0.6.1     
[37] scales_1.3.0      promises_1.3.2    timechange_0.3.0  rmarkdown_2.29   
[41] httr_1.4.7        hms_1.1.3         evaluate_1.0.3    knitr_1.49       
[45] rlang_1.1.5       Rcpp_1.0.14       glue_1.8.0        reactable_0.4.4  
[49] xml2_1.3.7        rstudioapi_0.17.1 jsonlite_1.9.1    R6_2.6.1         
Back to top