前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >118-R工具指南24-解决调用View的报错

118-R工具指南24-解决调用View的报错

作者头像
北野茶缸子
发布2022-04-05 15:40:42
发布2022-04-05 15:40:42
2.2K2
举报
文章被收录于专栏:北野茶缸子的专栏

书接上回

我提到了自己奇怪的报错,但是,我一旦使用View 就会报错:

代码语言:javascript
复制
Error in .External2(C_dataviewer, x, title) : unable to start data viewer
In addition: Warning message:
In View(a) : unable to open display

百般尝试无果后。我反复重新启动窗口:

我的sessionWatcher 也是顺利开启了的:

代码语言:javascript
复制
{
    "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 via R: 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。

指定Rsessions使用

作者依然给了完整的解决方案。

在Rprofile 文件中写入:

代码语言:javascript
复制
source(file.path(Sys.getenv(
   if (.Platform$OS.type == "windows") "USERPROFILE" else "HOME"
 ), ".vscode-R", "init.R"))

这段代码,也就是让R 运行脚本init.R,我们看看它长什么样:

代码语言:javascript
复制
local(source("/home/yzpeng/.vscode-server/extensions/ikuyadeu.r-2.4.0/R/session/init.R", chdir = TRUE, local = TRUE))

原来是套娃,再看看:

代码语言:javascript
复制
$ 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 作用的整个过程:

  • When vscode-R is activated with session watcher enabled, it deploys the initialization script to ~/.vscode-R/init.R.
  • vscode-R watches ~/.vscode-R/request.log for requests from user R sessions.
  • When a new R session is created, it sources init.R to initialize the session watcher and writes attach request to ~/.vscode-R/request.log.
  • vscode-R reads the attach request and knows the working directory and session temp directory ({tempDir}) of the attaching session.
  • vscode-R watches {tempDir}/vscode-R/globalenv.json for global environment info and {tempDir}/vscode-R/plot.png for plot graphics.
  • In the R session, the global environment info will be updated on each evaluation of top-level expression.
  • When user creates or updates a plot, the {tempDir}/vscode-R/plot.png is updated, and vscode-R will open the plot file.
  • When user calls 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.
  • When user calls the viewer (e.g. htmlwidget, provis) or browser (e.g. shiny app, HTML help documentation), the request is written to ~/.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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-03-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 北野茶缸子 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 书接上回
  • 问题所在
  • 指定Rsessions使用
  • 详细原理介绍
    • 参考资料
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档