"bad restore file magic number" error in R - r

As in What could cause a "bad magic number" error when loading an R workspace and how to avoid it? and R has magic number 'ëPNG' error, I'm getting a bad restore file magic number error:
> load("fossilien.dat")
Error: bad restore file magic number (file may be corrupted) -- no data loaded
In addition: Warning message:
file ‘fossilien.dat’ has magic number '"Samp'
Use of save versions prior to 2 is deprecated
Using the source function gives me a different error (similar to unexpected string constant):
> source("fossilien.dat")
Error in source("fossilien.dat") :
fossilien.dat:1:10: unexpected string constant
1: "Sample" "Sname"
^
This is especially frustrating as I'm confident the source of data is legit - its for a homework problem: ftp://stat.ethz.ch/Teaching/Datasets/WBL/fossilien.dat
None of the SO questions above were really answered properly so I thought I would try again.
Some details about my R installation:
> R.Version()
$platform
[1] "x86_64-apple-darwin9.8.0"
$version.string
[1] "R version 2.15.3 (2013-03-01)"

The read.table function ended up working: http://stat.ethz.ch/R-manual/R-devel/library/utils/html/read.table.html
> fossildata <- read.table("fossilien.dat")
EDIT by CGW: load is for files in .rdata format, while your fossilien.dat clearly is an ASCII table. source executes the named file or object.

Related

Problem with loading .RData objects: "file ‘pathway.path.RData’ has magic number '' Use of save versions prior to 2 is deprecated"

I am trying to load an .RData file downloaded from the github repository https://github.com/VCCRI/SPAGI/tree/master/data.
After having saved the files on my computer if I try to load one of them with
load("./data/pathwaypath.RData")
I get the error message:
Warning message:
“file ‘pathwaypath.RData’ has magic number ''
Use of save versions prior to 2 is deprecated”
Error in load("./data/pathwaypath.RData"): bad restore file magic number (file may be corrupted) -- no data loaded
Traceback:
1. load("./data/pathwaypath.RData")
It also does not work with readRDS:
readRDS("./data/pathwaypath.RData")
throws
Error in readRDS("./data/pathwaypath.RData"): unknown input format
Traceback:
1. readRDS("./data/pathwaypath.RData")
Browsing through stackoverflow I gained the sense that it might be a problem with the serialization protocol used for creating the .RData object. Would anyone know more about it? Are there any known solutions to this issue?
I am running R version 4.0.0 (2020-04-24).
Here's an approach that is working for me on version 3.6.3:
temp <- tempfile()
download.file("https://github.com/VCCRI/SPAGI/blob/master/data/pathway.path.RData?raw=true",temp)
load(temp)
pathway.path[[1]][1:3]
#[[1]]
#[1] "RTN4R" "NGFR" "IRAK1" "MAP3K7" "IKBKB" "NFKB1"
#
#[[2]]
#[1] "RTN4R" "NGFR" "IRAK1" "MAP3K7" "IKBKB" "FOXO3"
#
#[[3]]
#[1] "RTN4R" "NGFR" "MAPK8" "JUN"

runjags trouble locating JAGS - error "'where' not found" even after setting jagspath

Seems that runjags suddenly (after update to version 2.0.3-2) has trouble finding JAGS binary, issuing an error:
[1] "Error in system(\"where jags\", intern = TRUE) : 'where' not found\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError in system("where jags", intern = TRUE): 'where' not found
I fixed this by putting this line to my Rprofile:
.runjags.options <- list(jagspath = "c:/Program Files/JAGS/JAGS-4.2.0/i386/bin/jags-terminal.exe")
This pretty much fixes the problem (although it is not ideal - previous versions of runjags could find the binary automatically).
However, when the Rgui (in Windows XP) is launched by opening an .Rdata file, which is associated to it, it stops working:
> .runjags.options # it was set in the Rprofile
$jagspath
[1] "c:/Program Files/JAGS/JAGS-4.2.0/i386/bin/jags-terminal.exe"
> require(runjags)
Loading required package: runjags
Warning message:
package ‘runjags’ was built under R version 3.1.3
> runjags.getOption("jagspath")
[1] "Error in system(\"where jags\", intern = TRUE) : 'where' not found\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError in system("where jags", intern = TRUE): 'where' not found
Is this a bug? How to fix this?
I am currently calling runjags.options(jagspath = "c:/Program Files/JAGS/JAGS-4.2.0/i386/bin/jags-terminal.exe") in my source after require(runjags), but I would like to avoid this as much as possible!
The problem is in the findjags() function, more specifically use of the 'where' system command which was added to Windows in 2003 and therefore doesn't exist in Windows XP. There is some code in findjags() that is designed to detect earlier versions of Windows and revert to a different method of finding JAGS, but this hasn't changed since version 1.x so I don't know why this has 'suddenly' stopped working. Perhaps there has been a change in your system, or a change in the way the system() command works in R. I have modified this code to work around where I think the error is coming from, but I don't have access to Windows XP (I'm rather surprised that you do!) so I can't test it. I don't think it will do any harm though so I will include it in the next release anyway, after testing on more recent versions of Windows.
I also don't know exactly why the .runjags.options is not being found when you open a .Rdata file, but it is possible that opening a .Rdata file loads the library before setting objects. I will look into this but it is likely not something I can do anything about. The alternative to using runjags.options() is to set the jags argument in calls to run.jags etc.

How to avoid printing / showing messages

this question has been asked before but non of the answers is working for me.
I am using the library rhdf5 from bioconductor.org to read HDF5 files: source("http://bioconductor.org/biocLite.R"); biocLite("rhdf5"); library(rhdf5);
When I use the h5read function to read particular variables that contain references the following warning message is printed:
"Warning: h5read for type 'REFERENCE' not yet implemented. Values replaced by NA's"
(It is not shown in red like errors and warnings in RStudio. Just in black)
The warning is OK for me as I don't need those references. But I use this function to read hundreds of variables, so my screen gets polluted with these messages.
For example:
a <-h5read(f, "/#Link2#")
Warning: h5read for type 'REFERENCE' not yet implemented. Values replaced by NA's
Warning: h5read for type 'REFERENCE' not yet implemented. Values replaced by NA's
I have tried all suggestions I found (capture.output, suppressMessage/Warning, sink, options(warn, max.print, show.error.messages):
capture.output(a <- h5read(f, "/#Link2#"), file='/dev/null')
I also tried invisible just in case: invisible(capture.output(a <- h5read(f, "/#Link2#"), file='/dev/null'))
suppressWarnings(suppressMessages(a <- h5read(f, "/#Link2#")))
I also tried suppressForeignCheck and suppressPackageStartupMessages just in case
{sink("/dev/null"); a <-h5read(f, "/#Link2#"); sink()}
{options(warn=-1, max.print=1,show.error.messages=FALSE); a <-h5read(f, "/#Link2#") }
They all keep producing the same warning messages.
Does anyone knows any other thing I might try, or why are these things not working?
How does the library manages to print the messages skipping all these? Sounds that I might be doing something wrong...
Any help is appreciated.
Just as reference these are the other posts I used:
r: do not show warnings
How to suppress warnings globally in an R Script
suppress messages displayed by "print" instead of "message" or "warning" in R
Suppress one command's output in R
You should ask maintainer("rhdf5") to provide a solution -- print message less frequently, and use standard R warnings -- the message is from C code and uses printf() rather than Rf_warning() or Rf_ShowMessage() or Rprintf() / REprintf().
Here's an illustration of the problem
> library(inline)
> fun = cfunction(character(), 'printf("hello world\\n"); return R_NilValue;')
> fun()
hello world
NULL
> sink("/dev/null"); fun(); sink()
hello world
>
and a solution -- use Rf_warning() to generate R warnings. The example also illustrates how writing to R's output stream via Rprintf() would then allow the output to be captured with sink.
> fun = cfunction(character(), 'Rf_warning("hello world"); return R_NilValue;')
> x = fun()
Warning message:
In fun() : hello world
> x = suppressWarnings(fun())
> fun = cfunction(character(), 'Rprintf("hello world\\n"); return R_NilValue;')
> sink("/dev/null"); fun(); sink()
>
None of this helps you directly, though!
UPDATE the maintainer updated the code in the 'devel' branch of the package, version 2.17.2.

Error: 'MonetDBLite' is not an exported object from 'namespace:MonetDBLite'

Working through a download script for CPS data found here. Using the script verbatim, per recent update, except for the Java modification that I added for my environment (below) to fix a previous error in loadnamespace. While I am familiar with the basics of R, this is my first foray into MonetDBLite.
# configure Java
if (Sys.getenv("JAVA_HOME")!="")
Sys.setenv(JAVA_HOME="")
library(rJava)
Now I am getting the following error, which generally comes after 380,000 of the 400,000 cps asec lines are processed.
Warning message:
In readLines(url) :
incomplete final line found on 'http://thedataweb.rm.census.gov/pub/cps/march/asec2015early_pubuse.dd.txt'
Error in dbConnect(MonetDBLite::MonetDBLite(), dbfolder) :
error in evaluating the argument 'drv' in selecting a method for function 'dbConnect': Error: 'MonetDBLite' is not an exported object from 'namespace:MonetDBLite'
MonetDBLite has just been updated on CRAN, please reinstall.

rWishart function not found in R

I am using R version 2.13.1 (2011-07-08) on Windows 7 and wish to use the function rWishart.
When I enter, say, rWishart(1,2,3), I get
Error: could not find function "rWishart"
Entering just rWishart, however, displays a matrix:
[,1] [,2]
[1,] 1 0
[2,] 0 1
To check that this object is not interfering with the function, I did rm(rWishart), and now rWishart gives
Error: object 'rWishart' not found"
but I still get the function-not-found error when using it as a function.
A search of my hard drive finds no rWishart.* file. I Googled and downloaded an rWishart.R file from
https://projects.cs.kent.ac.uk/projects/cxxr/svn/branches/sqlite/src/library/stats/R/
to the directory shown by getwd().
When I did load("<DIRECTORY>/rWishart.R") , I got
Error: bad restore file magic number (file may be corrupted) -- no data loaded
In addition: Warning message:
file 'rWishart.R' has magic number '# Fi'
Use of save versions prior to 2 is deprecated
Would someone be able to tell me how I can get the rWishart function running?
rWishart wasn't introduced in base R until 2.15. So you should update your version of R or find a package that adds the functionality.

Resources