Error when loading an .mat file using R - r

I try to load a .mat file using the R.matlab library. When I run:
x <- readMat("MPL.mat")
I get this error however.
Error in mat5ReadTag(this) :
Unknown data type. Not in range [1,19]: 18569
In addition: Warning message:
In readMat5Header(this, firstFourBytes = firstFourBytes) :
Unknown MAT version tag: 512. Will assume version 5.
Anybody who experienced this and has a way to deal with this?
Im familiar with this post: R.matlab/readMat : Error in readTag(this) but alternatives ways didnt work for me...

The problem is related to how is saved your "MPL.mat". If you save by default in MATLAB, it probably won't work using R.matlab in R.
When I saved my .mat object in '-v7' or '-v6' i did not have problems, but the R.matlab documentation suggest to save in '-v6' (Pag.25 in R.matlab package). For example, I saved 2 matrices (A and B) in 'MPL.mat'.
save('MPL.mat','A', 'B', '-v7')
I could read in R:
require(R.matlab)
data <- readMat("MPL.mat")
View(data$A)
View(data$B)

Related

Error when exporting R data frame using openxlsx ("Error in zipr")

Usually I'm using the openxlsx package and the write.xlsx function when exporting R data frames into .xlsx-files. Since yesterday - probably after I was using the package XLConnect - something got messed up and the write.xlsx function doesn't work anymore. This is the error I get:
Error in zipr(zipfile = tmpFile, include_directories = FALSE, files = list.files(path = tmpDir, :
unused argument (include_directories = FALSE)
Unfortunately, I don't understand what this error means. Thanks for any helpful advice.
Edit: The function works when I use an older openxlsx version (4.1.0).
I was getting the same error.
I think the problem is with dependencies of openxlsx. There is a "zipR" package that might be picked up when you install openxlsx, while the actual dependency is zip package:
https://cran.r-project.org/web/packages/zip/index.html
https://cran.r-project.org/web/packages/zipR/zipR.pdf
I installed "zip" along with openxlsx and I don't get the error anymore.
I do not really understand the error message here. My computer does not allow me to save files to "c:/". So, if remove "c:/" part, it works fine, to save the file to the current working directory.
library(openxlsx)
df <- data.frame('x' = c(1,2,3),
'y' = c(3,2,1))
openxlsx::write.xlsx(df, "test.xlsx")
You would also try another package: writexl
writexl::write_xlsx(df, "text5.xlsx")`
This works on my machine.

Difficulty opening a package data file of unknown type

I am trying to load the state map from the maps package into an R object. I am hoping it is a SpatialPolygonsDataFrame or something I can turn into one after I have inspected it. However I am failing at the first step – getting it into an R object. I do not know the file type.
I first tried to assign the map() output to an R object directly:
st_m <- maps::map(database = "state")
draws the map, but str(st_m) appears to do nothing, unless it is redrawing the same map.
Then I tried loading it as a dataset: st_m <- data("stateMapEnv", package="maps") but this just returns a string:
> str(stateMapEnv)
chr "R_MAP_DATA_DIR"
I opened the maps directory win-library/3.4/maps/mapdata/ and found what I think is the map file, “state.L”.
I tried reading it with scan and got an error message I do not understand:
scan(file = "D:/Documents/R/win-library/3.4/maps/mapdata/state.L")
Error in scan(file = "D:/Documents/R/win-library/3.4/maps/mapdata/state.L") :
scan() expected 'a real', got '#'
I then opened the file with Notepad++. It appears to be a binary or compressed file.
So I thought it might be an R data file with an unusual extension. But my attempt to load it returned a “bad magic number” error:
st_m <- load("D:/Documents/R/win-library/3.4/maps/mapdata/state.L")
Error in load("D:/Documents/R/win-library/3.4/maps/mapdata/state.L") :
bad restore file magic number (file may be corrupted) -- no data loaded
Observing that these responses have progressed from the unhelpful through the incomprehensible to the occult, I thought it best to seek assistance from the wizards of stackoverflow.
This should be able to export the 'state' or any other maps dataset for you:
library(ggplot2)
state_dataset <- map_data("state")

Error : could not find function "ImportMethodFrom"

I tried to run the code in Chapter 7 Data mining with R learning with case study book but I got an error in following line:
rankWorkflows(svm, maxs = TRUE)
The error was:
Error in as.character.default(X[[i]], ...) : no method for coercing
this S4 class to a vector
Then I searched on the internet and found following solution:
importMethodsFrom(GenomicRanges, as.data.frame)
and again again I got a new error:
Error: could not find function "importMethodFrom"
I searched a lot but I got nothing :(
You can try using library(sos) to find the packages where your function is located.
library(sos)
findFn("replaceherewithyourfunction")
Based on the answer of #Bea, there does not seem to be a importMethodsFrom anywhere in R. My guess is you found the call in a NAMESPACE file. Those files have different syntax than normal R scripts.
If you want to load a specific function from an R package (rather than all functions from a package), you can use libraryname::functionname instad of functionname in your code. In your case, replace as.data.frame with GenomicRanges::as.data.frame
If this does not work (for example because you don't have as.data.frame anywhere in your code), you can also load the whole GenomicRanges library with library(GenomicRanges)

How can I open .net files with package sna in R?

I have a lot of .net files, and the package tutorial says it is possible to use this format with sna.
My error message is:
> Error in FUN(X[[1L]], ...) : as.edgelist.sna input must be an
> adjacency matrix/array, edgelist matrix, network, or sparse matrix, or
> list thereof.
How can I open .net files with sna package in R? I browsed the net but I couldn't find any helpful information to my problem.
I have no problem with using package igraph, but there are some pros to use package sna, too.
Thank you in advance.
P.S. I tried to read pajek with library(network):
b1<- read.paj("15.net")
Warning message:
In readLines(file, 1, ok = TRUE) : incomplete final line found on '15.net'
If it works with 'igraph', do it with 'igraph'. Then convert the data to a form acceptable by functions in the 'sna' package. For example with asNetwork function from package 'intergraph' you can convert igraph object to a network object (package 'network'), which you can use with 'sna'.

Error in exprs(eset)

I am trying to read probes from a dat file, put them in a vector then put the vector into a subset to be able to concatenate more data to it and write it in a CSV file. Here my piece of code:
library(Biobase)
library(affy)
affys <- read.csv("address of my dat file")
affys_vec <- as.vector(affys)[,1]
exprs(eset)[affys_vec,] -> sub.set
write.csv(sub.set,file="subset.csv")
However when I reach to the : exprs(eset)[affys_vec,] -> sub.set
I get the following error message:
** Error in exprs(eset) :
error in evaluating the argument object' in selecting a method for function 'exprs':
Error: object 'eset' not found **
Are there any suggestions please?
Thanks,
pqtm
See the vignette An introduction to Biobase and ExpressionSets available on your computer, once Biobase is installed, as
vignette(package="Biobase", "ExpressionSetIntroduction")
But the idea is that you have created eset by pre-processing some CEL or other vendor-specific files. How you pre-process those depends on what your files are, maybe with affy or oligo or lumi, or from a public repository using a package like ArrayExpress or GEOquery Or perhaps you're using limma and have no need for an expression set.
The Bioconductor web site and mailing list provides a lot more information.

Resources