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

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.

Related

pathtrackR - S4 vector needs to be a double

I am using R for the first time as part of my dissertation and have been given code from my supervisor who got it from someone else (code attached below with comments from my supervisor). When I try to run it, I get this error and I am unsure how to resolve it.
Error at line 50:
ww<-manualPath("C:/Users/Erin/Document/4th year university/Honours Project/Data files/AOTEA/Jan/508_C5_JAN25_0922",1000,1000,fps=4)
Click once on the top left corner of your arena, followed by clicking once on the bottom right corner of your arena, to define the opposing corners of the entire arena...
Error in as.double(y) :
cannot coerce type 'S4' to vector of type 'double'
My supervisor thinks that the issue is that when the pathtrackR package was created it relied on other packages which have subsequently been updated while it hasn't. These updated packages won't, in turn, function on an older version of R where pathtrackR will work, and so the two fall down.
We know that the error is because the S4 vector needs to be a double, but I don't have enough coding experience to examine the pathtrackR package myself to determine why this is the case.
I was wondering if there is a workaround without reworking the code, or if anyone has any other help or advice on what I could try.
Any help is much appreciated.
######### REQUIRES AN OLDER VERSION OF R TO EFFECTIVELY RUN. I AM USING 3.6 FOR THIS
install.packages("devtools")
library(devtools)
find_rtools() #should return TRUE if an appropriate level of package is present (although F doesn't seem to affect functionality of package?)
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("EBImage")
library(EBImage)
#ONLY NEED TO REVISIT GITHUB IF IT HASN'T BEEN UPDATED IN A WHILE
install_github("aharmer/pathtrackr", build_vignettes = TRUE)
install.packages("digest", dependencies=TRUE) #seems to resolve issues about not instaling newest versions?
library(pathtrackr)
#####BIG ISSUE SEEMS TO BE ANY PROBLEM REQUIRES CLOSING R AND REOPENING......
#######################################################################
#######################################################################
#MANUAL TRACKING
#seems to be resolved on mac following additional install of XQuartz & Xcoe (had to find an old version from
#eg. https://stackoverflow.com/questions/7047735/where-can-i-download-old-versions-of-xcode Need version 9.2 for
#macOS Sierra 10.12.6)
#see also here https://www.r-bloggers.com/installing-r-on-os-x/
library(devtools)
find_rtools() #should return TRUE if an appropriate level of package is present
library(pathtrackr)
################### NB OPENING MORE THAN 2 VIDEOS WITHOUT CLOSING & RESTARTING R LEADS TO
################### SIGNIFICANT SLOWING & EVENTUAL CRASHING OF R
################### PROB A WORKING MEMORY ISSUE?
################### EASIEST 'OPTION'FIX' IS TO CLOSE & REOPEN EACH TIME
rm(list=ls())
library(devtools)
#library(sp)
find_rtools() #should return TRUE if an appropriate level of package is present (DOESN'T SEEM TO MATTER WHETHER TRUE OR FALSE???
library(pathtrackr)
####################################################################################################################################
#ww<-manualPath("/Volumes/LaCie/MarieCurieFellowship/2020Fieldwork/2020mp4vids/FirstTimes/710_C2_MAR6_1028",1000,1000,fps=4)
ww<-manualPath("~/Desktop/2020_Data/SLISCALED/806_C2_JAN09_1023",1000,1000,fps=4)
pdf(file='/Volumes/LaCie/MarieCurieFellowship/2020Fieldwork/2020_Data/SLI/pdf_images/806_Jan09.pdf')
plotPath(ww) #saving image
dev.off()
plotSummary(ww)
pathSummary(ww)
xx<-as.data.frame(ww$movement)
write.csv(xx, "/Volumes/LaCie/MarieCurieFellowship/2020Fieldwork/2020_Data/SLI/MovementData/sli806_Jan09.csv",row.names=F)
zz<-as.data.frame(ww$position)
write.csv(zz, "/Volumes/LaCie/MarieCurieFellowship/2020Fieldwork/2020_Data/SLI/PositionData/sli806_Jan09.csv",row.names=F)
####################################################################################################################################
#Second run of same commands. Allows you to realise you forgot to write the csv files without having lost all the work as all files are stored under new names
#pp<-manualPath("/Volumes/LaCie/MarieCurieFellowship/2020Fieldwork/2020mp4vids/FirstTimes/712_A4_MAR6_1045",1000,1000,fps=4)
pp<-manualPath("~/Desktop/2020_Data/SLISCALED/807_C7_JAN10_0910",1000,1000,fps=4)
pdf(file='/Volumes/LaCie/MarieCurieFellowship/2020Fieldwork/2020_Data/SLI/pdf_images/807_Jan10.pdf')
plotPath(pp)
dev.off()
plotSummary(pp)
pathSummary(pp)
ll<-as.data.frame(pp$movement)
write.csv(ll, "/Volumes/LaCie/MarieCurieFellowship/2020Fieldwork/2020_Data/SLI/MovementData/sli807_Jan10.csv",row.names=F)
kk<-as.data.frame(pp$position)
write.csv(kk, "/Volumes/LaCie/MarieCurieFellowship/2020Fieldwork/2020_Data/SLI/PositionData/sli807_Jan10.csv",row.names=F)
####################################################################################################################################
#Third run of same commands. Ibid.
#yy<-manualPath("/Volumes/LaCie/MarieCurieFellowship/2020Fieldwork/2020mp4vids/FirstTimes/713_B4_MAR6_1052",1000,1000,fps=4)
yy<-manualPath("~/Desktop/2020_Data/SLISCALED/807_D5_JAN09_0846",1000,1000,fps=4)
pdf(file='/Volumes/LaCie/MarieCurieFellowship/2020Fieldwork/2020_Data/SLI/pdf_images/807_Jan09.pdf')
plotPath(yy)
dev.off()
uu<-as.data.frame(yy$movement)
write.csv(uu, "/Volumes/LaCie/MarieCurieFellowship/2020Fieldwork/2020_Data/SLI/MovementData/sli807_Jan09.csv",row.names=F)
oo<-as.data.frame(yy$position)
write.csv(oo, "/Volumes/LaCie/MarieCurieFellowship/2020Fieldwork/2020_Data/SLI/PositionData/sli807_Jan09.csv",row.names=F)
####################################################################################################################################
#Fourth run of same commands. Ibid.
#ee<-manualPath("/Volumes/LaCie/MarieCurieFellowship/2020Fieldwork/2020mp4vids/FirstTimes/709_D2_MAR6_1020",1000,1000,fps=4)
ee<-manualPath("~/Desktop/2020_Data/SLISCALED/801_B8_JAN09_0947",1000,1000,fps=4)
pdf(file='/Volumes/LaCie/MarieCurieFellowship/2020Fieldwork/2020_Data/SLI/pdf_images/801_Jan09')
plotPath(ee)
dev.off()
rr<-as.data.frame(ee$movement)
write.csv(rr, "/Volumes/LaCie/MarieCurieFellowship/2020Fieldwork/2020_Data/SLI/MovementData/sli801_jan09.csv",row.names=F)
dd<-as.data.frame(ee$position)
write.csv(dd, "/Volumes/LaCie/MarieCurieFellowship/2020Fieldwork/2020_Data/SLI/PositionData/sli801_jan09.csv",row.names=F)

Geomorph not functioning after update

I recently updated my MacBook air (2017) to the Mojave OS and proceeded to update my version of R and Rstudio. Unfortunately, ever since the 'plotTangentSpace' command no longer functions appropriately even when attempting to run the example 'plethodon' code from the Geomorph vingette, I always receive the following error in the output (bolded below):
data(plethodon)
Y.gpa<-gpagen(plethodon$land) #GPA-alignment
|====================================================================================| 100%
gp <- interaction(plethodon$species, plethodon$site)
plotTangentSpace(Y.gpa$coords, groups = gp)
Error in plotTangentSpace(Y.gpa$coords, groups = gp) :
unused arguments (Y.gpa$coords, groups = gp)
I have tried uninstalling and reinstalling R for a MacOS many times including versions 4.0.2, 3.6.3, 3.6.1, and 3.2.1. I have also uninstalled and reinstalled XQuartz several times.
If anyone has encountered a similar issue or has any other ideas I can try I would really appreciate the help. Thank you!
If you execute:
?plotTangentSpace
You should see that this is a deprecated function. That means you should not be using it. It says:
Notes for geomorph 3.3.0 and subsequent versions
I suspect you have not updated your copy of the vignette. There are instructions on the help page for an alternate approach. The example in the help page for the suggested function is:
data(plethspecies)
Y.gpa <- gpagen(plethspecies$land) #GPA-alignment
### Traditional PCA
PCA <- gm.prcomp(Y.gpa$coords)
summary(PCA)
plot(PCA, main = "PCA")
For the plethodon data they suggest:
PCA.w.phylo <- gm.prcomp(Y.gpa$coords, phy = plethspecies$phy)
summary(PCA.w.phylo)
plot(PCA.w.phylo, phylo = TRUE, main = "PCA.w.phylo")
So start a new session (to unload the currently loaded geomorph namespace, and execute this at the r session command line:
install.packages("geomorph")
You should be getting version 3.3.1 of the geomorph package.
I think I found the problem! In my case, the error was due to a old version of the package RRPP, which is required by Geomorph. After updating it, Geomorph is working perfectly! Hope this can be useful for you too.
I came across the same error but after updating the RRPP and rgl packages required for geomorph, the gm.prcomp() function worked for me. I hope this helps if you haven't figured it out already.

How do I deal with an error message while installing a package?

I am brand new to this so please forgive my inexperience...I'm trying to learn.
I'm attempting to install an R package called "Doublet Finder" using the specified code given on the Github site.
When I do this, I get this error immediately:
Error in rbind(info, getNamespaceInfo(env, "S3methods")) :
number of columns of matrices must match (see arg 2)
Being new to R, I'm not sure what this error means and when I google this something similar comes up and the individual removed and re-installed ALL of their libraries...that seems crazy. Does anyone have advice on what this could be, how to fix it, or why the package won't install?
Your problem seems to be fairly similar to this one. It might be the case that the dependencies (packages that Doublet Finder relies on) are outdated. What you can try is to follow these steps to uninstall and reinstall all packages with the hope that by updating packages there isn't a version mismatch.
This code is copied from the website above:
ip <- as.data.frame(installed.packages(lib.loc = .libPaths()[1]),
stringsAsFactors = FALSE)
head(ip)
str(ip)
path.lib <- unique(ip$LibPath)
# create a vector with all the names of the packages you want to remove
pkgs.to.remove <- ip[,1]
head(pkgs.to.remove)
str(pkgs.to.remove)
sapply(pkgs.to.remove, remove.packages, lib = path.lib)
sapply(pkgs.to.remove, install.packages, lib = path.lib)

TreeTagger in R

I have downloaded TreeTaggerv3.2 for Windows and have configured it per the install.txt. I am trying to use it in R with koRpus package. I have set the kRp.env as -
set.kRp.env(TT.cmd="C:\\TreeTagger\\bin\\tag-english.bat", lang="en",
preset="en", treetagger="manual", format="file",
TT.tknz=TRUE, encoding="UTF-8" )
.My data to be tagged is in a file and trying to use it as treetag("myfile.txt") but it is throwing the error-
Error in matrix(unlist(strsplit(tagged.text, "\t")), ncol = 3, byrow = TRUE, :
'data' must be of a vector type, was 'NULL'
In addition: Warning message:
running command 'C:\windows\system32\cmd.exe /c C:\TreeTagger\bin\tag-english.bat
C:\Users\vivsingh\Desktop\NLP\tree_tag_ex.txt' had status 255
The standalone TreeTagger is working on by windows.Any idea on how it works?
I had the exact same error and warning while trying lemmatization on R word vector following Bernhard Learns blog using windows 7 and R 3.4.1 (x64). The issue was also appearing using textstem package but TreeTagger was running properly in cmd window.
I mixed several answers I found on this post and here is my steps and code running properly:
get into R win_library (~\Documents\R\win-library\3.4\rJava\jri\x64\jri.dll) and copy jri.dll (thanks kravi!) to replace it the parent folder.
close and restart R
library(koRpus)
set.kRp.env(TT.cmd="C:\\TreeTagger\\bin\\tag-english.bat", lang="en", preset="en", treetagger="manual", format="file", TT.tknz=TRUE, encoding="UTF-8")
lemma_tagged <- treetag(lemma_unique$word_clean, treetagger="manual", format="obj", TT.tknz=FALSE , lang="en", TT.options=list(path="c:/TreeTagger", preset="en"))
lemma_tagged_tbl <- tbl_df(lemma_tagged#TT.res)
Hope it helps.
I am posting this answer to keep a record. I also faced the same issue due to incorrect specification of the location of jri.dll on 64-Bit processor and windows 8.1. If we call
set.kRp.env(TT.cmd="manual", lang="en", TT.options=list(path="/path/to/tree-tagger-windows-x.x/TreeTagger", preset="en")) and we follow either of following two steps, we can resolve this error:
While installing R, if we install only 64 Bit version of R, and
specify the proper path for these variables
LD_LIBRARY_PATH = /path/to/rJava/jri
JAVA_HOME = /path/to/jdk1.x.x
java.library.path = /path/to/rJava/jri/jri.dll
CLASSPATH = /path/to/rJava/jri
If we already installed both versions viz. 32 bit and 64 bit of R on your computer then just copy jri.dll from /path/to/rJava/jri/x64/jri.dll and replace at path/to/rJava/jri/jri.dll. Further, we need to set the path of above mentioned four variables.
I've got this issue (very similar I guess) and posted query to GitHub.
https://github.com/unDocUMeantIt/koRpus/issues/7
The current working solution for me for this case was easier than I could expect, just downgrading the koRpus package. This can change with time but this version should remain appropriate.
library("devtools")
install_github("unDocUMeantIt/koRpus", ref="0.06-5")
This package is not Java related they said.
You can face the same error while setting up the korpus environment and getting the result from treetagger. For example, when you use:
tagged.text <- treetag(
"C:/temp/sample_text.txt",
treetagger = "manual",
lang = "en",
TT.options = list(
path = "c:/Treetagger",
preset = "en"
),
doc_id = "sample"
)
You would receive a similar error
Error: Awww, this should not happen: TreeTagger didn't return any useful data.
This can happen if the local TreeTagger setup is incomplete or different from what presets expected.
You should re-run your command with the option 'debug=TRUE'. That will print all relevant configuration.
Look for a line starting with 'sys.tt.call:' and try to execute the full command following it in a command line terminal. Do not close this R session in the meantime, as 'debug=TRUE' will keep temporary files that might be needed.
If running the command after 'sys.tt.call:' does fail, you'll need to fix the TreeTagger setup.
If it does not fail but produce a table with proper results, please contact the author!
Here you need to change the value of treetagger, from
treetagger = "manual"
to
treetagger = "kRp.env"
However, before that remember to set the kRp.env as #Xochitl C. suggested in their answer
set.kRp.env(TT.cmd="C:\\TreeTagger\\bin\\tag-english.bat", lang="en", preset="en", treetagger="manual", format="file", TT.tknz=TRUE, encoding="UTF-8")
Once you do this, you'll get the desired result.

RStudio-only error with tcl/tk?

When I compute an ROC plot with the package pROC, I get the following error in RStudio v. 99.9.9:
## Loading required package: tcltk
## Warning: couldn't connect to display ":0"
## Error: [tcl] invalid command name "toplevel".
I thought this was an issue in my OS (Debian with r-base 3.1 installed from APT depending on tcl/tk lib version 8.5), but when evaluating the same code through the terminal, I get the plot rendered without the same error.
Here is a simple example following some pROC example code that will generate the error when knitted in RStudio, but works in vanilla R:
```{r markdown.test}
library(pROC)
features <- runif(25)
labels <- as.factor(runif(25) < 0.75)
lda.model <- lda(as.matrix(features),labels)
lda.pred <- predict(lda.model,as.matrix(features))
lda.roc <- plot.roc(lda.pred$posterior[,1],
c(labels),
ci=TRUE, of="se", #specificities = seq(0,100,5),
ci.type="shape",
ci.col="#1c61b6AA")
```
The error occurs when the confidence interval parameters are added to the call.
Is there something missing from my RStudio install/config, or is this a bug?
Blast from the past! I see that same error today on an instance of RStudio installed in the IBM computing environment called CP4D. Did you ever find an answer?
I think the way to fix on your Macintosh is to install Quartz and let it serve as the X11 server to display output from programs like tcltk or maybe even hist() or quartz() devices. (my team wrote install instructions https://pj.freefaculty.org/guides/crmda_guides/47.mac_R_setup).
I'm not sure what I'll do on the CP4D thing, since it is running inside a Web browser.

Resources