【问题标题】:RStudio editor destroys filesRStudio 编辑器破坏文件
【发布时间】:2021-02-10 04:05:04
【问题描述】:

我遇到了一个问题,如果我尝试通过 RStudio 编辑文件,该文件 替换为包含文本“NULL”的内容。我首先注意到这是 当我使用fix() 并出现语法错误时发生了,但进行了一些实验 导致我这就是原因。

为了说明,这里首先是 RStudio 中的一个会话不是,而是Rgui 应用程序 R自带的:

> edit(file = "temp.R")
[1] "There is stuff in this file" "extending over two lines."
>
> scan(file = "temp.R", what = "", sep = "\n")
Read 2 items
[1] "c(\"There is stuff in this file\"," "  \"extending over two lines.\")"

现在我将在 RStudio 中查看这个文件。

> scan(file = "temp.R", what = "", sep = "\n")
Read 2 items
[1] "c(\"There is stuff in this file\"," "  \"extending over two lines.\")"  
> 
> edit(file = "temp.R")

这是我看到的:

[此时我点击“取消”按钮]

然后我又读了一遍文件:

> scan(file = "temp.R", what = "", sep = "\n")
Read 1 item
[1] "NULL"

所以...它不仅没有检索文件的内容,而且还忽略了我取消编辑的事实。所以我的文件被破坏了,没有办法取回它。这真的不太好。谁能告知发生了什么?我在 Windows 下使用 RStudio 版本 1.3.1093(最新版本)。

我在两个界面中运行相同的已安装 R,版本 4.0.3(最新版本)。以下是 Rgui 的会话信息:

> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_4.0.3 parallel_4.0.3

这里是 RStudio 的会话信息:

> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] compiler_4.0.3  htmltools_0.4.0 tools_4.0.3     parallel_4.0.3  yaml_2.2.1     
 [6] Rcpp_1.0.4.6    rmarkdown_2.2   knitr_1.28      xfun_0.14       digest_0.6.25  
[11] rlang_0.4.6     evaluate_0.14 

【问题讨论】:

  • 顺便说一句,我在 RStudio 中卸载了几个命名空间,以便 sessionInfo() 结果相同。这并没有解决问题。
  • 可以复制(同样在 Windows 上,相同的 RStudio 版本)。我想提交一个错误报告并避免调用 RStudio 的内部编辑器,直到它被修复。
  • @27ϕ9 谢谢 - 很高兴知道我并不孤单!我不知道如何提交错误报告。我会继续努力找出答案。
  • 因其他活动中断,但现在报告为issue #8209
  • @27ϕ9 我有一个解决方法。看我的回答

标签: r rstudio


【解决方案1】:

我向 RStudio 报告了这个错误,这些人的反应非常好。显然,该问题发生在 Windows 上,而不是 MacOSX 上。不确定 Linux 和其他。

暂时的解决方法是设置

options(editor = "internal")

这会启用一个稍微不同的编辑器(不那么简单,但需要“保存”和“关闭”才能退出)。

在您启动 RStudio 时自动执行此操作有点棘手。您不能只在 .Rprofile 文件中设置此选项,因为 RStudio 会启动 R,然后用导致此错误的选项覆盖该选项。相反,将其添加到您的 .Rprofile 文件中:

setHook("rstudio.sessionInit", function(newSession) {
    if (newSession) {
      options(editor = "internal")
      message("NOTE: `internal` editor has been set")
    } 
  }, action = "append"
)

(通常,.Rprofile 位于您的主目录中——在 Windows 中,即 Documents)一些编辑器不喜欢以 . 开头的文件名,因此您可能需要以不同的名称保存它然后重命名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-18
    • 2022-11-12
    • 2011-03-24
    • 2015-03-14
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多