Saving an R-Script results in empty file - r

I have recently started using R for uni. I want to save my code using either save as or ctrl+s. However, the saved file is empty more often than not. For some reason, the files save normally on a few occasions. However, they also became empty after saving changes to the script.
I couldn't find any solution for the problem, neither here nor anywhere else.
I am using RStudio installed via Anaconda on an Windows 10 x64 Notebook, if that helps.
Thanks a lot for the responses! I'd be glad to provide more details if needed!
This is what the saved files look like from File Explorer:

I just meet the exactly same problem. I solved it by checking and changing RStudio -> File -> "Save as Encoding" from "ISO-8859-1 (System default)" to "UTF-8", then the file was saved. I'm trying to recover my previous 0kb files...

i couldn't reproduce the error but i also don't like to use the anaconda environment.
if the problem is saving the text file than may i suggest to do just that and save the code in a regular .txt file?
if this is not an option you can try to find out what happens if you save the r studio code on a different drive (or usb/flash drive)
###########################################################
EDIT 14.07.2022
Interestingly enough, just today (14.07.2022) I had the same problem. After installing R 4.2.1 everything worked fine until I changed the encoding (from utf-8 to windows 1252), at which point all the files in my project were overwritten and are now empty. Changing back to an older version of R (4.1.3) allowed me to display/save/use all scripts again (from backups).
Using the answer provided by #Shidan it is possible to open the data in R-4.2.1 as well.
This problem does not seem to be connected to Anaconda but an encoding issue between Rstudio and any R distribution above (and including) 4.2.

Related

R Studio will not open .r files in source

I have used R for years and, until recently, have had few problems. However, since the last update, R keeps erasing scripts that I have saved in the Source window; then, when I go to retrieve them from recent files, it opens a script that has a title but zero code. I am able to open the files in notepad and copy/paste them back in, but I would like to know how to resolve this issue so that I could use R in the same expeditious manner as previous.
To reiterate with slightly different wording: R opens my saved scripts (.r files) as codeless source windows; there is simply a number 1 there and nothing else, though the title is displayed. I did save one as a text file and tried to open it that way but got the same results. Opening files in Notepad and copying/pasting them does work, but obviously R should be able to open R scripts and display R code.
Can anyone shed some light on this issue?
Thank you,
Matt
It's not sophisticated, but I would
uninstall R & RStudio
reset/remove the "internal state" stored by RStudio
reset/remove the "preferences" stored by RStudio
reinstall R & RStudio
Here's good documentation from RStudio for points 2 & 3, depending on your version and OS: https://support.posit.co/hc/en-us/articles/200534577-Resetting-RStudio-Desktop-s-State

R Studio Fatal error: 'R_TempDir'contains space

I tried to download the latest version (4.2.0) of R on my windows computer and received this error message upon trying to open the program. "Fatal error: 'R_TempDir' contains space". Has anyone ever had this issue or have any ideas on how to resolve it? I have tried reinstalling the program and moving where the program is stored but no luck.
I also experienced this issue with 4.2.0 of R on Windows 10 but not prior versions of R.
In my case, the reason was the default temporary file folder was within my user directory and my user ID has a space in it. Apparently this causes an issue.
I was able to work around it by setting the global environment variable TMPDIR in windows:
Go to the search box next to the start button and type "environment"
Click on the choice which says "Edit the system environment variables"
Click the button that says "Environment Variables" near the bottom of the window
Click "New" and enter TMPDIR for variable name and some suitable directory without spaces under variable value (I chose C:\Temp)
As an aside, I do not believe this is an RStudio specific issue (and 4.2.0 isn't a version for RStudio anyway).
Thank you Venk for your post. It didn't work for me, but it pointed me in the direction of a solution that did.
I changed the values of my TEMP and TMP variables to a folder that had no spaces in it or in any folder name in the path (as you note).
I'm working on a Windows 10 machine.
Rob
P.S. My first solution was to revert to an earlier version of R, which initially worked. Recently though, I got the same error, even using a previous version. I'm no expert, but the whole thing is very weird IMO.
Had the same issue, and the other proposed solutions didn't resolve it for me. (R 4.2.2 and Windows 10.)
Solution found here worked for me:
create a file named renviron.site that contains the following text, and save it to the Desktop, using a text-editing program.
TMP=C:/Temp TEMP=C:/Temp TMPDIR=C:/Temp
if a copy already exists, may need to handle that case-by-case
move the file to C:\Program Files\R\R-4.2.x\etc
I wasn't able to save directly to this folder, despite being in the admin account.
I tried the other solutions and none worked. I came across this post that suggested to not install R in the /Program Files/ folder, to instead modify the installation to the folder C:\R\ which worked.
I didn't go back and undo the work done based on previous suggestions, so I'm not sure if they too are needed.

The command exams2html() does not generate HTML page when it is run from RStudio?

I do not know why exams2html() does not produce HTML page when I run the command from RStudio but it is fine when I run from R itself. Both R and RStudio are updated to the latest version. I saw Zeileis et al. (2014) mentioned a similar issue in their paper "Flexible Generation of E-Learning Exams in R...". But it is unclear for me why this happens with the latest version? Does anyone have any idea how to fix that issue? Thank you!
A colleague of mine reported a similar behavior recently under Windows 10 using R 4.0.0 and a current RStudio. We could track this down to browseURL() not working in the default temporary directory associated with an R session. Maybe you have the same problem.
For him the following worked:
library("exams")
exams2html("swisscapital.Rmd", dir = ".")
browseURL("plain1.html")
This creates the plain1.html output HTML file in the current working directory (".") and then manually opens the file in the browser using browseURL().
By default (without a dir= argument), exams2html() essentially does the same thing in a dedicated temporary subdirectory in tempdir(). On my colleagues machine, essentially all steps worked but `browseURL() couldn't open the HTML file that was successfully created.
If this is also what happens for you, you can try the following:
exams2html("swisscapital.Rmd")
which should create at least one copy of plain1.html in a subdirectory of tempdir():
dir(tempdir(), recursive = TRUE)
If so, you can try to open that file from within R
browseURL(dir(tempdir(), "plain1.html", recursive = TRUE, full = TRUE)[1])
or manually. For my colleague, manually opening the file (e.g., via the Windows Explorer) worked but browseURL() did not.
Unfortunately, this is as much as I know about the problem up to now... It's possible that this is related to the RStudio browser but we haven't explored this further.
As for the problem described in Zeileis et al. (2014): This was fixed a long time ago by RStudio. Nevertheless you could try to see what happens if you set: options(browser = NULL) before running exams2html().
Any other pointers are also welcome.

Unable to save R script as an R file

When trying to save a script as an R file, I am not able to. The save as window opens, but the 'save as type' bar below the 'file name' bar is blocked/greyed-out. I am, however, able to save the script as an unreadable file. When I open it again to continue working on it, it cannot run. I then have to copy paste it to a new script to continue working on it and running the commands. I cannot seem to find many questions on the internet relating to this problem except this one: https://support.rstudio.com/hc/en-us/community/posts/219163988-Can-not-save-new-R-script-save-as-bar-grayed-out.
I have tried save() which does not work. I have also uninstalled R and installed the latest version of R, this does not help either. Would anyone know how to fix this problem so that I can save it as an R file?
EDITED:
I am using RStudio version 3.4.4. on the Windows 10 Home edition.
Processor: Intel(R) Core(TM) i5-7300HQ CPU # 2.50GHz.
RAM: 8.00 GB.
System type: 64-bit Operating System, x64-based processor.
Many thanks
friend!
I had the same problem as you. But what resolved was just put ".R" (example: save.R) in the end of the name, even with the grey bar, it generated the script automatically.
Hope it can help.
Windows 10 now prevents Rstudio to save a new document.
Give Rstudio permission, restart it and than it worked for me.
These other answers don't solve it for me. What I noticed is:
Working off of a network drive is a nightmare for R Studio.
Even if you do save it as an .R file, R Studio may not be able to open it.
What I did was:
Paste my script into a notepad file; save it as .R
Right click my .R file and Open With RStudio.
This minimizing RStudio doing the driving of opening files. And once the file is NAMED (it was named and saved as .R in notepad) RStudio seems to be able to save/overwrite changes to the file. It just cannot save the file the first time nor save TXT files it opens as .R files.
So yeah, for the initial script; template it in notepad and never change it's name.
By saving the R script with .R at the end automatically saves it as an R file!

RStudio R File Corruption

I had a R script open in RStudio. The file was saved many times over the course of several weeks and worked perfectly fine when RStudio was opened and closed. However, today, I restarted my computer and when I opened RStudio and more specifically the script that I mentioned, all of the R code vanished, leaving a single long row of "....." with red highlighting.
When I tried to open the R file in other text processors such as Sublime Text and Notepad++, only a line of zeroes was visible. None of my other R files were affected. I'm currently running Windows 8.1 and have the latest version of R and RStudio. What can I do to recover the code in the file and prevent something like this from happening again?
It might be an old thread and it might have been covered in 'user4458796' answer in suggestion #1 ("Use the history..."), but:
My friend had the same problem and we managed to recover the code from a 'history_database' files located on Windows at:
'C:\Users\%user%\AppData\Local\RStudio-Desktop\'
I assume there is an equivalent location in Linux in general.
Hopefully I won't get downvoted, just sharing my 2cents.
Ben.
It's not clear what happened to corrupt your file (and thus how to fix it if possible) and it is kind of ominous that you're just seeing 0's in other text editors, but I'll give you my best suggestion and some tips.
Suggestions for Attempting Recovery
Since your other R files were unaffected, you should have a messy record of your code in the history. Use the history to reconstruct your code.
Access a copy of your file from any version control, cloud, or offline backup you may have used -- git, SVN, iCloud, SugarSync, Dropbox, etc (I realize you probably wouldn't have posted this question if that were an option, but I had to throw it out there).
Use a Hex or sector editor to try to recover the data.
Use a data recover program to find an old version of your file.
Inspect your trash or recycling bin to see if it has an old version. Depending on your OS and the settings of how you (insecurely or securely) delete files, then you may be able to undelete a deleted version, even if it's not immediately available.
Try different methods of recovering text data from corrupted text files like OpenOffice's and Microsoft's suggestions.
Tips for the Future
I know that hindsight is 20/20, but a few quick tips for good measure:
Use version control. Git is supported in RStudio's GUI interface.
Have more than one version of your file. Many professors and professionals recommend writing/storing code in a text editor and using your IDE only for the working copy.
Make backups. Distinct from #2, you should backup your files to a hard drive, flash drive, or cloud service like Dropbox or Spideroak.

Resources