Error while knitting a LaTeX file: could not find function "as" - r

I am trying to knit a chunk embedded in a latex file with extension Rnw.
library(ggplot2)
library(flexclust)
library(arules)
data(Groceries)
Gr <- Groceries[, itemFrequency(Groceries) > 0.02]
grc <- as(Gr, "matrix")
I get this error:
#Error in as(Gr, "matrix") could not find function "as"
The same commands work perfectly well in R
Any ideas?

When in doubt, try to upgrade your software packages. If you are using a recent version of R (>= 3.5.0), you should not need to library(methods), because it will be loaded (attached) by default. This problem (Rscript does not load methods) has existed in base R for many years until this year and surprised/confused many users. If you have to use an old version of R, you will need library(methods) explicitly.

Related

Using base load in an active renvironment

I am working on an R markdown notebook in an active renv in my R project directory. Before I had activated the environment, I could use the load function from base R without a problem. But since activating the environment, I get errors when I use load(file=abc.RData).
I have a code chunk as follows:
rm(list=ls())
library(haven)
library(dplyr)
library(data.table)
# Load complete (individual+network) data ---------------------------
load(file="/Volumes/caas/CADRE CLC Data Project5/Clean Data/AK-SU-NETWORKS-ROUT/eda.RData")
If I try to run the chunk, I get the following error:
Error in load(file = "/Volumes/caas/CADRE CLC Data Project5/Clean Data/AK-SU-NETWORKS-ROUT/eda.RData") :
unused argument (file = "/Volumes/caas/CADRE CLC Data Project5/Clean Data/AK-SU-NETWORKS-ROUT/eda.RData")
But if I knit the document, it compiles without a problem.
What might I be doing wrong?
It looks like all I had to do was specify that I wanted to use load from base, as:
base::load(file="/Volumes/caas/CADRE CLC Data Project5/Clean Data/AK-SU-NETWORKS-ROUT/eda.RData")
That seems to have fixed it.

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.

slidify + data.table( ggplot2) error

when i try to add (data.table/ggplot2)code in slidify, i got error.
Here is my code in slidify:
## data.table
```{r}
library(data.table)
DT = data.table(x = 1:5, y=6:10)
setkey(DT, x)
DT[J(1)] # Error: No J function
```
---
## ggplot2
```{r}
library(ggplot2)
a = b = shape = 1:5
ggplot(data=DT, aes(a, b, col=shape)) + geom_point() # Error: can not find object a
```
All the code can run outside slidify, so i guess there is something about variable namespace wrong with slidify.
I also find this link:data.table error when used through knitr, gWidgetsWWW which might be similar with my problem, but still don't know how to fix.
Just to add an answer to follow up on the comments on the question. The dev version of data.table fixed it and is now on CRAN (data.table v1.9.4). But this broke kable() in knitr which knitr v1.7 (on CRAN too) fixes.
So basically, upgrade to latest CRAN versions of knitr and data.table and you should be fine. Please let us know if not.
More detail for the curious ...
I've made another change to data.table in v1.9.5 to make it more robust for packages that evaluate user code (like knitr, slidify and gWidgetsWWW) but don't know about data.table themselves. So that they don't need to know in future. Here is the item :
knitr::kable() works again without needing to upgrade from knitr v1.6 to v1.7. Packages which evaluate user code and don't wish to import data.table need to be added to data.table:::cedta.pkgEvalsUserCode and now only the eval part is made data.table-aware (the rest of such package's code is left data.table-unaware). data.table:::cedta.override is now empty and will be deprecated if no need for it arises.
And here is the item in v1.9.4 that was a little too over-reaching and broke knitr::kable in knitr v1.6 and knitr v1.7 fixes (but shouldn't have needed to) :
Added shiny, rmarkdown and knitr to the data.table whitelist. Packages which take user code as input and run it in their own environment (so do not Depend or Import data.table themselves) either need to be added here, or they can define a variable .datatable.aware <- TRUE in their namepace, so that data.table can work correctly in those packages. Users can also add to data.table's whitelist themselves using assignInNamespace() but these additions upstream remove the need to do that for these packages.

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.

Creating wordcloud and I got this 'dataptr' not provided by package 'Rcpp'

New to R and have the following question. I got the error below when I was trying to create wordcloud in R. Could anyone tell me what the error means and is there a workaround?
Error in .overlap(x1, y1, sw1, sh1, boxes) :
function 'dataptr' not provided by package 'Rcpp'
That is an error we are getting with the newest Rcpp (which uses a different initialization scheme and no user-facing library). Make sure you have
the current version of Rcpp
and a current / rebuilt version of wordcloud.
On my system, with a fresh install of wordcloud, it all works fine:
R> library(wordcloud)
Loading required package: Rcpp
Loading required package: RColorBrewer
R> example(wordcloud)
wrdcldR> wordcloud(c(letters, LETTERS, 0:9), seq(1, 1000, len = 62))
wrdcldR> if(require(tm)){
wrdcld+
wrdcld+ ##### from character #####
wrdcld+ wordcloud(
wrdcld+ "Many years ago the great British explorer George Mallory, who
wrdcld+ was to die on Mount Everest, was asked why did he want to climb
wrdcld+ it. He said, \"Because it is there.\"
[.... more omitted ...]
After a while, I got it.
1) As stated, reinstall the latest version of Rcpp is the solution.
2) On top of that, if you use a library other than wordcloud that do not load automatically RCPP, do not forget to include
library(Rcpp)
or
require(Rcpp)
on your code before
dyn.load("your_shared_lib.so")
Source:
building_shared_libs_with_Rcpp

Resources