1.1 工作空间

Show the code
x=10
save(x,file = "mydata.RData")
load("mydata.RData")

my_model <- lm(hwy~ displ, data = mpg)
saveRDS(my_model, file = "my_model.rds")
readRDS("my_model.rds")
#> 
#> Call:
#> lm(formula = hwy ~ displ, data = mpg)
#> 
#> Coefficients:
#> (Intercept)        displ  
#>      35.698       -3.531

ls()
#> [1] "my_model" "x"
rm(list = ls())

1.2 工作目录

Show the code
getwd()
#> [1] "D:/_DataManager/GitHub/R/R_notes_version_0"


# setwd("My\\Path")
# setwd("My/Path") # Equivalent

1.3 文件系统

1.3.1 文件夹

Show the code
# 新建
dir.create("data/file_create_ccccccccccccccc")


# 删除
unlink("data/file_create_ccccccccccccccc", recursive = TRUE)



# 列出文件夹里的文件

list.files(path = "data/",pattern = "\\.xlsx$", full.names = TRUE, recursive =T)
#> [1] "data/deaths.xlsx"                        
#> [2] "data/deathsA5_F15.xlsx"                  
#> [3] "data/penguins.xlsx"                      
#> [4] "data/read_write/多个sheet-iris_data.xlsx"
#> [5] "data/students.xlsx"

dir(path = "data/",pattern = "\\.xlsx$", full.names = TRUE, recursive =T) # Equivalent
#> [1] "data/deaths.xlsx"                        
#> [2] "data/deathsA5_F15.xlsx"                  
#> [3] "data/penguins.xlsx"                      
#> [4] "data/read_write/多个sheet-iris_data.xlsx"
#> [5] "data/students.xlsx"

1.3.2 文件

Show the code
file.create("data/create.R")
#> [1] TRUE
file.remove("data/create.R")
#> [1] TRUE
system.file("data",package = "MatrixEQTL")
#> [1] ""

file.path(system.file("data",package = "MatrixEQTL"),"exprsData.txt")
#> [1] "/exprsData.txt"


# 复制
# file.copy("my_file.R", "my_copied_file.R")

1.3.3 压缩文件

Show the code
unzip("data/read_write/leadership.zip", exdir = "data/read_write/解压缩") 

1.4 R包管理

1.4.1 安装

Show the code
# CRAN 
install.packages("package_name")  

# R Forge

install.packages("MPAgenomics", repos = "http://R-Forge.R-project.org",
                 dependencies = TRUE)

# Bioconductor 
install.packages("BiocManager") 
BiocManager::install()  

# 本地 
install.packages("./GenomeInfoDbData_1.2.12.tar.gz", repos = NULL, type = "source")
install.packages("./GenomeInfoDbData_1.2.12.zip", repos = NULL, type = "binary")


# Github 
remotes::install_github("tidyverse/ggplot2")

1.4.2 更新

Show the code
# 更新
update.packages(ask = FALSE)  
remotes::update_packages()

1.4.3 检查

Show the code
# If FALSE, install the package
if (require("MASS")) install.packages("MASS")
Show the code

(.packages())
#>  [1] "stats"      "graphics"   "grDevices"  "utils"      "datasets"  
#>  [6] "writexl"    "readxl"     "patchwork"  "lubridate"  "forcats"   
#> [11] "stringr"    "dplyr"      "purrr"      "readr"      "tidyr"     
#> [16] "tibble"     "ggplot2"    "tidyverse"  "conflicted" "showtext"  
#> [21] "showtextdb" "sysfonts"   "methods"    "base"
sessionInfo()
#> R version 4.4.1 (2024-06-14 ucrt)
#> Platform: x86_64-w64-mingw32/x64
#> Running under: Windows 11 x64 (build 22631)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=Chinese (Simplified)_China.utf8 
#> [2] LC_CTYPE=Chinese (Simplified)_China.utf8   
#> [3] LC_MONETARY=Chinese (Simplified)_China.utf8
#> [4] LC_NUMERIC=C                               
#> [5] LC_TIME=Chinese (Simplified)_China.utf8    
#> 
#> time zone: Asia/Shanghai
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] writexl_1.5.1    readxl_1.4.3     patchwork_1.3.0  lubridate_1.9.3 
#>  [5] forcats_1.0.0    stringr_1.5.1    dplyr_1.1.4      purrr_1.0.2     
#>  [9] readr_2.1.5      tidyr_1.3.1      tibble_3.2.1     ggplot2_3.5.1   
#> [13] tidyverse_2.0.0  conflicted_1.2.0 showtext_0.9-7   showtextdb_3.0  
#> [17] sysfonts_0.8.9  
#> 
#> loaded via a namespace (and not attached):
#>  [1] utf8_1.2.4        generics_0.1.3    stringi_1.8.4     hms_1.1.3        
#>  [5] digest_0.6.37     magrittr_2.0.3    evaluate_1.0.1    grid_4.4.1       
#>  [9] timechange_0.3.0  fastmap_1.2.0     jsonlite_1.8.9    cellranger_1.1.0 
#> [13] fansi_1.0.6       scales_1.3.0      codetools_0.2-20  cli_3.6.3        
#> [17] rlang_1.1.4       munsell_0.5.1     withr_3.0.1       cachem_1.1.0     
#> [21] tools_4.4.1       tzdb_0.4.0        memoise_2.0.1     colorspace_2.1-1 
#> [25] vctrs_0.6.5       R6_2.5.1          lifecycle_1.0.4   htmlwidgets_1.6.4
#> [29] pkgconfig_2.0.3   pillar_1.9.0      gtable_0.3.5      glue_1.8.0       
#> [33] xfun_0.48         tidyselect_1.2.1  knitr_1.48        rstudioapi_0.17.0
#> [37] farver_2.1.2      htmltools_0.5.8.1 rmarkdown_2.28    compiler_4.4.1

1.4.4 删除

Show the code
remove.packages("airway")

1.5 全局选项

Show the code
getOption("timeout")
#> [1] 60
getOption("digits")
#> [1] 7
getOption("repos")
#>                        CRAN 
#> "https://cran.rstudio.com/"
getOption("BioC_mirror")
#> [1] "https://mirrors.tuna.tsinghua.edu.cn/bioconductor"

1.6 语言设置

Show the code
Sys.setenv(LANGUAGE = "en")