Russ Lenth
Russ Lenth

Reputation: 6770

RStudio editor destroys files

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.

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.\")"

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")

This is what I see:

enter image description here

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

Then I read the file again:

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

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.

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

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 

Upvotes: 1

Views: 89

Answers (1)

Russ Lenth
Russ Lenth

Reputation: 6770

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.

A workaround for the time being is to set

options(editor = "internal")

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

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"
)

(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.

Upvotes: 2

Related Questions