General Guide

Copy & use this format

  1. Put Exercise number and question numbers.
  2. Put questions & your solutions all in code chunk
  3. Put Description if the question request or if you need to add it after the code chunk.

Never spend time to decorate this report!

Special Notes

  1. Show output using cat() or print() if the function doesn’t show
  2. Use round() or signif()to show appropriate decimal point.
    • If it is not necessary, don’t exceed 2 decimal points for printing.


Common Libraries & Variables of this Rmd file

You can organize them clearly using this section: optional

# libraries('librarian') # smart install & loading a pkg: shelf() unshelf()
# library(devtools) # pkg development, install_github()
# library(dplyr) # data wrangling, %>%
# library(tidyverse) # data wrangling set:'dplyr', 'ggplot2' etc
# Rafalib pkgs
# library('downloader') # for download
# library('dagdata') # for 3 mice data 
# libraries('dslabs') # for murders, gene expression, 
# libraries('rafalib') # for mypar, maplot, popsd, popvar, sboxplot, shist, splot, ssplit

Q1 & Q2

## Q1. Install the following packages. 
# 'librarian', 'devtools', 'tidyverse' for general functions. 
# 'downloader', 'dagdata' for DAL book specific functions

if (!"librarian" %in% installed.packages()) install.packages("librarian");
if (!"devtools" %in% installed.packages()) install.packages("devtools");
if (!"tidyverse" %in% installed.packages()) install.packages("tidyverse");
if (!"downloader" %in% installed.packages()) install.packages("downloader");
if (!"dagdata" %in% installed.packages()) install.packages("dagdata");
if (!"rafalib" %in% installed.packages()) install.packages("rafalib");
if (!"dslabs" %in% installed.packages()) install.packages("dslabs");

## Q2. Load, detach, unload "downloader".  
## show sessionInfo() in each step

sessionInfo()
## R version 4.4.1 (2024-06-14 ucrt)
## Platform: x86_64-w64-mingw32/x64
## Running under: Windows 10 x64 (build 19045)
## 
## Matrix products: default
## 
## 
## locale:
## [1] LC_COLLATE=Korean_Korea.utf8  LC_CTYPE=Korean_Korea.utf8   
## [3] LC_MONETARY=Korean_Korea.utf8 LC_NUMERIC=C                 
## [5] LC_TIME=Korean_Korea.utf8    
## 
## time zone: Asia/Seoul
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## loaded via a namespace (and not attached):
##  [1] digest_0.6.37     R6_2.5.1          fastmap_1.2.0     xfun_0.47        
##  [5] cachem_1.1.0      knitr_1.48        htmltools_0.5.8.1 rmarkdown_2.28   
##  [9] lifecycle_1.0.4   cli_3.6.3         sass_0.4.9        jquerylib_0.1.4  
## [13] compiler_4.4.1    rstudioapi_0.16.0 tools_4.4.1       evaluate_0.24.0  
## [17] bslib_0.8.0       yaml_2.3.10       rlang_1.1.4       jsonlite_1.8.8
library(downloader)
sessionInfo()
## R version 4.4.1 (2024-06-14 ucrt)
## Platform: x86_64-w64-mingw32/x64
## Running under: Windows 10 x64 (build 19045)
## 
## Matrix products: default
## 
## 
## locale:
## [1] LC_COLLATE=Korean_Korea.utf8  LC_CTYPE=Korean_Korea.utf8   
## [3] LC_MONETARY=Korean_Korea.utf8 LC_NUMERIC=C                 
## [5] LC_TIME=Korean_Korea.utf8    
## 
## time zone: Asia/Seoul
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] downloader_0.4
## 
## loaded via a namespace (and not attached):
##  [1] digest_0.6.37     R6_2.5.1          fastmap_1.2.0     xfun_0.47        
##  [5] cachem_1.1.0      knitr_1.48        htmltools_0.5.8.1 rmarkdown_2.28   
##  [9] lifecycle_1.0.4   cli_3.6.3         sass_0.4.9        jquerylib_0.1.4  
## [13] compiler_4.4.1    rstudioapi_0.16.0 tools_4.4.1       evaluate_0.24.0  
## [17] bslib_0.8.0       yaml_2.3.10       rlang_1.1.4       jsonlite_1.8.8
detach('package:downloader')
sessionInfo()
## R version 4.4.1 (2024-06-14 ucrt)
## Platform: x86_64-w64-mingw32/x64
## Running under: Windows 10 x64 (build 19045)
## 
## Matrix products: default
## 
## 
## locale:
## [1] LC_COLLATE=Korean_Korea.utf8  LC_CTYPE=Korean_Korea.utf8   
## [3] LC_MONETARY=Korean_Korea.utf8 LC_NUMERIC=C                 
## [5] LC_TIME=Korean_Korea.utf8    
## 
## time zone: Asia/Seoul
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## loaded via a namespace (and not attached):
##  [1] digest_0.6.37     R6_2.5.1          fastmap_1.2.0     xfun_0.47        
##  [5] cachem_1.1.0      knitr_1.48        htmltools_0.5.8.1 rmarkdown_2.28   
##  [9] lifecycle_1.0.4   cli_3.6.3         downloader_0.4    sass_0.4.9       
## [13] jquerylib_0.1.4   compiler_4.4.1    rstudioapi_0.16.0 tools_4.4.1      
## [17] evaluate_0.24.0   bslib_0.8.0       yaml_2.3.10       rlang_1.1.4      
## [21] jsonlite_1.8.8
library(downloader)
detach('package:downloader', unload=T)
sessionInfo()
## R version 4.4.1 (2024-06-14 ucrt)
## Platform: x86_64-w64-mingw32/x64
## Running under: Windows 10 x64 (build 19045)
## 
## Matrix products: default
## 
## 
## locale:
## [1] LC_COLLATE=Korean_Korea.utf8  LC_CTYPE=Korean_Korea.utf8   
## [3] LC_MONETARY=Korean_Korea.utf8 LC_NUMERIC=C                 
## [5] LC_TIME=Korean_Korea.utf8    
## 
## time zone: Asia/Seoul
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## loaded via a namespace (and not attached):
##  [1] digest_0.6.37     R6_2.5.1          fastmap_1.2.0     xfun_0.47        
##  [5] cachem_1.1.0      knitr_1.48        htmltools_0.5.8.1 rmarkdown_2.28   
##  [9] lifecycle_1.0.4   cli_3.6.3         sass_0.4.9        jquerylib_0.1.4  
## [13] compiler_4.4.1    rstudioapi_0.16.0 tools_4.4.1       evaluate_0.24.0  
## [17] bslib_0.8.0       yaml_2.3.10       rlang_1.1.4       jsonlite_1.8.8

Description

  1. sessionInfo results look different from the results in .R file. Rmarkdown might load some namespaces for Knitr & Pandoc.
  2. Whendownloaderis attached on .Globalenv, digest is loaded via a namespace additionally.
  3. When downloaderis detatched, downloader moves to the namespace & digest remain on their namespace.
  4. detach with unload=T removes the downloader namespace but not the digest namespace.
  5. unloadNamespace(“digest”) removes digest too.

Q3

# Q3. Download "femaleMiceWeights.csv" inside the "genomicsclass/dagdata" on GitHub
librarian::shelf(downloader)
## 
##   The 'cran_repo' argument in shelf() was not set, so it will use
##   cran_repo = 'https://cran.r-project.org' by default.
## 
##   To avoid this message, set the 'cran_repo' argument to a CRAN
##   mirror URL (see https://cran.r-project.org/mirrors.html) or set
##   'quiet = TRUE'.
## 1) Check or set your working directory
getwd();
## [1] "C:/Users/yejin/BiS335"
## 2) Find the options of download() in "downloader" pkg 
# by using ?, help(), & args() 
?download
## httpd 도움말 서버를 시작합니다 ...
##  완료
?downloader::download
args(downloader::download)
## function (url, ...) 
## NULL
## 3) Check apropos("download") 
apropos("^download")
## [1] "download"          "download.file"     "download.packages"
#### Get help for download.file 
?download.file
args(download.file)
## function (url, destfile, method, quiet = FALSE, mode = "w", cacheOK = TRUE, 
##     extra = getOption("download.file.extra"), headers = NULL, 
##     ...) 
## NULL
## 4) Download the file with the following arguments:
# download(url, destfile=file1); 
url  <- "https://raw.githubusercontent.com/genomicsclass/dagdata/master/inst/extdata/femaleMiceWeights.csv"

file1 <- "femaleMiceWeights_github.csv" # filename to be saved

## 5) Check if the file is in your working directory 
# using list.files() & file.exists() 
# before & after download the file.

list.files()
## [1] "HW1-1.1-v24.Rmd"
file.exists(file1)
## [1] FALSE
download(url , destfile=file1)

list.files()
## [1] "femaleMiceWeights_github.csv" "HW1-1.1-v24.Rmd"
file.exists(file1)
## [1] TRUE

Description

  1. Help page says downloader is a wrapper for download.file.
  2. To know the arguments, we need to see the help of download.file
  3. apropos shows there are 3 objects having “download” in their name.

Q4, Q5, & Q6

# Q4. Install 'dagdata' pkg from GitHub at "genomicsclass/dagdata" 
## You need "devtools" pkg to use install_github()
if(!require(dagdata)) {install_github("genomicsclass/dagdata")  }
## 필요한 패키지를 로딩중입니다: dagdata
# More efficiently use shelf & unshelf instead of library & detach
librarian::shelf(genomicsclass/dagdata);
## 
##   The 'cran_repo' argument in shelf() was not set, so it will use
##   cran_repo = 'https://cran.r-project.org' by default.
## 
##   To avoid this message, set the 'cran_repo' argument to a CRAN
##   mirror URL (see https://cran.r-project.org/mirrors.html) or set
##   'quiet = TRUE'.
# Q5. Find the Path of the file
(dag_dir <-  system.file(package="dagdata"))
## [1] "C:/Users/sypark/AppData/Local/R/win-library/4.4/dagdata"
list.files(file.path(dag_dir))  
## [1] "data"        "DESCRIPTION" "extdata"     "help"        "html"       
## [6] "Meta"        "NAMESPACE"   "script"
list.files(file.path(dag_dir, "extdata"))
## [1] "admissions.csv"               "astronomicalunit.csv"        
## [3] "babies.txt"                   "femaleControlsPopulation.csv"
## [5] "femaleMiceWeights.csv"        "mice_pheno.csv"              
## [7] "msleep_ggplot2.csv"           "README"                      
## [9] "spider_wolff_gorb_2013.csv"
# Q6. Assign the "full path & file name" to file2
(file2 <- system.file(package="dagdata", "extdata", "femaleMiceWeights.csv"))
## [1] "C:/Users/sypark/AppData/Local/R/win-library/4.4/dagdata/extdata/femaleMiceWeights.csv"

Description

  1. “If(!require(pkg)) function” tries to load the pkg, returns TRUE if it can’t load the pkg which means it is not installed.
  • Using this, we can install & load a pkg if it isn’t installed.
  • and the pkg is loaded if it is installed already.
  1. system.file identifies “full path of the pkg folder.
  2. (function) coerce to print the outputs
  3. file.path identifies “full path of a file or a folder”
  4. list.files lists the folders & files in the path

Q7 & Q8

# Q7. Load file1 and file2 

(dat1 <- read.csv(file1)) 
(dat2 <- read.csv(file2)) 
## Different ways
dat1<- read.csv(url); 
dat2 <- read.csv(system.file(package = 'dagdata', 'extdata', 'femaleMiceWeights.csv'))


# Q8. Compare them using View() & identical().

View(dat1) 
View(dat2)  
identical(dat1, dat2)
## [1] TRUE
##  Set the data name to "dat" after you confirm the identity
dat <- dat1

Description

  1. “read.csv” read csv file to a R workspace.
  2. “a <- b” assign b to a.
  3. identical(a,b) compares two data & returns TRUE or FALSE
  4. Files from GitHub & dagdata are same.