How to close file connection in R after an error handle? - r

I have been using read.dbc function from read.dbc package and it is working fine for me.
Unfortunatelly, sometimes the file to be read is corrupted. To handle this, I use the try statement. The problem is that the corrupted file keeps "open" in R until I finish my R session, in a way I can't delete the file until the session is over.
My code is:
ReadFile <- try(read.dbc("C:/LocalPath/A_File.txt"))
Is there a way to "close" the connection prematurely (without the need to end the session)?
Also, I have tried to remove the variable with rm() and then cleaned the memory with gc() but the file keeps open.
More details: RStudio v0.99.484, R v3.3.1 (x64), OS Windows 7 Enterprise SP1
PS.: To reproduce the error simply try to read any file that is not a dbc file, a TXT file for example.

I don't think that this can be done in R as the file is locked from C code. If you look at the function dbc2dbf in this file you can see that it opens the file and tries to decompress it. Error "handling" is just printing the error and returning, but not closing the opened files which is bad practice imo. (Or a bug if you like to call it so.)
If you care you could open an issue on the github site of the project for this, maybe the author fixes the problem.
In case you are in desperate need (:P) of a working version I have compiled a quick fix for you that just calls fclose before it signals the error(s).
You can download it here. Just unpack and copy to your R library. Load with library(custom.read.dbc)

Related

Saving an R-Script results in empty file

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.

How to recover a file after rm() in rstudio project?

I rm(mydataframe) from my project in rstudio.(that's a mistake by long time work)
I clike edit- undo, but nothing happen.
Then, How to recover mydataframe? I believe rstudio has this feature.
You may have how you originally created the dataframe in the "History" tab. You can save the history to file as well if you like. Else, build a time machine?

RStudio error message - the device does not recognize the command

I am repeatedly getting the error message:
Error Save File: ' The device does not recognize the command' popping up over and over (every 15 seconds) - even when I don't click save or when I do click save.
I also can't load a R file that is in my folder - I have to open it up in notepad and copy it paste it to even see it.
Can someone let me know how to fix this? I have the latest version of Rstudio installed.
This happens when the drive memory runs out of space. As a quick fix measure, try copying all the code to an another R script file/ notepad and you might save it luckily. Long term solution would be to clear the cache and upgrade your hard drive..
This can also happen if you are reading from a file that is open in another program in some cases. Generally speaking R has no issues reading in files that are open / read only (i.e. import a CSV file) if that file is in use by another process. However, there are some cases where the read-only restriction from a locked file being imported causes the
Error Save File: ' The device does not recognize the command'
message to persist even if you are not trying to to read from or write to the locked file.
As a specific example, if you are using openxlsx to import an xlsx file that you are also viewing in excel, after reading the xlsx file in, you will get the error whenever you make changes to your R script and stop for more than a few seconds, or when you try to save the R script, even though it is not touching the xlsx file. However, if you close R Studio and re-open your script, you will notice that the issue will not appear until you try to read the xlsx file in again.
Met error "The device does not recognise the command" today. Not due to disc space but due to an r script that was somehow no good, even though file extension OK. After a very frustrating time when I couldn't copy to a fresh R script or shut down the project, saved content of troublesome r script to another older r script, deleted it and all OK - e.g. could now open up another new script that saved with no problems.

Error occurred while checking for updates.Unable to establish connection with R session Rstudio

I am unable to load an R project and I am getting an error: Error occurred while checking for updates. Unable to establish a connection with R session with R studio. I am using large data sets inside that projects which might be causing this issue. Is there a way I could log in from the terminal and remove some datasets from the workspace. Can someone help how I can log in?
It sounds like you're auto-saving the workspace to .RData. It's usually not a problem (though still not advised) to do so with small-ish data sets/objects in the workspace but it's almost deadly to use this setting if you work with large data sets/objects often.
Disable the Restore .RData into workspace at startup setting in RStudio preferences and also set Save workspace to .RData on exit to Never.
Hunt down all the .RData files in your various working directories and delete them, too.
I had the same problem. And the above proposed strategy did not work well. However, having anaconda installed in my PC I downloaded Rstudio on that platform. Contrarily to the other Rstudio standalone version, Rstudio in anaconda works well. I do not know why, but in case of need you can smash out your Rstudio without deleting your files and use the Rstudio on the anaconda platform. Just to have another point of view!

Plot() error when using REngine.Rserve

I'm now work on a project which wants to use org.rosuda.REngine.Rserve to use facilities of R by C#. I have a particular problem that when I run the following code in C# client side:
c.eval("pdf(file=\"plots.pdf\", width=11, height=8.5)");
c.eval("plot(1,2)");
An error occour which said
Error in plot.new():cannot open file '', reason No such file or directory
But I checked that the file "plots.pdf" has already been created successfully by "pdf.." function. In R semantics, the successive plot() will output figure to the active device opened by the "pdf(...)" function, i.e. "plots.pdf" this time. But I'm very confused that why the error indicated that the file is ''.
I use R-2.15.0 and Rserve-1.7.0 in linux server side.
I'm eager to see your response.
Thanks a lot.
Try this code :-
c.eval("dev.off()")
A folder named (null).1001.1001
Related answer at
Cannot open file '', reason No such file or directory
suggests using file.path as a alternative to paste for creating filenames with platform-independent path separators.
Comments suggest dev.off(). I had better luck with graphics.off(). The difference is that dev.close() closes the current device, graphics.off() closes all open devices.
But my solution turned out to be
https://askubuntu.com/questions/721485/what-is-folder-null-1001-1001
I found a mysterious folder named (null).1001.1001 (Rserve is running as uid 1001, gid 1001 on Linux). I create this folder at the start of my session and I'm good to go. The presence of this folder seems to help in my case, but I don't know (yet!) what that folder is.

Resources