Chapter 21 R trick

科学计数法:

  • 取消科学计数法
options(scipen = 200)

scipen 表示在200个数字以内都不使用科学计数法

  • 显示位数:
options(digits=3)
  • Rmarkdown 不运行 eval=FALSE

  • 清除环境变量


- 拼接字符串
```paste()```

- 生成全排列:
[来自](https://stackoverflow.com/questions/18705153/generate-list-of-all-possible-combinations-of-elements-of-vector)

expand.grid(0:1, 0:1, 0:1) # for longer case: n <- 14 l <- rep(list(0:1), n) expand.grid(l)



- Try catch


```r
for (i in 1:10) {
tryCatch({log(4-i)},warning=function(a){print("warning!");browser();stop()},finally = {print(i);})
}

Try后面expr中的式子,如果一切无事,则run finally里面的expr,如果有warning,就run warning里面的function,如果有error,就run error 里面的function。

  • Call browser when error
options(error=browser)
options(warn=2)
options(error=recover)
  • R 取余:%%
2300%%100 
## [1] 0
230%%100
## [1] 30
  • 一个进度条的包,这个是外跳一个进度条窗口
library(tcltk2)

u <- 1:2000

plot.new()

pb <- tkProgressBar("进度","已完成 %",  0, 100)

for(i in u) {

x<-rnorm(u)

points(x,x^2,col=i)

info <- sprintf("已完成 %d%%", round(i*100/length(u)))

setTkProgressBar(pb, i*100/length(u), sprintf("进度 (%s)", info), info)

}
library(progress)
pb <- progress_bar$new(total = 100)
for (i in 1:100) {
  pb$tick()
  Sys.sleep(1 / 100)
}

一个比较完整应用:

 pb <- progress::progress_bar$new(
    format = "MCMC running [:bar] :percent in :elapsed eta: :eta",
    total = iter, clear = FALSE)

-Benchmark

其中的试例代码:

benchmark("lm" = {
            X <- matrix(rnorm(1000), 100, 10)
            y <- X %*% sample(1:10, 10) + rnorm(100)
            b <- lm(y ~ X + 0)$coef
          },
          "pseudoinverse" = {
            X <- matrix(rnorm(1000), 100, 10)
            y <- X %*% sample(1:10, 10) + rnorm(100)
            b <- solve(t(X) %*% X) %*% t(X) %*% y
          },
          "linear system" = {
            X <- matrix(rnorm(1000), 100, 10)
            y <- X %*% sample(1:10, 10) + rnorm(100)
            b <- solve(t(X) %*% X, t(X) %*% y)
          },
          replications = 1000,
          columns = c("test", "replications", "elapsed",
                      "relative", "user.self", "sys.self"))

可以很简单的改成我的代码:

benchmark("Rvec2tril"=BayesJMCM::vec2tril(phiiVec)
          "Rcppvec2tril"=vec2trilcpp(phiiVec)
         )
  • 用字符串访问变量: 使用 get(). 如果要访问list中的元素,比如说
a=list(var1=c(1,2,3),var2=c(2,3,4))

要访问var1的话:

get("var1",a)

或者可以用一个更简单的方案:

a[["var1"]]
  • 1行/1列的矩阵取下标不降维:

https://d.cosx.org/d/420997-1-vector/4

matrix(rep(1,6),ncol=1)[1:3,,drop=FALSE] %*% 1
  • 加载管道符%>%就只需要magrittr一个包 by yihui

  • 矩阵反序 来源

a <- matrix(c(1:10,10:1), ncol=2)
b <- apply(a, 2, rev)

One approach is to publish your book as a GitHub Pages site from a /docs folder on your master branch as described in GitHub Help. First, set the output directory of your book to be /docs by adding the line output_dir: "docs" to the configuration file _bookdown.yml. Then, after pushing your changes to GitHub, go to your repository’s settings and under “GitHub Pages” change the “Source” to be “master branch /docs folder”. In this case, the .nojekyll file has to be in the /docs folder.

另外一个方法教程长了5倍所以还是忽略吧