RStudio 编辑器破坏文件 [英] RStudio editor destroys files

查看:76
本文介绍了RStudio 编辑器破坏文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,如果我尝试通过 RStudio 编辑文件,该文件被包含文本NULL"的一个替换.我首先注意到这是当我使用 fix() 并犯了语法错误时发生了一些实验导致我这就是原因.

I am having a problem whereby if I try to edit a file via RStudio, that file is replaced by one that contains the text "NULL". I first noticed that this was happening when I used fix() and made a syntax error, but some experimentation has led me to this being the reason.

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

To illustrate, first here is a session not in RStudio, but rather the Rgui app that comes with 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 中查看此文件.

Now I will look at this file in 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")

这是我看到的:

[此时我点击取消"按钮按钮]

[At this point I click on the "Cancel" button]

然后我再次读取文件:

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

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

So... Not only did it not retrieve the content of the file, it also ignored the fact that I canceled the edit. So my file is destroyed and there is no way to get it back. This is really not good. Can anyone advise what has happened? I am using RStudio Version 1.3.1093 (latest version) under Windows.

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

I am running the same installed R, version 4.0.3 (latest version) in both interfaces. Here is the session information for 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 的会话信息:

and here is the session information for 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 报告了这个错误,这些人非常积极响应.显然,该问题发生在 Windows 上,而不是 MacOSX.不确定 Linux 和其他.

I reported this bug to RStudio, and those folks have been very responsive. Evidently, the issue occurs on Windows but not MacOSX. Not sure about Linux and others.

暂时的解决方法是设置

options(editor = "internal")

这启用了一个稍微不同的编辑器(更少的基本框架,但需要保存"和关闭"来退出它).

This enables a somewhat different editor (less bare-bones, but takes both a "Save" and a "Close" to exit it).

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

Making this happen automatically when you start RStudio is a bit tricky. You can't just set this option in your .Rprofile file, because RStudio starts R and then overwrites that option with the one that causes this bug. Instead, add this to your .Rprofile file:

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

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

(Typically, .Rprofile is in your home directory -- in Windows, that is Documents) Some editors don't like filenames that begin with ., so you may need to save it under a different name and then rename it.

这篇关于RStudio 编辑器破坏文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆