Error when trying to load .RData information - r

I have been trying to save my R environment to load in later sessions (my environment has about 12 data frames).
save.image(file = 'tests.RData')
if I look in my directory, my file looks like data has been saved since the .RData file is about 40MB in size.
Now when I try to load the file in the directory...
load('test.RData')
I get this error:
Error: bad restore file magic number (file may be corrupted) -- no data loaded
In addition: Warning message:
file 'tests.RData' has magic number ''
Use of save versions prior to 2 is deprecated
I tried looking at previous issues with this, and the only advice was to try load() and source(). However, I believe load() is for .rda/.RData files.
Is there anyway to save my environment and load it properly? What is causing my issue exactly?

Related

Set wd in RStudio

I am creating a series of r scripts that will be used by multiple people, meaning that the working directory of files used and stored will differ. There are two folders, one for the R code, called "rcode," and another to store the generated outputs, called "data". These two folders will always be shared in tandem. To accommodate for the changing working directory I created a "global" script that has the following lines of code and resides in the "rcode" folder:
source_path = rstudioapi::getActiveDocumentContext()$path
setwd(dirname(source_path))
swd_data <- paste0("..\\data\\")
The first line gets the source path of the global script. The second line makes this the working directory. The third line essentially tells the script to store an output in the "data" folder, which has the same path as the "rcode" folder. So to read in a csv file within the "data" folder I write:
old_total_demand <- read.csv(paste0(swd_data, "boerne_total_demand.csv"))
When I use this script on my Windows laptop it works beautifully, but when I use it on my Mac I get the following error:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file '..\data\demand\boerne_total_demand.csv': No such file or directory
Would anyone have any idea why this would be? Thanks in advance for the help.
I'm not sure what systems your collaborators will be using, but you may run into issues due to differences between Window/Mac/Linux with regards to how paths are written. I suggest you create a R Project .Rprj using RStudio and save that in your directory that contains subdirectories for data and rcode, and share the entire project directory.
/Project_dir/MyProject.Rprj
/Project_dir/data/
/Project_dir/rcode/
Then from the R project opened through RStudio you should be able to directly refer to your data by:
data <- read.csv("data/boerne_total_demand.csv")
The working directory will always be where your .Rproj is stored, so you can avoid having to setwd as it causes lots of chaos when sharing and collaborating with others.
I have this code from my current script at hand.
I hope you like it !
path <- dirname(getActiveDocumentContext()$path)
setwd(path)
swd_path <- paste0(path,"/data/")
if(!dir.exists(swd_path)){
dir.create(swd_path)
}
old_total_demand <- read.csv(paste0(swd_data, "boerne_total_demand.csv"))

How to solve "bad restore file magic number" when trying to load data?

I tried to load data to my R working directory and receive this error:
Error: bad restore file magic number (file may be corrupted) -- no data loaded
In addition: Warning message:
file ‘classize.RData’ has magic number 'RDX3'
Use of save versions prior to 2 is deprecated
I googled it and tried many options, unsuccessfully.
My Rstudio version is: 1.2.5033 (The error was happening before updating as well)
I create a new project, in the new directory, I put the data file
The data file is "classize.RData"
I have another alternative which is "classize.RDS" with the sugesstion to use readRDS(file = "classize.RDS"). When using this command, I receive that error:
cannot read workspace version 3 written by R 3.6.1; need R 3.5.0 or newer
This is in the context of a statistical course at university and my teacher assistant is unable to help me out, and whitout resolving this issue, I cannot move forward in the resolution of the needed exrecices. So please, couly you help me resolve that problem.
ps: all the students have access to the same data, It's just for me that it's not working, therefore the file should not be corrupted.

Why is the working directory overwritten to the directory of the current Rmd file?

I have an R.proj file called Food_Choices.Rproj that is supposed to be setting my working directory to ~/Desktop/Food_Choices, a folder containing reproducibility files according to the TIER system like
But it's not setting the working directory properly, because when I knit my processing file with code like this
food<-read_csv("Original_Data/food_coded.csv")
#imagine some processing code in between here
write.csv(food, file = "Analysis-Data/analysis_data.csv")
I get this error:
Error: 'Original_Data/food_coded.csv' does not exist in current working directory ('/Users/IdanCarre/Desktop/Food_Choices/Command_Files').
Which is not the project directory, it's the directory of the processing file!
I thought I set the working directory when I opened the files in the context of the R project, but that doesn't seem to be happening anymore (even though my files from a year ago with the same setup still work??)
NOTE: I don't want to use
library(knitr)
opts_knit$set(root.dir = '/Users/IdanCarre/Desktop/Food_Choices')
Because then new users who want to reproduce the results have to go manually insert their own directory into each file they want to run. That's a lot of work they shouldn't have to do.
UPDATE TO COMMENTS:
I used the here package, and that works satisfactorily for read.csv (it throws a data column de-duplication warning but I think it's probably okay for now), but when I write out the processed data file to the analysis data folder, I'm trying to use
write.csv(food, file = here("Analysis-Data", "analysis_data.csv"))
And the error I get is
Error in file(file, ifelse(append, "a", "w")) : cannot open the connection
I get this same problem if I use
write.csv(food, file = "Analysis-Data/analysis_data.csv")

Cannot load my CSV file into my R? keep getting error messages

So basically I succesfully exported my SQL view data into a csv file. but no when I load into Rgui software, I get the following errror:
> load("C:\\Users\\dachen\\Documents\\vTargetBuyers.csv")
Error: bad restore file magic number (file may be corrupted) -- no data loaded
In addition: Warning message:
file ‘vTargetBuyers.csv’ has magic number 'Marit'
Use of save versions prior to 2 is deprecated
What should I do? Is it the R version installed wrong? or something wrong with my CSV file?
Try using read.csv instead of load. load is for reading files created by save.
Type ?read.csv to access the documentation.

.RData error - R doesn't load

R won't load throwing the following error:
Error: bad restore file magic number (file may be corrupted) -- no data loaded
In addition: Warning message: file ‘.RData’ has magic number ''
Use of save versions prior to 2 is deprecated
How might I resolve this error?
It looks like your .RData file is corrupt. Try removing or renaming it, i.e.
rm .RData
or
mv .RData _RData

Resources