第 1 章 Environment setup

Always launch your RStudio as a project. Or weird thing can heppen.

1.1 .Rprofile

An R script that you wants your project to run everytime when you open the project.

1.1.1 Put a root function

Where is the root that R thinks depends on where you execute your codes, it creates a lot of trouble.

Solution: in your .Rprofile

rprojroot::is_rstudio_project -> pj
pj$make_fix_file() -> root

Everytime you need a file under, say, root/doc/mycode.R, you type:

file.path(root(),"doc/mycode.R")

1.1.2 chinese font setting

knitr::opts_chunk$set(fig.showtext=TRUE, fig.retina = 1)

library(showtext)
font_add(
  "Noto Sans TC",
  regular="NotoSansTC-Regular.otf",
  bold="NotoSansTC-Bold.otf")
showtext_auto()
theme_set(
  theme(
    text=
      element_text(
        family = "Noto Sans TC")
  )
)

1.2 Github

1.2.1 Turn your R project into a git

Two ways:

Make it a Git from the beginning

Turn into Git afterward

1.2.2 Connect your Git to Github

File > Add local repository

(Commit) Under Changes tab, check those changes, write commit message, hit commit to master

Click Publish Repository

Uncheck Keep this code private

1.2.3 Update your Github

Whenever you have enough progress in your project, you can upload them to Github, called commit, push

Commit is the same as before. Push is the same as publish, except that icon is called Push Origin

1.2.4 Access your Github repo

1.3 Discussion

1.3.1 Using Github

click on the code file that you have problem, * move cursor to the problematic line.

  • next to the left of line number, double click; it will create ....

  • click ... you can (1) send the copy permalink in email or gitter chatroom, or (2) click Reference in new issue to create an issue for discussion in your repo.

1.3.2 Using Gitter

要貼程式碼到Gitter chatroom時,連同code chunk開頭與結尾的```一起copy & paste(即下圖35-40行全部)到聊天室會形成code的黑底模式,較清楚討論範圍。

1.4 Drake/rmd2drake Installation

install.packages("drake")
install.packages("devtools")
remotes::install_github("tpemartin/rmd2drake", force=T)

使用rmd2drake要注意:

  • 使用環境必需是個R project

  • 模版開起後要先存檔才能使用addins。

  • Rmd檔名只能有字母,_ 或數字。

plan_filename = drake::drake_plan(target1={...},target2={...},...)mk_plan_filename <- function(){# makecondition...}vis_plan_filename <- function(){# makecondition...}meta_plan_filename <- list(....) plan_filename.R filename.Rmd ---:drake_cache: ".myCache":---```{r setup, include=FALSE}knitr::opts_chunk$set(...)library(rmd2drake)library(drake)```## makecondition```{r makecondition}```## plan content```{r target1}target1 = {...}``````{r target2}target2 = {...}```