前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R 和 RStudio 的安装及 R Profile 的配置 & 爬取 CRAN 上的所有 R 包的名称、发布日期和标题

R 和 RStudio 的安装及 R Profile 的配置 & 爬取 CRAN 上的所有 R 包的名称、发布日期和标题

作者头像
王诗翔呀
发布2020-07-06 17:42:22
3.4K0
发布2020-07-06 17:42:22
举报
文章被收录于专栏:优雅R优雅R

安装 R

现在最新版的 R 语言是 3.6.2 版本 (2019 年 12 月 12 日发布),该发行版的名字是 Dark and Stormy Night (漆黑暴风夜 ??),事实上只要用 3.0.0 之后的版本都差距不是很大。我的电脑上的 R 的版本是 R version 3.6.2 (2019-12-12)。

根据自己的版本点击下面的链接即可直接下载 R 软件了:

  1. Linux:https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/linux/
  2. Mac 系统: https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/R-3.6.2.pkg
  3. Windows 系统:https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/windows/base/R-3.6.2-win.exe

下载好之后一路安装即可。

Windows 用户:安装 Rtools:Windows 构建工具

Windows 用户需要安装这个工具:https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/windows/Rtools/Rtools35.exe

Mac 用户:安装 XQuartz

官网下载(很慢):https://dl.bintray.com/xquartz/downloads/XQuartz-2.7.11.dmg 从我的服务器上下载:https://czxa.top/assets/XQuartz-2.7.11.dmg

打开终端(Terminal)输入如下命令安装 homebrew:

代码语言:javascript
复制
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

安装 xcode 工具(在终端运行):

代码语言:javascript
复制
xcode-select --install

安装一些 pkg-config 和 gdal(在终端运行)(一些 R 包的依赖):

代码语言:javascript
复制
brew install pkg-config
brew install gdal

安装 RStudio Desktop

最新版的 RStudio 是 1.2.5019,各个版本的下载链接为:

  1. Windows 系统:https://download1.rstudio.org/desktop/windows/RStudio-1.2.5019.exe
  2. Mac 系统:https://download1.rstudio.org/desktop/macos/RStudio-1.2.5019.dmg
  3. Ubuntu18 系统:https://download1.rstudio.org/desktop/bionic/amd64/rstudio-1.2.5019-amd64.deb

注意

一定要先安装 R 再安装 RStudio!

安装常用的一些 R 包

可以运行下面的几句命令快速的安装一些 R 包:

代码语言:javascript
复制
# 安装 devtools
install.packages("devtools")
# tidyverse 系列的 R 包
install.packages('tidyverse', dependencies = TRUE)
# 安装 tinytex
install.packages("tinytex", dependencies = TRUE)
# 安装完成之后运行
tinytex::install_tinytex()
# 安装一些 RMarkdown 模板
install.packages("rticles", dependencies = TRUE)
install.packages("xaringan", dependencies = TRUE)

# 安装 Shiny
install.packages("shiny", dependencies = TRUE)

# 安装我写的一些 R 包
install.packages("hwordcloud", dependencies = TRUE)
install.packages("hchinamap", dependencies = TRUE)
install.packages("hpackedbubble", dependencies = TRUE)
install.packages("sankeywheel", dependencies = TRUE)

# 安装最近用到的一些 R 包
install.packages("sf", dependencies = TRUE)
install.packages("prettydoc", dependencies = TRUE)
install.packages("hrbrthemes", dependencies = TRUE)
install.packages("tmap", dependencies = TRUE)
install.packages("worldtilegrid", repos = "https://cinc.rud.is", dependencies = TRUE)
devtools::install_github('awhstin/awtools', dependencies = TRUE)
devtools::install_github("konradsemsch/ggrapid", dependencies = TRUE)
devtools::install_github("thomasp85/patchwork")
install.packages('manipulateWidget', dependencies = TRUE)
install.packages('ggthemes', dependencies = TRUE)
install.packages('tidyquant', dependencies = TRUE)
install.packages('rvest', dependencies = TRUE)
install.packages('DT', dependencies = TRUE)
install.packages("basetheme", dependencies = TRUE)
install.packages("ggchicklet", repos = "https://cinc.rud.is")

用一段时间之后你的电脑上就会安装一大堆 R 包,例如我电脑上安装的 R 包有 1479个(截止 2019年12月14号)。

代码语言:javascript
复制
library(tidyverse)
installed.packages() %>%
  as_tibble() %>%
  count() %>%
  pull() %>%
  paste("该电脑一共有", ., "个 R 包!") %>%
  message()

完整的名单(仅供参考):

代码语言:javascript
复制
installed.packages() %>%
  as_tibble() %>%
  select(Package, Version, License, Built) %>%
  DT::datatable()

安装字体

可以在知识星球上搜索 字体合集 找到我电脑上安装的一些字体。

解压后全选全部安装即可。

设置 R Profile

R Profile 里面的代码会在启动 R 的时候自动运行,创建 R Profile 文件可以使用:

代码语言:javascript
复制
usethis::edit_r_profile()

运行之后自动会打开一个文件,在里面输入你想要在 R 启动的时候自动运行的代码即可,可以参考我的:

Windows 系统:

代码语言:javascript
复制
# 加载包
suppressMessages(suppressWarnings(library(ggplot2, quietly = T)))
suppressMessages(suppressWarnings(library(dplyr, quietly = T)))
suppressMessages(suppressWarnings(library(RColorBrewer, quietly = T)))
suppressMessages(suppressWarnings(library(reshape2, quietly = T)))
suppressMessages(suppressWarnings(library(hrbrthemes)))
suppressMessages(suppressWarnings(library(awtools)))
suppressMessages(suppressWarnings(library(grDevices)))

# 把日期环境设定为英语环境
suppressMessages(suppressWarnings((Sys.setlocale("LC_TIME", "en_US.UTF-8"))))

# 设定字体,特别注意里面有一个 enfont 和 cnfont 是我最常用的两个字体。
windowsFonts(
  `Arial Narrow` = windowsFont("Arial Unicode MS"),
  `enfont` = windowsFont("Cascadia Code"),
  `cnfont` = windowsFont("宋体"),
  EconSansCndReg = windowsFont("Econ Sans Cnd"),
  IBMPlexSans = windowsFont("IBM Plex Mono"),
  IBMPlexSans = windowsFont("IBM Plex Sans"),
  `Public Sans` = windowsFont("Public Sans"),
  `Roboto Condensed` = windowsFont("Roboto Condensed"),
  `Roboto Slab` = windowsFont("Roboto Slab"),
  `Titillium Web` = windowsFont("Titillium Web")
)
enfont = "enfont"
cnfont = "cnfont"

# 设定 ggplot2 绘图主题
theme_set(theme_ipsum(base_family = 'enfont'))
print("ggplot: theme_ipsum()!")

# 这是设定 R 的基础绘图系统的绘图主题
basetheme::basetheme("brutal")

Mac 系统参考:

代码语言:javascript
复制
# 把日期环境设置成英文
suppressMessages(suppressWarnings((Sys.setlocale("LC_TIME", "en_US.UTF-8"))))
print("已把日期环境设置成英文!")

# 加载包
suppressMessages(suppressWarnings(library(ggplot2, quietly = T)))
suppressMessages(suppressWarnings(library(dplyr, quietly = T)))
suppressMessages(suppressWarnings(library(RColorBrewer, quietly = T)))
suppressMessages(suppressWarnings(library(reshape2, quietly = T)))
suppressMessages(suppressWarnings(library(hrbrthemes)))
suppressMessages(suppressWarnings(library(awtools)))
print("已加载ggplot2、reshape2、dplyr、hrbrthemes和RColorBrewer!")

# 设定 ggplot2 绘图主题
theme_set(theme_ipsum(base_family = 'CascadiaCode-Regular'))
print("已ggplot2绘图主题为theme_ipsum()!")

# 这里我的用的英文字体是圆体,pomofont 是凌慧体
enfont = "CascadiaCode-Regular"
cnfont = "STYuanti-SC-Regular"
pomofont = "MLingWaiMedium-SC"

# 这是设定默认使用 Python 的位置,可以参考自己的 Python 安装位置设定:
# reticulate::use_python('/opt/anaconda3/bin/python',required = T)

# 这是设定 R 的基础绘图系统的绘图主题
basetheme::basetheme("brutal")
options(rgl.useNULL=TRUE)

这样设定好之后再重启 R 即可生效。

基于这种设定,使用 R 基础绘图系统绘图的结果将会是这样的:

代码语言:javascript
复制
hist(iris$Sepal.Length)

使用 ggplot2 绘图的结果是这样的:

代码语言:javascript
复制
library(ggplot2)
ggplot(iris) +
  geom_col(aes(x = Sepal.Length,
               y = Sepal.Width,
               fill = Species,
               color = Species)) +
  tidyquant::scale_fill_tq() +
  tidyquant::scale_color_tq()

设置 RStudio 的样式

我比较喜欢暗黑一些的样式,在 Console 运行下面的代码即可更换为我推荐的主题:Mojave-Dark-RStudio-Theme

代码语言:javascript
复制
rstudioapi::addTheme("https://raw.githubusercontent.com/patrickshox/Mojave-Dark-RStudio-Theme/master/Mojave%20Dark%20(Static).rstheme", apply=TRUE, force=TRUE)

这个主题是这样的:

一个小的练习:爬取 CRAN 上的所有 R 包的名称、发布日期和标题

可以爬清华镜像源的列表:https://mirrors.tuna.tsinghua.edu.cn/CRAN/web/packages/available_packages_by_date.html

显然,这是个表格,很容易爬取:

代码语言:javascript
复制
library(rvest)
library(tidyverse)
# lubridate 是处理日期的一个 R 包
library(lubridate)
library(hrbrthemes)

# 需要耐心地等待一会儿
pkg <- "https://mirrors.tuna.tsinghua.edu.cn/CRAN/web/packages/available_packages_by_date.html" %>%
  read_html() %>%
  html_table() %>%
  .[[1]] %>%
  as_tibble() %>%
  mutate(
    Date = ymd(Date),
    Year = year(Date),
    Month = month(Date)
  )

# install.packages("ggchicklet", repos = "https://cinc.rud.is")
library(ggchicklet)
pkg %>%
  group_by(Year, Month) %>%
  count() %>%
  ggplot(aes(x = factor(Year), y = n)) +
  geom_chicklet(aes(fill = factor(Month)),
                width = 0.75,
                radius = grid::unit(3, "pt")) +
  theme_ipsum(base_family = enfont,
              grid = "X") +
  coord_flip() +
  scale_fill_brewer(name = "Month",
                    palette = "Paired",
                    breaks = 1:12,
                    labels = month.name) +
  theme(axis.text.x = element_text(color = "gray60",
                                   size = 10)) +
  theme(legend.position = "right") +
  guides(fill = guide_legend(ncol = 1)) +
  labs(
    title = "Number of R packages on CRAN",
    subtitle = "Created by TidyFriday @ czxa.top",
    caption = "Data Source: \nhttps://mirrors.tuna.tsinghua.edu.cn/CRAN/",
    x = "",
    y = "")

按年统计:

代码语言:javascript
复制
pkg %>%
  group_by(Year) %>%
  count() %>%
  ggplot() +
  geom_chicklet(aes(x = factor(Year), y = n,
                    fill = factor(Year))) +
  geom_label(aes(x = factor(Year),
                 y = n + 200,
                 label = n),
             family = enfont,
             color = '#444444',
             label.size = 0,
             size=3) +
  scale_fill_manual(
    values = c(RColorBrewer::brewer.pal(4, "Paired"),
               RColorBrewer::brewer.pal(10, "Paired"))
  ) +
  theme_ipsum(base_family = enfont) +
  theme(legend.position = "none") +
  labs(
    title = "Number of R packages on CRAN",
    subtitle = "Created by TidyFriday @ czxa.top",
    caption = "Data Source: \nhttps://mirrors.tuna.tsinghua.edu.cn/CRAN/",
    x = "",
    y = "")

可以看到,今年截止到今天,CRAN 上一共有 15334 个 R 包,今天新发布的 + 更新的有 6428 个!

❝附件链接:https://t.zsxq.com/qZjmiuV ❞

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-02-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 优雅R 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Windows 用户:安装 Rtools:Windows 构建工具
  • Mac 用户:安装 XQuartz
  • 安装 RStudio Desktop
  • 注意
  • 安装常用的一些 R 包
  • 安装字体
  • 设置 R Profile
  • 设置 RStudio 的样式
  • 一个小的练习:爬取 CRAN 上的所有 R 包的名称、发布日期和标题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档