王國慶 发表于 2022-9-26 02:53:54

R、01 VSCODE 配置 R 环境快速指南、4.2.1版本

安装最新版 R-4.2.1

R: The R Project for Statistical Computing (r-project.org)
有大量镜像供选择下载,找中国地区镜像下载会快一点。安装一口气Next到底。
https://cran.rstudio.com/bin/windows/Rtools/这个也要装!
Download Python | Python.org Python也要装!
https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/09/20220926021026_image-20220926021025778.png

添加系统环境变量

https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/09/20220926020919_image-20220926020917684.png

VSCODE 安装插件

https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/09/20220926021633_image-20220926021632227.png
推荐个 better comments插件: VSCode Better Comments 扩展配置、高亮注释插件 - 小能日记 - 博客园
VSCODE 配置插件

打开设置

https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/09/20220926022220_image-20220926022219197.png
rpath

填入 R.exe 可执行程序路径
https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/09/20220926022826_image-20220926022825153.png
r.rterm

安装 radian 插件。新建终端输入(确保Python、pip已安装)
pip install radian
where radian :: 这句指令会告诉你 radian 安装在哪https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/09/20220926022401_image-20220926022359699.png
r.rterm.option

删除其他,添加 --no-site-file
https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/09/20220926022642_image-20220926022641094.png
r.sessionWatcher

实现绘图IDE,查看dataframe。取消勾选用原生绘图。
https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/09/20220926022709_image-20220926022708057.png
创建终端

按 Shift+Ctrl+P 打开搜索面板,输入 terminal ,可以看到快捷键,。
https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/09/20220926023130_image-20220926023129959.png
安装languageserver

打开r语言( 终端R.exe 或 RGui.exe 或 RStudio 安装),输入 install.packages("languageserver")
保存时自动格式化

https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/09/20220926022944_image-20220926022942793.png
执行代码注意事项

右上角箭头 Run Source 执行代码。注意代码末尾需要用 View(数据、图片变量) 来展示数据框或图片。代码执行完毕依然可以在 Radian 查看和执行变量。
常见问题

install.packages("languageserver")

R语言安装languageserver报错
Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:解决方法:下载对应版本 Rtools 安装 https://cran.rstudio.com/bin/windows/Rtools/

SyntaxError: Non-UTF-8 code starting with '\xc4' in file

vscode命令行powershell运行radian报错
PS C:\Users\小能喵喵喵\Desktop\R\homework\1_Pelican> radian
SyntaxError: Non-UTF-8 code starting with '\xc4' in file C:\Users\小能喵喵喵\AppData\Local\Programs\Python\Python310\Scripts\radian-script.py on line 2, but no encoding declared; see https://python.org/dev/peps/pep-0263/ for details解决方法:打开错误的py文件,第一行加上下面代码保存
# -*- coding: gbk -*-
Error in loadNamespace(x) : there is no package called 'vscDebugger'

运行R代码出错
解决方法:按Ctrl+Shift+P 输入 r.debugger.updateRPackage (可能要开梯子,但不要开全局模式)
安装到一半依旧报错
Error in unpackPkgZip(pkgs, pkgnames, lib, libs_only, lock, quiet) :
package 'vscDebugger' not installed because it is not built for UCRT
Calls: install.packages -> .install.winbinary -> unpackPkgZip解决方法:手动安装,下载TAR版本
https://github.com/ManuelHentschel/VSCode-R-Debugger/releases/download/v0.4.7/vscDebugger_0.4.7.tar.gz
然后打开RGui.exe手动安装,在 C:\Environment\R-4.2.1\bin\x64,重启VSCODE即可.
https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/09/20220926021308_image-20220926021307038.png
该方法来自 issue:No support for R version 4.0.2 · Issue #110 · ManuelHentschel/vscDebugger (github.com)
功能展示

自动补全
https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/09/20220926023250_image-20220926023249985.png
绘图
https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/09/20220926021729_5a7d78b514dfb94041007aba7c31a77a.png
查看及搜索数据
https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/09/20220926021747_4bba01739112b7d684a99868ce7a0510.png
多行输出
https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/09/20220926021826_5e1617d6241c3b6cfe10fef03e98dbf0.png
鼠标悬停显示函数文档
https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/09/20220926021841_c4feabd833967b58cc883f776e465b16.png
鼠标悬停,显示变量信息
https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/09/20220926021905_c579ee5f3080c975f9990414263a88ee.png
格式化代码
https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/09/20220926021914_d13e34fdf73d348b6bab70ef0347ab23.png

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: R、01 VSCODE 配置 R 环境快速指南、4.2.1版本