Rstudio Windows安装指南

Sat, Jun 1, 2024heziahR语言

建议使用chocolatey安装R和Rstudio

  • 安装chocolatey

    • win+r后输入powershell,按下ctrl+shfit+enter打开管理员模式powershell

    • 输入指令来安装choco

powershell
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) 作者:公子_Gongzi https://www.bilibili.com/read/cv24333287/?jump_opus=1 出处:bilibili
  • 安装r和rstudio

    win+r后输入cmd,按下ctrl+shfit+enter打开管理员模式cmd

powershell
choco install r r.studio
遇见 `Do you want to run the script?([Y]es/[A]ll - yes to all/[N]o/[P]rint):`  输入`A`

安装tinytex

Usage

r
install.packages('tinytex')

这里可以在Tools→Global Options→Packages→Primary CRAN respository中切换清华源

在执行后需要重启rsession,按快捷键ctrl+shift+F10,刷新环境变量

r
tinytex::install_tinytex()
r
# 中国大陆地区用户
# 因为默认的 CTAN 镜像往往太慢了,所以建议在安装完 TinyTeX 之后设置国内的 CTAN 镜像,以方便将来更新或安装 LaTeX 包。比如清华镜像:
library(tinytex)
tlmgr_repo('http://mirrors.tuna.tsinghua.edu.cn/CTAN/')
tlmgr_install('ctex')
# 安装需要的包
install.packages("rticles")
install.packages("showtext")

来源:https://yihui.org/tinytex/cn/

一些问题的解决方法

控制台无法显示中文

在 R 脚本中设置:

Sys.setlocale(category = "LC_ALL", locale = "en_US.UTF-8")

在 R 配置文件中设置

在当前目录创建 .Rprofile 文件,添加以下内容:

r
Sys.setlocale(category = "LC_ALL", locale = "en_US.UTF-8")

输出pdf中的代码不换行

在rmd文件头部加入yaml内容

yaml
---
title: "title"
author:
  - author
documentclass: ctexart
keywords:
  - 模拟
output:
  rticles::ctex:
    fig_caption: yes
    number_sections: yes
    toc: yes
    pandoc_args: --listings
    includes:
      in_header: "wrap-code.tex"
---

warp-code.tex

latex
\usepackage{listings}
\usepackage{color,xcolor}
\usepackage{fontspec}

% Define colors
\definecolor{grey}{rgb}{0.8,0.8,0.8}
\definecolor{darkgreen}{rgb}{0,0.3,0}
\definecolor{darkblue}{rgb}{0,0,0.3}

\lstset{
    backgroundcolor=\color{white},   % choose the background color
    basicstyle=\small\ttfamily, % size of fonts used for the code或改成\small\monaco稍大
    numbers=left,                        % 设置行号
    numberstyle=\tiny\ttfamily,            % 设置行号字体大小
    columns=fullflexible,
    breaklines=true,                 % automatic line breaking only at whitespace
    captionpos=b,                    % sets the caption-position to bottom
    tabsize=4,                       % 把tab扩展为4个空格,默认是8个太长
    commentstyle=\color{darkgreen},%\itshape,%
    escapeinside={\%*}{*)},          % if you want to add LaTeX within your code
    keywordstyle=\color{darkblue}\bfseries,%
    stringstyle=\color{black},%
    frame=single,                        % 设置有边框
    rulesepcolor=\color{red!20!green!20!blue!20},
    language=R,
}

绘图无法显示中文

在rmd文件yaml内容下方加入,注意删除#符号

r
# ```{r setup, echo=FALSE, message=FALSE, results='hide'}
if (!require("showtext")) install.packages("showtext")
library(showtext)
showtext_auto()
# ```

修改r代码块头部为

r
# ```{r fig.showtext = TRUE, fig.align='center', message=FALSE, warning=FALSE}
curve(dnorm(x, mean = 0, sd = 1), col = "red", lwd = 2, main="标准正态分布密度函数曲线")
# ```

即可绘制出

000016

安装汉化版-慎用

来源


Usage

下载**2023.03.1** Build 446版本的Rstudio https://download1.rstudio.org/electron/windows/RStudio-2023.03.1-446.exe

替换C:\Program Files\RStudio\resources\app.webpack\main\index.js

https://github.com/s0521/rstudio_cn/blob/main/index.js

替换C:\Program Files\RStudio\resources\app\www\rstudio\7EA4A6E4E8493375F0C7C283A8C92A12.cache.jshttps://github.com/s0521/rstudio_cn/blob/main/7EA4A6E4E8493375F0C7C283A8C92A12.cache.js