R

How to install R in Terminal

  1. Go to https://cloud.r-project.org/ and download it through package. Or use homebrew
brew install r
  1. Install pakcage languageserver in R. (It will go forever in terminal)
R #go to R in terminal
install.packages("languageserver")
  1. Install radian (requires R and Python3)
pip3 install -U radian

(You probably need to add a new path)

Some commands

To know your current directory you can use

getwd()

In order to go to the sepecific directory, use the following code:

setwd("path")

Install packages

To install a package, use the following code:

install.packages("package_name")

For example, if you want to intall package tidyverse, use the the code below:

install.packages("tidyverse")

Click the link https://www.tidyverse.org/packages/ for more information.

Note: Packages "harfbuzz", "fribidi", "freetype" and "pkg-config" are necessary to install "tidyverse". If you failed to install tidyverse, you probably never intall these packages and you can easily installed it through homebrew:

brew install pkg-config harfbuzz fribidi freetype

If you failed to install a package on R, the source pachage will be stored in a temporary folder. In order to check that, use the following code

tempdir()

You can run the library after you installed the package. For example, if you want to run the library "tidyverse", use the following code:

library(tidyverse)

To load a package:

library(package_name)

To list all installed packages:

installed.packages()[, "Package"]

If you want to see more information for each package installed on your computer:

installed.packages()

Read CSV file

To read a CSV file, you have to use the following command:

read_csv("file_name.csv")

To quit R, use the following code:

To store data into a variable named "data":

data <- read_csv("file_name.csv")

To show i number of rows of "data":

print(data, n=i)

For example, if you only want to show 11 rows of data:

print(data, n=11)

To select columns from column 2 to column 4:

data[2:4]

To quit R, use the following code:

q()