dataset cannot be loaded although data() lists it - r

We have an R library which has the dataset plaq.sample in the file data/plaq.sample.Rdata. One of the examples of a library function uses it. The example works just fine on my laptop (R 3.5.1), my colleagues laptop (R 3.1.3 and 3.4.4) but it fails on Travis CI (R 3.5.1 as well). The output is the following:
> data(plaq.sample)
Warning in data(plaq.sample) : data set ‘plaq.sample’ not found
> plaq.boot <- bootstrap.analysis(plaq.sample, pl=TRUE)
Error in bootstrap.analysis(plaq.sample, pl = TRUE) :
object 'plaq.sample' not found
Execution halted
This is bewildering. I have called data() at the beginning of the example and the output clearly states that this dataset is available:
Data sets in package ‘hadron’:
correlatormatrix
plaq.sample Sample plaquette time series
pscor.sample
samplecf Sample cf data
We are lost and do not understand why the R CMD check . does work on our laptops but not on Travis CI. What could be the issue for not finding the dataset?

I read a warning about the hidden file .Rdata which was present in the checking. I have added .Rdata to the .Rbuildignore file to exclude it. However, this file interprets each line as a regular expression, therefore also including data/plaq.sample.Rdata. Removing this line makes the dataset available again.

Related

unable to open .dat files on R even with haven installed

So I use SGA tools for processing my images. It gives back results in .dat files. Now in order to work on this data in R, I tried to import the .dat file using the haven package. I installed haven and then its library, but I am not able to import data still and it gives this error message.
Error: Failed to parse C:/Users/QuRana/Desktop/SGA Tools/Plate_Image_Example (1).dat: This version of the file format is not supported.
When I use this command install.packages("haven"), haven is loaded, but then when I load library using library(haven) nothing appears on my console except for this
> library(haven)
Then when I use this code:
datatrial1 <- read_dta("C:/Users/QuRana/Desktop/SGA Tools/Plate_Image_Example (1).dat")
It gives me the error mentioned above. When I try converting my .dat file to a .csv file and load my data, the imported data adds additional "t" values before the values in columns except for the first one like this:
Flags: S - Colony spill or edge interference C - Low colony circularity
# row\tcol\tsize\tcircularity\tflags
1\t1\t4355\t0.9053\t
1\t2\t4456\t0.8401\t
1\t3\t3439\t0.8219\t
1\t4\t3215\t0.8707\t
All the t's before the numeric values are not what I want. Another issue that I am facing is I cannot install the gitter package on my R version which is R 4.2.2.
You can read your tab separated file like so `read.delim("file_path", header = TRUE, sep = "\t")

RStudio session aborts when I run gRbase::stepwise function (undirected graphs)

I'm doing an R project on RStudio (RStudio 2021.09.1+372 "Ghost Orchid" Release for Windows), (R version 4.1.2).
I'm working on Windows 10 x64.
I want to create an Undirected Graph from a dataset:
library(gRbase)
library(gRim)
library(gRain)
data(BodyFat)
BodyFat <- BodyFat[-c(31,42,48,76,86,96,159,169,175,182,206),]
BodyFat$Age <- sqrt(BodyFat$Age)
BodyFat$Weight <- sqrt(BodyFat$Weight)
names(BodyFat)[names(BodyFat) == 'BodyFat'] <- 'BodyFatPerc'
gRbodyfat <- BodyFat[,2:15]
graph.MyGraph <- cmod(~.^., data = gRbodyfat)
AIC.MyGraph <- gRbase::stepwise(graph.MyGraph)
I'm still exploring RStudio and graphical models, so I run these lines from the console, one by one.
When I run the last line of code, R session aborts and I get the following pop-up message:
I've also tried the following lines of code (I changed the last line):
library(gRbase)
library(gRim)
library(gRain)
data(BodyFat)
BodyFat <- BodyFat[-c(31,42,48,76,86,96,159,169,175,182,206),]
BodyFat$Age <- sqrt(BodyFat$Age)
BodyFat$Weight <- sqrt(BodyFat$Weight)
names(BodyFat)[names(BodyFat) == 'BodyFat'] <- 'BodyFatPerc'
gRbodyfat <- BodyFat[,2:15]
graph.MyGraph <- cmod(~.^., data = gRbodyfat)
AIC.MyGraph <- stepwise(graph.MyGraph)
but I get the same problem. I tried three or four times running those lines of code, R aborted everytime.
My gRbase, gRim, gRain, Rgraphviz and RBGL libraries are in the home folder:
C:/Users/MyUser/Documents/R/win-library/4.1
Any advice?
EDIT:
I've tried uninstalling/reinstalling R, Rtools, RStudio, reinstalling libraries, updating them, I've tried launching RStudio as Administrator, I've tried changing computer.
The last thing I tried is this:
uninstalling R, Rtools and RStudio
deleting .RData and .Renviron files in the Documents folder
deleting all libraries
downloading R (latest version) from CRAN webpage and installing it as Administrator
downloading Rtools (latest version from webpage) and installing it as Administrator
creating the .Renviron file as described here
downloading RStudio (latest version from webpage) and installing it as Administrator
installing BiocManager as described here (when asked to update Matrix library I said NO, I thought it could be the problem)
installing gRbase, gRim, gRain libraries as described here (when asked to update fansi library I said NO because it said "There are binary versions available but the source versions are later", I thought it could be the problem)
Tried again the lines of code
Still got the problem, R session aborted again.
A friend of mine doing a similar project used gRbase::stepwise function, so I inserted his code in the console and the function worked. So I think the problem might be the object of the function, graph.MyGraph. The cmod function works fine, so maybe there's a problem with gRbodyfat, which is used to build graph.MyGraph.
If I run these lines (I tried changing the dataset, I used BodyFat instead of gRbodyfat):
graph.MyGraph <- cmod(~.^., data = BodyFat)
AIC.MyGraph <- gRbase::stepwise(graph.MyGraph)
I get the following error:
Error in dim(res) <- c(NSEL, NSET) :
dims [product 210] do not match the length of object [14]
Don't know what to do.
EDIT:
I've tried debugging gRbase::stepwise function and I get this:
The problem appears to be the iModel function.
EDIT:
I've discovered that if I use graph.MyGraph <- cmod(~.^1, data = gRbodyfat) the gRbase::stepwise function works fine.

BiodiversityR::accumcomp.long function not found

I am trying to use the function accumcomp.long in the Biodiversity.R package in order to adjust my species accumulation curves in ggplot.
However, calling the function accumcomp.long gives me an error, suggesting that this function perhaps does no longer exists.
accum.long1 <- accumcomp.long(Accum.1, ci=NA, label.freq=5)
Error in accumcomp.long(Accum.1, ci = NA, label.freq = 5) :
could not find function "accumcomp.long"
The function BiodiversityR::accumcomp.long
returns
Error: 'accumcomp.long' is not an exported object from 'namespace:BiodiversityR'
I am not certain whether this is an issue with the package, the way the package is loaded into my R space, or perhaps unrelated to the package simply the format of my data.
My community and environment files are of dataframe class. I have tried loading them as .csv files as well as .rda files, but the error remains.
??accumcomp.long
renders "No results found".
I am using R version 3.5.3 (2019-03-11)
Any suggestions as to what else I could try?
Thanks in advance!
I re-installed the package BiodiversityR and included force = T (as per this thread https://community.rstudio.com/t/could-not-find-function-error-even-though-fun-is-in-namespace-and-has-an-rd-file/91243). I then reloaded the package. I am not sure what force = T does, but the accumcomp.long function now works.

How to quickly replicate/update local library under $R_LIBS_USER?

Suppose that
the version of R I have installed is 3.3.1;
my environment variable $R_LIBS_USER is set to $HOME/my/R_lib/%V; and
I have a directory $HOME/my/R_lib/3.3.1 containing a large number of packages I've installed over time.
Now I want to upgrade my version of R, to 3.4.1, say.
I'm looking for a convenient way to install a new collection of packages under the new directory $HOME/my/R_lib/3.4.1 that is the "version-3.4.1-equivalent" of the library I currently have under $HOME/my/R_lib/3.3.1.
(IOW, I'm looking for a functionality similar to what one can do with Python's pip installer's "freeze" option, which, essentially, produces the input one would have to give the installer in the future to reproduce the current installation.)
You can use function installed.packages for that purpose. Namely:
installed.packages(lib.loc = "$HOME/my/R_lib/3.3.1")
The returned object contains a lot of informations (most fields from each package DESCRIPTION file) but the names of the packages are in the first column. So something like the following should do the trick:
inst <- installed.packages(lib.loc = "$HOME/my/R_lib/3.3.1")
install.packages(inst[,1], lib="$HOME/my/R_lib/3.4.1", dependencies=FALSE)
To answer the additional question in the comments:
If your old library contained packages from other sources than CRAN, you will have to do some gymnastic based on the content of the DESCRIPTION files, and thus will depend on how well the package author documented it.
Argument field in installed.packages allows you to select some additional fields from those files. Fields of interest to determine the source of a package are fields Repository, URL and Maintainer. Here are some ideas on how to split them apart:
CRAN vs non-CRAN:
inst <- installed.packages(lib.loc = "$HOME/my/R_lib/3.3.1",
fields=c("URL","Repository","Maintainer"))
inst <- as.data.frame(inst, row.names=NA, stringsAsFactors=FALSE)
cran <- inst[inst$Repository%in%"CRAN",]
non_cran <- inst[!inst$Repository%in%"CRAN" & !inst$Priority%in%"base",]
Bioconductor packages:
bioc <- inst[grepl("Bioconductor",inst$Maintainer),]
source("https://bioconductor.org/biocLite.R")
biocLite(pkgs=bioc$Packages)
Github packages:
git <- non_cran[grepl("github", non_cran$URL),]
install.packages("devtools")
library(devtools)
for(i in seq(nrow(git))){
install_github(repo=gsub("http://github.com/","",git$URL[i]))
}

Error with importShapefile with PBSmapping package in R

I am receiving a sporadic error message with importShapefile in PBSmapping (version 2.63.37) in RStudio (0.97.318), running R version 2.15.2, platform: i386-w64-mingw32/i386 (32-bit). I also received the error while running previous versions of R and RStudio.
> ST6 = importShapefile("Data/pvi_stat_2002_utm.shp", projection="UTM", readDBF = TRUE)
Error in 1:nrow(dbf) : argument of length 0
> traceback()
2: cbind(1:nrow(dbf), dbf)
1: importShapefile("Data/pvi_stat_2002_utm.shp", projection = "UTM",readDBF = TRUE)
I only receive this error occasionally - perhaps 1 in every 10 times that I run the code. But once the error occurs in a session, it occurs repeatedly and will not successfully implement the command until I have closed R completely and reopened it. On one occasion I had to reboot the computer for it to work, as successive reopening of R did not help.
I thought it might be a memory issue but sometimes I will get the error when no objects are in the workspace. And usually the code runs fine even if I have large objects loaded. In response to the error I have removed all objects from the workspace and even followed with gc(), but to no avail.
This is the only shapefile with which I have received the error but as it is the only one that I use with regularity and since I can not predict when the error will occur, my efforts with other shapefiles are inconclusive. Not sure about uploading a shapefile to Stack Overflow. The zipped file is about 9MB.
Have a look in the folder where your shapefile is. Is there actually a .dbf file? If there is, it sounds like it is empty or corrupted, or misnamed. Are you expecting your shapefile to have polygons with attributes. Can you try importShapefile(... readDBF = FALSE )? Maybe you can make our data available through a dropbox link or something?
Alternatively have you tried rgdal:::readOGR or, my personal favourite, maptools:::readShapePoly(). I personally find readShapePoly() to be extremely robust and there are methods for coercing a SpatialPolygonsDataFrame from sp to a PolySet from PBS.
If you really must use PBS have you tried...
require( maptools )
require( sp )
myshp <- readShapePoly("Data/pvi_stat_2002_utm")
myshpPBS <- SpatialPolygons2PolySet( myshp )
I am assuming that there is a .prj file with your shapefile, describing the projection information?
I'm using R-3.0.1 and PBS Mapping 2.66.53 with the NAVO Divisions shapefile from http://www.nafo.int/about/overview/gis/Divisions.zip. On Windows 7 x86_64 and OS X Snow Leopard (using macports R built for x86_64), the .dbf is being read properly, but it
sometimes fails using RHEL 5.9:
> library("PBSmapping", lib.loc="/home/gwhite/R/x86_64-unknown-linux-gnu-library/3.0")
-----------------------------------------------------------
PBS Mapping 2.66.53 -- Copyright (C) 2003-2013 Fisheries and Oceans Canada
[...]
-----------------------------------------------------------
> library("rgeos", lib.loc="/home/gwhite/R/x86_64-unknown-linux-gnu-library/3.0")
rgeos version: 0.2-19, (SVN revision 394)
GEOS runtime version: 3.3.8-CAPI-1.7.8
Polygon checking: TRUE
> layer='Divisions'
> divs = importShapefile(layer, projection='LL')
Error in 1:nrow(dbf) : argument of length 0
Using readDBF=F does allow the shapefile data to be read:
> divs = importShapefile(layer, projection='LL', readDBF=F)
So far, importShapefile() has been working in a freshly started R session.

Resources