Chapter 2 R Language

x <- 100

2.1 Is there a way to obtain source code of non-exported function

2.2 Packages

2.2.1 sf

  • All the functions have the prefix st which means spatial and temporal.

2.2.2 Markdown

Rstudio has some particular templates for Markdown. When using knit it is not able to render adequately the markdown file. Then is better to use:

rmarkdown::render("my-file.Rmd")

2.2.3 Bookdown

git clone 
  • One chapter lives in ones R Markdown file 01-intro.Rmd or 01-intro.Rmd.
  • index.Rmd is the first file as usual in webpages.
  • _example.Rmd is skipped.

2.3 Submit a package

R CMD check --as-cran

2.4 Math labels

use expression with paste, bquote, and math notation

expression(paste(bold('a)'), beta = 1))
    

2.5 Sf and ggplot

geom_sf:

  • Use fill = NA when you want fill color.
  • By default, it uses datum 4326. You can change it using coord_sf. For example, you can set coord_sf(datam = 3857).

2.6 Rmd and knitr

knitr::opts_chunk$set(eval = FALSE) to not evaluate any R code.

2.7 Regular expresion

Lookahead assertion to find expressions that does not match. For example, all strings that does not start with meta:

x <- c("metadata", "file1", "file2", "mass1", "mass2")
grep("^(?!meta)", x, value = TRUE, perl = TRUE)
## [1] "file1" "file2" "mass1" "mass2"

2.8 Install blogdown

devtools::install_github(‘rstudio/blogdown’)

2.9 Crate github repositories

  • Create two repositories
    • sources
      • public, README, and R .gitignore, LICENSE
    • github.io
      • public, README, and , LICENSE

2.10 Setting up main rapository

Clone your web repository

git clone git@github.com:statistical-issues/statistical-issues-source.git

Set working directory on the created folder and do not create any new file

library(blogdown)
new_site('.', theme = 'digitalcraftsman/hugo-material-docs')
cd myweb
rm -r public

git add -A
git commit -m "hugo source files"
git push

https://git-scm.com/book/en/v2/Git-Tools-Submodules

git submodule add :statistical-issues/statistical-issues.github.io.git public git commit -am ‘added hugo web module’ git push

blogdown::build_site() touch .nojekyll git add -A git commit -m “built web”

git clone :statistical-issues/statistical-issues-source.git git submodule init git submodule update cd public git checkout master

2.11 How to contribute to a git repository with submodules

git clone --recursive https://github.com/chaconinc/MainProject
git clone --recursive git@github.com:statistical-issues/statistical-issues-source.git

get into your repository and to the public folder, this is where the source code for your web is located

cd statistical-issues-source/public
git checkout master

%% similar to git pull for a submodule %% git submodule update –remote DbConnector

%% git submodule update –remote –rebase

2.12 R packages

  • Adding vignettes
devtools::use_vignette("my-vignette")

2.13 R profiling

Rprof("profile.out", line.profiling=TRUE)
source("test_multiple_observations.R")
Rprof(NULL)
summaryRprof("profile.out", lines = "show")
CPUPROFILE="myprof.log" R -f my-program.R
google-pprof --text /usr/lib/R/bin/exec/R myprof.log | more
google-pprof --text --focus=emcore /usr/lib/R/bin/exec/R myprof.log | more

2.14 Bash Execution

2.15 Connect to a server

# connect to the server
ssh chaconmo@wayland.hec.lancs.ac.uk
# automatize password
ssh-copy-id chaconmo@wayland.hec.lancs.ac.uk

2.16 Batch file

#!/bin/bash

#$ -S /bin/bash
#$ -N myjobname

#$ -l h_vmem=2.5g

source /etc/profile
module add R/3.4.1

R CMD BATCH --no-save --no-restore test.R
#!/bin/bash

#$ -S /bin/bash
#$ -N myjobname

#$ -q parallel
#$ -l np=4
#$ -l h_vmem=0.5g

source /etc/profile

module add R/3.4.1

R CMD BATCH --no-save --no-restore test.R

2.17 R file

n <- 5000
x <- matrix(rnorm(n^2), n)
system.time(a <- x %*% t(x))

2.18 R file

n <- 5000
x <- matrix(rnorm(n^2), n)
system.time(a <- x %*% t(x))
system.time(plop <- chol(a))

2.19 Submission

qsub test.sh
qsub -q test test.sh
# qsub -N test test.sh
# monitor submissions
qstatus
# delete submission
qdel test

2.20 Install packages

remotes::install_github("ErickChacon/blogdown")
remotes::install_github("ErickChacon/reprodown")

2.21 Start your project

reprodown::create_proj()
blogdown::new_site('docs', theme = 'ErickChacon/scholar-docs', sample = FALSE)
reprodown::create_gitlab_ci()

2.22 Serve your web

blogdown::serve_site()
servr::daemon_stop()