centos7 r语言安装_R 和 RStudio 的安装及 R Profile 的配置 amp; 初识 R 语言数据爬取...
本文更新至 R version 4.0.0 (2020-04-24)
?R 和 RStudio 的安裝是非常簡單的,這里提供一些安裝 Tips。在文章的最后我還通過一個案例帶大家走進奇幻的 R 世界。
視頻講解
這個視頻講解比較老了,但是還是有一定的參考學習價值的:
安裝 R
?下載頁面:https://mirrors.tuna.tsinghua.edu.cn/CRAN/
?我安裝的 R 語言是 R version 4.0.0 (2020-04-24),該發行版的名字是 Arbor Day。
如果你已經安裝了 R 和 RStudio,可以運行下面的語句查看自己 R 版本的版本名稱:
#?查看自己?R?版本version$version.string
#>?[1]?"R?version?4.0.0?(2020-04-24)"
#?查看自己?R?版本的名稱
version$nickname
#>?[1]?"Arbor?Day"
根據自己的版本點擊下面的鏈接即可直接下載 R 軟件了(使用迅雷下載會很快):
下載好之后一路安裝即可。(如果你的電腦(Windows系統)是 32 位的,注意選擇 32 的安裝)。
Windows 用戶:安裝 Rtools:Windows 構建工具(Mac 用戶請跳過)
好像只有 Windows 用戶需要安裝這個工具:https://cran.r-project.org/bin/windows/Rtools/rtools40-x86_64.exe
如果你的電腦是 32 位的,下載這個安裝:https://cran.r-project.org/bin/windows/Rtools/rtools40-i686.exe
Mac 用戶:安裝 XQuartz(Windows 用戶請跳過)
官網下載(很慢):https://dl.bintray.com/xquartz/downloads/XQuartz-2.7.11.dmg 從我的服務器上下載:https://tidyfriday.cn/assets/XQuartz-2.7.11.dmg
打開終端(Terminal)輸入如下命令安裝 homebrew:
/usr/bin/ruby?-e?"$(curl?-fsSL?https://raw.githubusercontent.com/Homebrew/install/master/install)"#?如果安裝失敗,可以運行下面的代碼安裝
/usr/bin/ruby?-e?"$(curl?-fsSL?https://tidyfriday.cn/homebrew/install)"
安裝 Xcode 工具(在終端運行):
xcode-select?--install安裝 pkg-config 和 gdal(一些 R 包的依賴,在終端運行):
brew?install?pkg-configbrew?install?gdal
安裝 RStudio Desktop(用迅雷下載會很快)
?下載頁面:https://rstudio.com/products/rstudio/download/
?最新版的 RStudio 是 1.2.5042,各個版本的下載鏈接為(這個建議使用迅雷下載):
注意
一定要先安裝 R 再安裝 RStudio!
安裝常用的一些 R 包
可以運行下面的幾句命令快速的安裝一些 R 包,如果第一次安裝失敗,可以嘗試把 dependencies = TRUE 參數刪除重新運行一次試試。
#?安裝?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('patchwork',?dependencies?=?TRUE)
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("pacman",?dependencies?=?TRUE)
#?這些包是從我的服務器上安裝的,如果安裝失敗就多試幾次
install.packages("https://tidyfriday.cn/pkg/ggchicklet_0.5.2.tar.gz",?repos?=?NULL,?type?=?"source")
install.packages("https://tidyfriday.cn/pkg/awtools_0.2.1.tar.gz",?repos?=?NULL,?type?=?"source")
install.packages("https://tidyfriday.cn/pkg/ggrapid_0.0.1.0.tar.gz",?repos?=?NULL,?type?=?"source")
install.packages("https://tidyfriday.cn/pkg/worldtilegrid_0.2.0.tar.gz",?repos?=?NULL,?type?=?"source")
install.packages("https://tidyfriday.cn/pkg/ggtext_0.1.0.tar.gz",?repos?=?NULL,?type?=?"source")
用一段時間之后你的電腦上就會安裝一大堆 R 包,例如我電腦上安裝的 R 包有 1709個(截止 2020年5月1號)。
library(tidyverse)installed.packages()?%>%?
??as_tibble()?%>%?
??count()?%>%?
??pull()?%>%?
??paste("該電腦一共有",?.,?"個 R 包!")?%>%?
??message()
#>?該電腦一共有 1709 個 R 包!
完整的名單(僅供參考):
installed.packages()?%>%???as_tibble()?%>%?
??select(Package,?Version,?License,?Built)?%>%?
??DT::datatable()
安裝字體
可以從知識星球上下載我電腦上安裝的一些字體,鏈接:https://t.zsxq.com/3J6uBA6
解壓后全選雙擊全部安裝即可。
設置 R Profile
R Profile 里面的代碼會在啟動 R 的時候自動運行,創建 R Profile 文件可以使用:
usethis::edit_r_profile()運行之后自動會打開一個文件,在里面輸入你想要在 R 啟動的時候自動運行的代碼即可,可以參考我的:
?里面的字體設置非常重要,大家不要直接照搬這里的代碼,要掌握里面字體設置的方法。關于 Windows 電腦的字體設置可以點擊:https://t.zsxq.com/AQFUbau 跳轉到知識星球學習。
?Windows 系統:
#?加載包pacman::p_load(ggplot2,?dplyr,?RColorBrewer,?hrbrthemes,?grDevices)
#?設定字體,特別注意里面有一個 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'))
#?這是設定?R?的基礎繪圖系統的繪圖主題
library(basetheme)
pars?"default")
pars$palette?"#2A363B",?"#019875",?"#99B898",?"#FECEA8",?"#FF847C",?"#E84A5F",?"#C0392B",?"#96281B")
pars$bg??"white"
pars$fg??"gray20"
pars$col?"gray20"
pars$col.main?"black"
pars$col.axis?"gray20"
pars$col.lab??"gray20"
pars$family???"cnfont"
pars$lab??????10,?10,?7)
pars$cex.axis?0.8
pars$las??????0
pars$rect.border?"black"
pars$rect.lwd????4
basetheme(pars)
#?取消科學計數法的顯示
options(scipen?=?1)
#?設定?ggplot2?的默認調色板
options(
??ggplot2.continuous.colour?=?"viridis",
??ggplot2.continuous.fill?=?"viridis"
)
Mac 系統參考:
#?加載包pacman::p_load(ggplot2,?dplyr,?RColorBrewer,?hrbrthemes)
print("已加載?ggplot2、reshape2、dplyr、hrbrthemes?和?RColorBrewer!")
#?為了以后方便以后調用這些字體,我把我喜歡用的一些字體存儲成一些簡單的單詞。
enfont?=?"CascadiaCode-Regular"
cnfont?=?"SourceHanSerifSC-Medium"
pomofont?=?"MLingWaiMedium-SC"
#?設定?ggplot2?繪圖主題
theme_set(theme_ipsum(base_family?=?enfont))
print("已?ggplot2?繪圖主題為?theme_ipsum()!")
#?這是設定?R?的基礎繪圖系統的繪圖主題
library(basetheme)
pars?"default")
pars$palette?"#2A363B",?"#019875",?"#99B898",?"#FECEA8",?"#FF847C",?"#E84A5F",?"#C0392B",?"#96281B")
pars$bg??"white"
pars$fg??"gray20"
pars$col?"gray20"
pars$col.main?"black"
pars$col.axis?"gray20"
pars$col.lab??"gray20"
pars$family???pars$lab??????10,10,7)
pars$cex.axis?0.8
pars$las??????0
pars$rect.border?"black"
pars$rect.lwd????4
basetheme(pars)
#?取消科學計數法的顯示
options(scipen?=?1)
#?設定?ggplot2?的默認調色板
options(
??ggplot2.continuous.colour?=?"viridis",
??ggplot2.continuous.fill?=?"viridis"
)
注意上面的 pars$family 里面的字體應該使用你設置好的字體。
這樣設定好之后再重啟 R 即可生效(記得按 Ctrl + S 保存)。
基于這種設定,使用 R 基礎繪圖系統繪圖的結果將會是這樣的:
hist(iris$Sepal.Length)使用 ggplot2 繪圖的結果是這樣的:
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
rstudioapi::addTheme("https://tidyfriday.cn/assets/Mojave%20Dark%20(Static).rstheme",?apply?=?TRUE,?force?=?TRUE)這個主題是這樣的:
一個小的練習:爬取 CRAN 上的所有 R 包的名稱、發布日期和標題
?大家可以先不用急著理解下面的代碼。只想想通過下面的案例讓大家感受 R 語言的語法邏輯和繪圖的精美。
?可以爬清華鏡像源的列表:https://mirrors.tuna.tsinghua.edu.cn/CRAN/web/packages/available_packages_by_date.html
顯然,這是個表格,很容易爬取:
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)
??)
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?@?tidyfriday.cn",
????caption?=?"Data?Source:?\nhttps://mirrors.tuna.tsinghua.edu.cn/CRAN/",
????x?=?"",
????y?=?"")
按年統計:
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(5,?"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?@?tidyfriday.cn",
????caption?=?"Data?Source:?\nhttps://mirrors.tuna.tsinghua.edu.cn/CRAN/",
????x?=?"",
????y?=?"")
可以看到,今年截止到今天,CRAN 上一共有 15584 個 R 包,今年新發布的 + 更新的有 4008 個!
附錄
「為了讓大家更快熟悉 RStudio 這個 IDE(集成開發環境),我為大家提供了一份 RStudio 的速查表。在公眾號后臺回復 rstudio 即可獲取下載鏈接~」
?更多內容歡迎加入 TidyFriday 的知識星球獲取,詳情可以閱讀這篇推文了解:你知道如何加入我的知識星球么?
?推薦閱讀
- ?R 語言:邁向 DashBoard:使用 highcharter 繪制交互式世界地圖
- ?R 語言:使用 highcharter 繪制詞云圖!
- ?R 語言:從民政部網站爬取最新的行政區劃代碼表
- ?Stata: 如何在幾秒鐘內獲得整理好的新冠疫情數據?
- 【問答】如何刪除 Stata 箱線圖中的離群值?
- 【問答】Stata 如何繪制雙軸圖?還是那種分組柱形圖 + 折線圖的?
- ??必看!最新版!都是別人問我我也不說的東西!
- 如何繪制一幅圓滾滾的世界地圖?
- 從原始數據到動態圖表 —— 在 R 中完成所有操作
- 如何繪制一幅漂亮的路網地圖?& 繪制阜陽市確診病例的分布
- R 和 RStudio 的安裝及 R Profile 的配置 & 爬取 CRAN 上的所有 R 包的名稱、發布日期和標題
- Stata 修圖與操作記錄
- 彈性、半彈性及其在 Stata 中的代碼實現
- Python爬取知乎高贊回答并繪制詞共現網絡(二)
- 如何根據經緯度判斷該地點所處的省份?
- 誰在管理我們的國家?——2019年全國人民代表大會數據分析與可視化
總結
以上是生活随笔為你收集整理的centos7 r语言安装_R 和 RStudio 的安装及 R Profile 的配置 amp; 初识 R 语言数据爬取...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构02-链表
- 下一篇: 进程间通信之共享内存