Corrupt R Script

I tried to open an R script I usually worked with without any problem, but I got this error message:

WARNING: Failed to restore workspace from 'D:/...'
Reason: bad restore file magic number (file may be corrupted) -- no data loaded

Any advice?

Thanks in advance

Sonia :grinning:

The workspace can be saved to file when closing an R or RStudio session. It contains all of the objects in your namespace when the previous session was close if save workspace is set as the default element or chosen as the option on exit manually.

You do not need the workspace to run an R script unless the script depends on objects created outside the script and not saved to file. For example

some_function(some_object) -> some_other_object
my_script(some_other_object)

To debug, try navigating to the location with your R script and

source("my_script.R")

which will perform whatever operations it performs and complain about missing objects it expects or, if so specified, read data from file and transform it into the objects it needs.

Hi technocrat,

Thanks for your reply :slight_smile:
I tried what you suggested, and that is what I got

setwd("D:/my_folder")
source("my_script.R")

Error in file(filename, "r", encoding = encoding) : 
  cannot open the connection
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
  cannot open file 'my_script.R': No such file or directory

and I can't use my R Script. :tired_face:

Any clue what's going on?

Thanks in advance! :pray:

I would be amazed if the actual name of your script is the same as the example name that technocrat made up for demonstration purposes...

I would be amazed too actually :joy:

The actual name of the script is another one, but I am pretty sure it is not important for the conversation. :sweat_smile:

Any ideas about any solutions of this problem, @nirgrahamuk?

Thanks in advance :slight_smile:

Are you sure that my_script.R is in D:/my_folder?

:slight_smile: so Sonia
lets not be secretive, you can tell your actual script name to us, and probably nothing bad would happen :slight_smile:
in practical advice, I would verify you have what you think you have locationwise by using the dir() function to look at your present working directories contents and seeing that your script is sat there, spelt exactly as it would be when you try to source it in...

Yes, it is there...as strange as it sounds! :sweat_smile:

I tried with dir(my_script.R) as suggested by @nirgrahamuk, and that's what I got:

dir(my_script.R)
Error in dir(my_script.R) : object 'my_script.R' not found

What do you think?

Thanks.

You need to quote "my_script.R"

This relates to namespace, the objects that R knows about. These are the {base} libraries, libraries loaded into the session, and objects restored from an Rds file or read in by read.csv, for exaple.

Rather than even pass a value into dir(), I would call it empty, to get a list of all contents, and see if the script is one of them

1 Like

Actually, calling it empty I get it in the list

setwd("D:/my_folder")
dir()
[1] "my_script.R"

How can I open it now?

Thanks a lot :pray:

Having run that code to confirm the presence of the script. Does the source() give you the same or a different error?

Source() gives me

> setwd("D:/my_folder")
> source("my_script.R")
Error in file(filename, "r", encoding = encoding) : 
  cannot open the connection
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
  cannot open file 'my_script.R': No such file or directory

and dir()

> dir("my_script.R")
character(0)

Thanks :pray:

This has (easy for me to say without Windows to confirm it) to happen because there is no my_script.R in D:/my_folder).

setwd("D:/my_folder")
dir()
source("my_script.R")

Please run this Sonia and show me all the output from this sequence of codes...

1 Like

That's what I got!

> setwd("D:/my_folder")
Warning message:
file β€˜.RData’ has magic number ''
  Use of save versions prior to 2 is deprecated 
> dir()
[1] "my_script.R"               
> source("my_script.R")
Error in file(filename, "r", encoding = encoding) : 
  cannot open the connection
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
  cannot open file 'my_script.R': No such file or directory

Thanks :pray:

I'm scratching my head very hard.
What happens when you load my_script into the RStudio Editor by the File menu, Open (or Control+O) shortcut ?
Does the script show with any issues ?
Will the script run without errors under that circumstance ?

When I open it with Ctrl+O in the Console Window I have no error message, but the script is not shown.

It is shown only a long line of dots, the same as you can find here:

I guess it has something to do with the corruption of the file... :pensive:

That is worrying, I'm sorry you fell victim to that. It seems terribly unlucky :frowning:

Am I that hopeless? :pensive: