readRDS fails to read in file in R. Is there an alternative? - r

I'm trying to read the RDS file I downloaded from here:
https://github.com/jcheng5/googleCharts/tree/master/inst/examples/bubble
However, when I try to load it into RStudio via:
data<- readRDS('/Users/nathanf/shinyCharts/healthexp.rds')
I get the Error: unknown input format.
I've searched and found a possible solution already posted on StackOverflow, but the solutions mentioned in it do not work.
Does not work:
readRDS(file) in R
Please note, I'm trying to do this with a freshly installed copy of R (3.2.1) on a Mac running Yosemite.
I've found articles online that say the readRDS function is now defunct. https://stat.ethz.ch/R-manual/R-devel/library/base/html/base-defunct.html
Sooooooo....dearest community, what should I do? Is there another way to read RDS files using a new function?
Any help would be much appreciated.
Thank you,
Nathan

I faced exactly the same problem and I can recommend switching from .rds objects into .RData objects. Simply:
save(random_forest1, "random_forest2.RData")
and then
load("random_forest2.RData")
Just for clarity, you will find your object named as random_forest1 after using load function

Related

compareGroups package - how to specify destination path in R when exporting files?

Beginner to R! Bit of a bizarre question, but is anyone familiar with the compareGroups package in R? There's a function to export files, but I have no idea where the file ends up in - can't seem to find it doing a computer search. (I use RStudio). The CRAN guide wasn't particularly helpful.
The functions I tried were export2csv() and export2word()
Thank you!
Found a solution. Oddly the formula doesn't follow instructions in the package's manual. In case anyone else gets the same problem, export2word(createTable(compareGroups(x ~ y1 + y2, data=dataset), file="filename.doc")
If you run
getwd()
you should get back the directory path that RStudio is using for your current project at that moment. If you do not specify a special destination for the export, I believe it should output it in that directory. You can also run
list.files()
to list the files in your current working project directory

Cannot open data source. .GDB in R

I am trying to load the data from the National Address Database provided by
Transportation.gov into R. The data can be downloaded by anyone after accepting the disclaimer at this link: https://www.transportation.gov/gis/nad/disclaimer
I download the data, unzipped it into a directory I called data and then tried to use rgdal to list all the layers present in the data via:
fc_list<- rgdal::ogrListLayers("./data/NAD_20180215.gdb").
However, I cannot get rgdal to return anything other than an error saying "Cannot Open Data Source"....
I am wondering how I would go about listing the layers present in the .gdb folder as well as reading them into R?
I'm very grateful for any help. Thank you in advance.
-nate
Following the suggestion here, using a full path to the gdb folder helped in my case.
# check for package and install if needed
if(!require(rgdal)){
install.packages("rgdal", dep=T)
library(rgdal)
}
# full path to the geodatabase required
fgdb <- "C:/full/path/to/the/geodatabase.gdb"
# list all feature classes in a file geodatabase
subset(ogrDrivers(), grepl("GDB", name))
ogrListLayers(fgdb)
paths in mac are written differently.
https://rpubs.com/bpattiz/Directories_Paths_Workspaces
And especially:
https://derekyves.github.io/2016/05/10/codeshare.html
First you need to set your working directory.
And then probably:
fc_list<- rgdal::ogrListLayers("/data/NAD_20180215.gdb")
or
fc_list<- rgdal::ogrListLayers("~/data/NAD_20180215.gdb")
will work

"Warning message: package ‘ XLConnect ’ is not available (for R version 3.3.3)"

I receive this message when I try to install "XLConnect" in R. I am trying to use some data from excel, and I don't know how else to load it into the program.
I'm completely new to R and programming, so any help is greatly appreciated!
This usually happens when there is a typo in the package name. The code below should work. My guess is that the capitalization might have been off when you tried it?
install.packages("XLConnect")
I'm reasonably sure that you had spaces on either side of term "XLConnect". At least that is what your error message is telling us. If trying what Mallick suggested does not succeed, then [edit] to include exact copies of any error message in the body of the question.
Other ways to transfer data from Excel to R include copying to the Clipboard or exporting as a '.csv' file. There must be hundreds of questions and answers about the Excel - to - R Highway Eternal Resurfacing Project. One more recent addition is the readxl package (function is read_excel) and that choice doesn't have the Java version dependencies that cause trouble for some XLConnect useRs.

problems opening hdf in R

I have scanned through and can't seem to find any thing that deals with "hdf" in R. I currently use rhdf5 but it works more with the hdf5 file format on R 3.0.2 for windows. any suggestions please?
I finally resolved it by downloading ENVIview and Beat software from ENVISAT directly. It allows for easy changes between data formats. the h4toh5 software too was a good option but would ony run with C library. Thanks, Frank.

The cause of "bad magic number" error when loading a workspace and how to avoid it?

I tried to load my R workspace and received this error:
Error: bad restore file magic number (file may be corrupted) -- no data loaded
In addition: Warning message:
file ‘WORKSPACE_Wedding_Weekend_September’ has magic number '#gets'
Use of save versions prior to 2 is deprecated
I'm not particularly interested in the technical details, but mostly in how I caused it and how I can prevent it in the future. Here's some notes on the situation:
I'm running R 2.15.1 on a MacBook Pro running Windows XP on a bootcamp partition.
There is something obviously wrong this workspace file, since it weighs in at only ~80kb while all my others are usually >10,000
Over the weekend I was running an external modeling program in R and storing its output to different objects. I ran several iterations of the model over the course of several days, eg output_Saturday <- call_model()
There is nothing special to the model output, its just a list with slots for betas, VC-matrices, model specification, etc.
I got that error when I accidentally used load() instead of source() or readRDS().
Also worth noting the following from a document by the R Core Team summarizing changes in versions of R after v3.5.0 (here):
R has new serialization format (version 3) which supports custom serialization of
ALTREP framework objects... Serialized data in format 3 cannot be read by versions of R prior to version 3.5.0.
I encountered this issue when I saved a workspace in v3.6.0, and then shared the file with a colleague that was using v3.4.2. I was able to resolve the issue by adding "version=2" to my save function.
Assuming your file is named "myfile.ext"
If the file you're trying to load is not an R-script, for which you would use
source("myfile.ext")
you might try the readRDSfunction and assign it to a variable-name:
my.data <- readRDS("myfile.ext")
The magic number comes from UNIX-type systems where the first few bytes of a file held a marker indicating the file type.
This error indicates you are trying to load a non-valid file type into R. For some reason, R no longer recognizes this file as an R workspace file.
Install the readr package, then use library(readr).
It also occurs when you try to load() an rds object instead of using
object <- readRDS("object.rds")
I got the error when saved with saveRDS() rather than save(). E.g. save(iris, file="data/iris.RData")
This fixed the issue for me. I found this info here
Also note that with save() / load() the object is loaded in with the same name it is initially saved with (i.e you can't rename it until it's already loaded into the R environment under the name it had when you initially saved it).
I had this problem when I saved the Rdata file in an older version of R and then I tried to open in a new one. I solved by updating my R version to the newest.
If you are working with devtools try to save the files with:
devtools::use_data(x, internal = TRUE)
Then, delete all files saved previously.
From doc:
internal If FALSE, saves each object in individual .rda files in the data directory. These are available whenever the package is loaded. If
TRUE, stores all objects in a single R/sysdata.rda file. These objects
are only available within the package.
This error occured when I updated my R and R Studio versions and loaded files I created under my prior version. So I reinstalled my prior R version and everything worked as it should.

Resources