# R in Ubuntu22.04 ###### tags: `Tutorial`, `Ubuntu`, `R` 目錄: + [Install R](#R) + [Dependency Problem](#depend) + [Using R with Jupyter Notebook](#RJupyter) + [Using R with VS code](#RVScode) + 必要套件:vscode-R, languageserver + 輔助套件:[rmarkdown](#rmd), [radian](#radian) + (未使用) [R studio](#rstudio) 其他參考: + [Ubuntu22.04重灌筆記](https://hackmd.io/@Chieh997/UbuntuCUDAvenv) --- <a id='R'></a> ## R ### [Install R & Get CRAN Packages](https://cran.rstudio.com) Run these lines to tell Ubuntu about the R binaries at CRAN. ```bash=0 # update indices sudo apt update -qq # install two helper packages we need sudo apt install --no-install-recommends software-properties-common dirmngr # add the signing key (by Michael Rutter) for these repos # To verify key, run gpg --show-keys /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc # Fingerprint: E298A3A825C0D65DFD57CBB651716619E084DAB9 wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc # add the R 4.0 repo from CRAN -- adjust 'focal' to 'groovy' or 'bionic' as needed sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/" ``` Then install R and its dependencies. ```bash=+ sudo apt install --no-install-recommends r-base ``` Run this command (as root or by prefixing sudo) to add the current R 4.0 or later ‘c2d4u’ repository: ```bash=+ sudo add-apt-repository ppa:c2d4u.team/c2d4u4.0+ ``` to add the key id for this repository, add the repository, and update the index. + Open R in terminal ```bash R # or sudo R ``` + Run R script in terminal ```bash Rscript {file_name.R} ``` :::info Q. [TWCC]`install.packages` with error about setting working diectory? ``` Error in setwd(od) : cannot change working directory Error in setwd(startdir) : cannot change working directory Execution halted ``` A. This heppended probably due to the old working directory cannot be found, try mannually set your work directory to the path of R library (check with `.libPaths()`), for example, ``` setwd('/usr/local/lib/R/site-library') ``` Also, remember to use `sudo R` when you try to install new packages in TWCC. ::: <a id='depend'></a> ### Dependancy Problem #### GDAL for shapefile 空間統計中常使用的shapefile(`.shp`),欲開啟相關檔案常需搭配特殊套件(e.g. `rgdal`, `RJSONIO`)。 在安裝套件時可能會遇到dependency的問題,主要在於GDAL並未事先安裝在ubuntu上。 + [GDAL install](https://mothergeo-py.readthedocs.io/en/latest/development/how-to/gdal-ubuntu-pkg.html) ```bash= # repository preparation sudo add-apt-repository ppa:ubuntugis/ppa && sudo apt-get update sudo apt-get update # gdal install sudo apt-get install gdal-bin sudo apt-get install libgdal-dev # enviroment variables setting export CPLUS_INCLUDE_PATH=/usr/include/gdal export C_INCLUDE_PATH=/usr/include/gdal ``` + other dependency probelm 可能需要額外安裝`proj`或`protobuf`等,具體參考安裝報錯時的指令。 ```bash sudo apt-get install proj-bin libproj-dev # or sudo apt-get install libprotobuf-dev protobuf-compiler ``` <a id='RJupyter'></a> ## [Using R with Jupyter Notebook](https://developers.refinitiv.com/en/article-catalog/article/setup-jupyter-notebook-r) After R installing, install `IRkernel` with the following codes (`sudo` is required when opening R terminal): ```r=0 # Install R kernel for Jupyter Notebook via CRAN install.packages('IRkernel') # Making the kernel available to Jupyter IRkernel::installspec(user = FALSE) ``` Then open Jupyter Notebook, you will see that there's a new option `R` when you try to open some new notebook. <a id='RVScode'></a> ## 使用 VS Code 搭配 R + [在VS Code中使用R的幾個技巧](https://blog.yfei.page/cn/2020/12/vscode-r/) + [REditorSupport/vscode-R - GitHub](https://github.com/REditorSupport/vscode-R/wiki/Installation:-Linux) + [languageserver - GitHub](https://github.com/REditorSupport/languageserver) + [Using R in VS Code](https://schiff.co.nz/blog/r-and-vscode/) #### VS Code套件:vscode-R 1. 在 VS Code 的 extension marketplace 搜尋`R`或`reditorsupport.r`以安裝套件. 2. 如果無法順利開啟R terminal,在設定介面的`r.rterm.linux`更改R terminal的路徑。 #### R套件:languageserver `languageserver`是一個針對 Language Server Protocol 的R套件,輔助 VS Code 或其他平台作為 R editor 的自動補全、Highlight等。 1. 確認並安裝R所需環境 ```bash=0 sudo apt install --assume-yes --no-install-recommends build-essential libcurl4-openssl-dev libssl-dev libxml2-dev r-base ``` 2. 在R terminal中自CRAN安裝`languageserver` ``` install.packages("languageserver") ``` <a id='rmd'></a> #### Rmarkdown 1. 在R中安裝rmarkdown套件 ``` install.packages("rmarkdown") ``` 2. 可搭配VS Code套件: + Markdown Preview Enhance: 同步顯示編譯後輸出結果(after run chunk) + Markdown All in One: 自動補全、縮排/排版、數學顯示等 3. 若要順利knit,需額外安裝pandoc ```bash= sudo apt-get install pandoc ``` <a id='radian'></a> #### (非必要) Python套件: Radian 提供R在VS code上的Terminal Syntax Highlight。 但若啟用radian作為VS code上的預設R terminal,會導致視窗無法通過滾輪上下檢視先前輸入的指令(同tmux那樣),故不建議使用。 1. 以 pip 安裝 radian,並確認其路徑 ```bash=0 pip install -U radian which radian ``` 2. 在設定介面的`r.rterm.linux`更改 R terminal 的預設路徑為 Radian。 --- <a id='rsudio'></a> :::info 因本身使用ubuntu作為server遠端操作,並未使用Rstudio,以下安裝方式並未實際驗證過,僅做為參考。 ::: ## [Install RStudio Desktop](https://posit.co/download/rstudio-desktop/)**待校正 + [在 Linux 系統安裝 R 與 Rstudio](https://jacky810428.pixnet.net/blog/post/150622106-%E5%9C%A8-linux-%E7%B3%BB%E7%B5%B1%E5%AE%89%E8%A3%9D-r-%E8%88%87-rstudio) Download the installer (e.g. [.deb](https://download1.rstudio.org/electron/jammy/amd64/rstudio-2022.12.0-353-amd64.deb) for Ubuntu 22.04), then follow their instruction. ### [Install RStudio Server](https://posit.co/download/rstudio-server/)**待校正 After installing R, run the following code ```bash=0 sudo apt-get install gdebi-core wget https://download2.rstudio.org/server/jammy/amd64/rstudio-server-2022.12.0-353-amd64.deb sudo gdebi rstudio-server-2022.12.0-353-amd64.deb ``` Check your installation is sucess with ```bash+= systemctl status rstudio-server # if not activate systemctl start rstudio-server ``` Login to server with `{server_ip}:8787`