XiaO

用 R 进行绘图、写作与网站

XiaO / 2020-06-07


安装 R,RStudio 与 pandoc

brew install r

brew install --cask rstudio # 稳定版
brew install --cask rstudio-preview # 预览版
brew install --cask rstudio-daily # 日更版

本机安装的 rstudio 版本为 [2021.09.0,351](https://dailies.rstudio.com/version/2021.09.0+351.pro6/)

brew install pandoc
https://cran.r-project.org/src/base/R-4/ # R 4.0 语言安装包(可查看到不同的版本号以及源码)

  R-4.0.0.tar.gz  2020-04-24 09:05  32M  
[   ] R-4.0.1.tar.gz  2020-06-06 09:05  32M  
[   ] R-4.0.2.tar.gz  2020-06-22 09:05  32M  
[   ] R-4.0.3.tar.gz  2020-10-10 09:05  32M
[   ] R-4.0.4.tar.gz  2021-02-15 09:05  32M  
[   ] R-4.0.5.tar.gz  2021-03-31 09:05  31M 
[   ] R-4.1.0.tar.gz  2021-05-18 09:05  32M  
[   ] R-4.1.1.tar.gz  2021-08-10 09:05  32M  
[   ] R-4.1.2.tar.gz  2021-11-01 09:05  32M  
[   ] R-4.1.3.tar.gz  2022-03-10 09:05  33M  
[   ] R-4.2.0.tar.gz  2022-04-22 09:05  36M  

brew install --build-from-source r
https://hackage.haskell.org/package/pandoc # Pandoc 安装包(可查看到不同的版本号以及源码)

https://github.com/jgm/pandoc/tags

设置 Sublime Text 为 Terminal 默认编辑器

~/.zshrc 配置文件中添加如下一条语句即可,即将 Sublime Text 的命令行快捷方式添加到 PATH 中。

# Adding Sublime Text as the default efitor for Terminal, 似乎不同版本或安装方式的配置会不同
export EDITOR='subl -w' # brew --cask 版本可正常打开。

export PATH=$PATH:/Applications/Sublime\ Text.app/Contents/SharedSupport/bin

pandoc 的引文格式

Pandoc 默认管理引文的方法是使用 pandoc-citeproc。该工具依据 Citation Style Language (CSL) 规范管理文献,其中各个杂志的引文格式,可从 Zotero Style Repository 下载。

Terminal 中启动 R 后提示

During startup - Warning messages:  
1: Setting LC_COLLATE failed, using "C"   
2: Setting LC_TIME failed, using "C"   
3: Setting LC_MESSAGES failed, using "C"   
4: Setting LC_MONETARY failed, using "C"  

关于 Locale语言环境

add the codes to shell config file

export LANG=en_GB.UTF-8
export LC_ALL=en_GB.UTF-8

自定义 R 包的安装位置 说明之

查看 R 包的安装位置:

.libPaths()

R 在启动时会自动加载的其配置文件,可通过修改该配置文件来修改包的默认安装位置,比如我们将其安装到 ~/Rpackages

homebrew 安装的 R 的位置为 /usr/local/Cellar/,其配置文件的位置 /usr/local/Cellar/r/4.0.0_1/lib/R/library/base/R/Rprofile (注意版本号的不同)。

更新 R 以后,可以找到该文件,把上述包的位置重新写入即可,不必重新下载安装所有包,省事不少。

# Rprofile.site
.libPaths( c( "~/Rpackages", .libPaths()))

为进一步简化配置,可以添加 ~/.Rprofile 文件,该文件为 R 全局配置文件,每次启动将自动加载该文件。

# Rprofile.site
.libPaths(c("~/Rpackages", .libPaths()))

还可在每个 R 项目中 *.Rproj 所在位置添加单独的项目级 .Rprofile 文件,为每个项目单独添加相应的启动项,譬如:

if (file.exists("~/.Rprofile")) {
  base::sys.source("~/.Rprofile", envir = environment())
}

# Load library
library(viridis, quietly=TRUE, warn.conflicts=FALSE)
library(cowplot)
library(stats)
library(tidyverse)

安装 R 包

启动 RStudio,安装 R 包。

install.packages(c("tidyverse","svglite")) # 用 R 画图
install.packages(c("blogdown", "rmarkdown","bookdown", "tinytex","rticles","kableExtra")) # 生成网站或论文

包 rbbt 版本更新比 R 慢,导致其和 R 有匹配问题。如果使用 rbbt,可能需要安装较旧版本的 r。

利用 tinytex 包来安装 LaTeX 发行版 TinyTeX

tinytex::install_tinytex()

tinytex::install_tinytex()
Error in curlGetHeaders("https://mirror.ctan.org/systems/texlive/tlnet") : 
  libcurl error code 60:
	SSL certificate problem: certificate has expired
	
tinytex::install_tinytex(repository = "http://mirrors.tuna.tsinghua.edu.cn/CTAN/", version = "latest")
trying URL 'https://github.com/rstudio/tinytex-releases/releases/download/v2022.05/TinyTeX-1-v2022.05.tgz'
Content type 'application/octet-stream' length 88404844 bytes (84.3 MB)
==================================================
downloaded 84.3 MB

tlmgr option repository 'http://mirrors.tuna.tsinghua.edu.cn/CTAN/systems/texlive/tlnet'
tlmgr: setting default package repository to http://mirrors.tuna.tsinghua.edu.cn/CTAN/systems/texlive/tlnet
tlmgr: updating ~/Library/TinyTeX/tlpkg/texlive.tlpdb
tlmgr update --list
tlmgr: package repository http://mirrors.tuna.tsinghua.edu.cn/CTAN/systems/texlive/tlnet (not verified: gpg unavailable)
update:   babel              [186k]: local:    63189, source:    63498
update:   fancyvrb            [12k]: local:    63012, source:    63496
update:   fontspec            [27k]: local:    61617, source:    63386
update:   hyperref            [89k]: local:    62142, source:    63323
update:   l3kernel           [169k]: local:    63177, source:    63476
update:   l3packages          [22k]: local:    61588, source:    63505
update:   latex-amsmath-dev   [31k]: local:    62725, source:    63339
update:   latex-tools-dev     [43k]: local:    62725, source:    63339
update:   texlive-scripts    [108k]: local:    63172, source:    63503

安装 hugo v0.85.0 (静态博客生成器)

新版 hugo v0.89.4 与 blogdown 的 Addins 有冲突,在使用 blogdown:::new_post_addin() 时,无法生成并打开新的文件。

brew install hugo

Warning: Error in file: cannot open the connection
  9: shiny::runApp
  8: shiny::runGadget
  7: eval
  6: eval
  5: sys.source
  2: source_addin
  1: blogdown:::new_post_addin
brew edit hugo
# 改变版本地址 url
url "https://github.com/gohugoio/hugo/archive/v0.89.4.tar.gz" -> url "https://github.com/gohugoio/hugo/archive/v0.85.0.tar.gz" 
# 去除 fomular 中的 SHA 值验证
sha256 "9d4f61788f8d886913a1be15b3eae04fad04a4e243bd7f65c5e7367bd617856d" -> sha256 :no_check 
# 或者直接将  SHA 值验证注释掉
# sha256 "9d4f61788f8d886913a1be15b3eae04fad04a4e243bd7f65c5e7367bd617856d"

brew install --build-from-source hugo

brew pin hugo # To stop updating a particular package
brew unpin hugo # To Unpin a package