1.1 工作目录

Show the code
getwd()
#> [1] "D:/.GitHub/RDataScience/R_data_science"
# setwd("My\\Path")
# setwd("My/Path") # Equivalent

1.2 保存

Show the code
x <- "Hello world"
save(x,file = "data/intro/mydata.RData")
load("data/intro/mydata.RData")

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

1.3 工作空间

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

1.4 文件系统

1.4.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/read_write/multi-sheet-iris_data.xlsx"

dir(path = "data/",pattern = "\\.xlsx$", full.names = TRUE, recursive =T) # Equivalent
#> [1] "data/read_write/multi-sheet-iris_data.xlsx"

1.4.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.4.3 压缩文件

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

1.5 R包管理

1.5.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.5.2 更新

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

1.5.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"     "lubridate"  "forcats"    "stringr"   
#> [11] "dplyr"      "purrr"      "readr"      "tidyr"      "tibble"    
#> [16] "ggplot2"    "tidyverse"  "conflicted" "showtext"   "showtextdb"
#> [21] "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     lubridate_1.9.3  forcats_1.0.0   
#>  [5] stringr_1.5.1    dplyr_1.1.4      purrr_1.0.2      readr_2.1.5     
#>  [9] tidyr_1.3.1      tibble_3.2.1     ggplot2_3.5.1    tidyverse_2.0.0 
#> [13] conflicted_1.2.0 showtext_0.9-7   showtextdb_3.0   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      cli_3.6.3         rlang_1.1.4      
#> [17] munsell_0.5.1     withr_3.0.2       cachem_1.1.0      tools_4.4.1      
#> [21] tzdb_0.4.0        memoise_2.0.1     colorspace_2.1-1  vctrs_0.6.5      
#> [25] R6_2.5.1          lifecycle_1.0.4   htmlwidgets_1.6.4 pkgconfig_2.0.3  
#> [29] pillar_1.9.0      gtable_0.3.6      glue_1.8.0        xfun_0.49        
#> [33] tidyselect_1.2.1  rstudioapi_0.17.1 knitr_1.48        htmltools_0.5.8.1
#> [37] rmarkdown_2.29    compiler_4.4.1

1.5.4 删除

Show the code
remove.packages("airway")

1.6 全局选项

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

1.7 语言设置

Show the code
Sys.getenv()["HOMEPATH"]
#> HOMEPATH                \Users\WANGANLIN
Sys.getenv()["LANGUAGE"]
#> LANGUAGE                en
Sys.setenv(LANGUAGE = "en")