我提到了自己奇怪的报错,但是,我一旦使用View 就会报错:
Error in .External2(C_dataviewer, x, title) : unable to start data viewer
In addition: Warning message:
In View(a) : unable to open display
百般尝试无果后。我反复重新启动窗口:
我的sessionWatcher 也是顺利开启了的:
{
"r.alwaysUseActiveTerminal": true,
"r.bracketedPaste": true,
"r.rterm.linux": "/home/yzpeng/miniconda3/envs/R4.1/bin/radian",
"r.workspaceViewer.showObjectSize": true,
"r.lsp.use_stdio": true,
"r.lsp.debug": true,
"r.sessionWatcher": true
}
发现vscode 持续提示我languageserver
不存在。
可是,我明明是安装在对应的R4.1
环境下了啊。
我偏不信解决不了它。
接着发现,vs code 竟然一直找不到我的R:
搜了一下这个问题,找到了有意思的回答:R session not attached after run command `create terminal` and restart vscode · Issue #393 · REditorSupport/vscode-R (github.com)[1]
又是一个页面:R Session watcher · REditorSupport/vscode-R Wiki (github.com)[2]
在R Session watcher
文档中,写到:
★To enable this feature, turn on
r.sessionWatcher
in VSCode settings, reload or restart VSCode, and the session watcher will be activated automatically on R sessions launched by vscode-R viaR: Create R Terminal
command. ”
所以,还得是通过vscode 指令,Create R Terminal
开启R 吗?
可是,这里调用的R却是我初始环境的R,还要重新安装一遍languageserver
。
终于出来了:
不出意外的,View 也成功使用:
至此来看,问题主要在于,我的vs code 无法正确识别到我是用的R,自然R 插件的R Session watcher
,也没法正确运行。
但我的问题还是存在,我想要R Session watcher
在我指定的conda 环境中运行,而非是默认的R 或是base 环境中的R。
作者依然给了完整的解决方案。
在Rprofile 文件中写入:
source(file.path(Sys.getenv(
if (.Platform$OS.type == "windows") "USERPROFILE" else "HOME"
), ".vscode-R", "init.R"))
这段代码,也就是让R 运行脚本init.R
,我们看看它长什么样:
local(source("/home/yzpeng/.vscode-server/extensions/ikuyadeu.r-2.4.0/R/session/init.R", chdir = TRUE, local = TRUE))
原来是套娃,再看看:
$ cat /home/yzpeng/.vscode-server/extensions/ikuyadeu.r-2.4.0/R/session/init.R
# This file is executed with its containing directory as wd
# Remember the working directory (should be extension subfolder that contains this script)
dir_init <- getwd()
# This function is run at the beginning of R's startup sequence
# Code that is meant to be run at the end of the startup should go in `init_last`
init_first <- function() {
# return early if not a vscode term session
if (
!interactive()
|| Sys.getenv("RSTUDIO") != ""
|| Sys.getenv("TERM_PROGRAM") != "vscode"
) {
return()
}
# check requried packages
required_packages <- c("jsonlite", "rlang")
missing_packages <- required_packages[
!vapply(required_packages, requireNamespace,
logical(1L), quietly = TRUE)
]
if (length(missing_packages)) {
message(
"VSCode R Session Watcher requires ",
toString(missing_packages), ". ",
"Please install manually in order to use VSCode-R."
)
} else {
# Initialize vsc utils after loading other default packages
assign(".First.sys", init_last, envir = globalenv())
}
}
old.First.sys <- .First.sys
# Overwrite for `.First.sys`
# Is used to make sure that all default packages are loaded first
# Will be assigned to and called from the global environment,
# Will be run with wd being the user's working directory (!)
init_last <- function() {
old.First.sys()
# cleanup previous version
removeTaskCallback("vscode-R")
options(vscodeR = NULL)
.vsc.name <- "tools:vscode"
if (.vsc.name %in% search()) {
detach(.vsc.name, character.only = TRUE)
}
# Source vsc utils in new environmeent
.vsc <- new.env()
source(file.path(dir_init, "vsc.R"), local = .vsc)
# attach functions that are meant to be called by the user/vscode
exports <- local({
.vsc <- .vsc
.vsc.attach <- .vsc$attach
.vsc.view <- .vsc$show_dataview
.vsc.browser <- .vsc$show_browser
.vsc.viewer <- .vsc$show_viewer
.vsc.page_viewer <- .vsc$show_page_viewer
View <- .vsc.view
environment()
})
attach(exports, name = .vsc.name, warn.conflicts = FALSE)
# overwrite S3 bindings from other packages
suppressWarnings({
if (!identical(getOption("vsc.helpPanel", "Two"), FALSE)) {
# Overwrite print function for results of `?`
.vsc$.S3method(
"print",
"help_files_with_topic",
.vsc$print.help_files_with_topic
)
# Overwrite print function for results of `??`
.vsc$.S3method(
"print",
"hsearch",
.vsc$print.hsearch
)
}
# Further S3 overwrites can go here
# ...
})
# remove this function from globalenv()
suppressWarnings(
rm(".First.sys", envir = globalenv())
)
# Attach to vscode
exports$.vsc.attach()
invisible()
}
init_first()
终于解决了:
关于图片,有个不错的插件:
再看看图片:
如果想要查看图片历史记录,可以利用vscode 的文件查找功能,其会保留最近的打开文件记录:
作者也详细介绍了R Session watcher
作用的整个过程:
~/.vscode-R/init.R
.~/.vscode-R/request.log
for requests from user R sessions.init.R
to initialize the session watcher and writes attach request to ~/.vscode-R/request.log
.{tempDir}
) of the attaching session.{tempDir}/vscode-R/globalenv.json
for global environment info and {tempDir}/vscode-R/plot.png
for plot graphics.{tempDir}/vscode-R/plot.png
is updated, and vscode-R will open the plot file.View()
with a data frame, list, environment, or any other object, the request is written to ~/.vscode-R/request.log
and vscode-R will open a WebView to show the data or open a text document to show the content of the object.~/.vscode-R/request.log
and vscode-R will open a WebView to present the viewer content.我们先前配置的session 中的R,根本就没有和vscode-R 产生交集,自然也无从调用插件中的这些功能了。
[1]
R session not attached after run command create terminal
and restart vscode · Issue #393 · REditorSupport/vscode-R (github.com): https://github.com/REditorSupport/vscode-R/issues/393#:~:text=%EE%80%80R%20session%20not%20attached%20after%20run%20command%EE%80%81%20create,%28%EE%80%80not%EE%80%81%20%EE%80%80attached%EE%80%81%29%20will%20run%20.vsc.%EE%80%80attach%EE%80%81%28%29%20but%20nothing%20happens.
[2]
R Session watcher · REditorSupport/vscode-R Wiki (github.com): https://github.com/REditorSupport/vscode-R/wiki/R-Session-watcher#advanced-usage-for-self-managed-r-sessions