美文网首页R小小白
学R记1:R、RStudio安装和 R Profile的配置

学R记1:R、RStudio安装和 R Profile的配置

作者: 凡有言说 | 来源:发表于2020-02-11 09:49 被阅读0次

    以下为跟随czx大神学习R语言时所做的笔记

    1.安装R

    Windows 系统:
    https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/windows/base/R-3.6.2-win.exe

    下载好后进行安装即可。

    2.安装Rtools

    Tools to build R and R packages. This is what you want to build your own packages on Windows, or to build R itself.
    Windows 系统:
    https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/windows/Rtools/Rtools35.exe

    3.安装RStudio

    RStudio是R的IDE(集成开发环境)https://rstudio.com/products/rstudio/download/#download

    注意:一定要先安装 R 再安装 RStudio!

    4.安装一些常用的R包

    # 安装 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)
    

    devtools可以使得我们能安装基于Git的包,因为有很多不错的包并没有发布在CRAN上,而是开源发布到Github。

    # 安装 Shiny
    install.packages("shiny", dependencies = TRUE)
    
    # 安装czx大神写得一些 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包

    # 查看电脑安装了多少个包
    library(tidyverse)
    installed.packages() %>%
      as_tibble() %>%
      count() %>%
      pull() %>%
      paste("该电脑一共有", ., "个 R 包!") %>%
      message()
    

    完整的R包名单

    installed.packages() %>%
      as_tibble() %>%
      select(Package, Version, License, Built) %>%
      DT::datatable()
    

    5.设置 R Profile

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

    usethis::edit_r_profile()
    
    # 加载包
    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")
    

    建议:在安装和更新R包时可以先将R profile 清空,待安装完毕后再恢复。

    6.RStudio样式

    比如暗黑色:

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

    参考资料:
    Windows下使用Rtools编译R语言包
    R 和 RStudio 的安装及 R Profile 的配置
    R与RStudio的安装

    相关文章

      网友评论

        本文标题:学R记1:R、RStudio安装和 R Profile的配置

        本文链接:https://www.haomeiwen.com/subject/cvvyxhtx.html