Fork me on GitHub

R语言(Mac)

R安装

官方下载

https://cloud.r-project.org/bin/macosx/
下载 .pkg 安装包(如 R-4.4.0.pkg),双击安装包,按提示安装即可

homebrew安装

brew install r
brew install libgit2
#检查R是否安装成功
R --version

image

交互式指令

运行R命令进入交互式窗口
image

Rscript运行R脚本

编写script.R脚本文件

# script.R
x <- 1:10
y <- x^2
print(y)

通过Rscript运行.R文件
image

配置R

#设置国内镜像源(加快安装速度)
options(repos = c(CRAN = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
#将其写入启动配置文件 ~/.Rprofile
echo 'options(repos = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))' >> ~/.Rprofile

安装常用包

输入R进入交互窗口,运行如下命令

install.packages("tidyverse")
install.packages("data.table")
install.packages("ggplot2")

编译器RStudio

推荐使用 RStudio 作为 IDE,下载地址:https://posit.co/download/rstudio-desktop/

posted @ 2025-05-06 17:02  秋夜雨巷  阅读(322)  评论(0)    收藏  举报