Incompatibility between ggplot2 and other packages in R? - r

I am revising a paper for submission and would like to replace the old lattice graphics with shiny new ggplot2 versions. However, I run into compatibility problems between ggplot2 and two packages that are absolutely crucial for my analyses, coin and arm. When executing the following example from the manual
qplot(sleep_rem / sleep_total, awake, data = msleep)
I get an error message saying:
Error in function (classes, fdef, mtable) :
unable to find an inherited method for function "empty", for signature "data.frame"
as soon as either coin or arm are loaded.
Here are the details:
Running R 2.10.1, empty .RData file
require(ggplot2)
require(xtable)
require(MASS)
require(gdata)
require(car)
require(Hmisc)
require(psych)
qplot(sleep_rem / sleep_total, awake, data = msleep)
require(coin)
qplot(sleep_rem / sleep_total, awake, data = msleep)
require(arm)
qplot(sleep_rem / sleep_total, awake, data = msleep)
Is this reproducible with R 2.12? If not, might it be worth upgrading? I must confess I am loath to update a working system, especially when on a tight deadline.

It's only the coin package that seems incompatible. Reason is that one of the packages loaded together with coin (modeltools to be exact) also contains a function empty() that masks the function empty() of plyr. Updating to R 2.12 won't change a darn thing, as both packages are contributed by other authors. You should mail the author of the ggplot package and tell him to specify plyr::empty() in his code. Or even the guys from the plyr package, as the problem might actually be there.
A possible hack could be :
require(coin)
empty <- plyr::empty
qplot(sleep_rem / sleep_total, awake, data = msleep)
rm(empty)
This works on 2.11.1 using following data frame:
msleep <- data.frame(
sleep_rem=c(1,2,3,4,5),
sleep_total=c(10,20,30,40,50),
awake=c(5,4,3,2,1)
)
Make sure you remove empty from the workspace right after plotting. Otherwise you might get trouble with the other packages that are loaded. You'll have to do that with every plot you make after you loaded the package coin.

Related

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.

Check for Null Values in R tis

I rarely use R, but need for a helper function that somebody has given me. I have the following line:
league_model_19 <- gam(SHOT_MADE_FLAG ~ ti(LOC_X) + ti(LOC_Y) + ti(LOC_X, LOC_Y), data = shots)
But, when I run it, the following error message is displayed:
Error in ti.default(LOC_X) :
'tif' and 'freq' cannot both be NULL if 'x' is not a ti
I saw online somebody write code along the lines of:
if(is.null(tif)){
if(is.null(freq))
stop("'tif' and 'freq' cannot both be NULL if 'x' is not a ti")
else tif <- freq2tif(freq)
}
But I don't know how to integrate this into my current line, or if there is a more efficient way to do this. Does anybody know how to get rid of this error message so I can proceed with my code? Let me know if more details are needed.
Edit: Added minimal dataframe to reproduce error (player_id and player_name aren't used here, just left in there). This is the data that is in 'shots' as called in the line of code I shared:
PLAYER_ID,PLAYER_NAME,LOC_X,LOC_Y,SHOT_MADE_FLAG
1627759,Jaylen Brown,-20,19,0
1627759,Jaylen Brown,-29,168,1
1627759,Jaylen Brown,7,30,1
1627759,Jaylen Brown,9,6,0
1627759,Jaylen Brown,-176,186,0
Sounds like you either don't have the mgcv package loaded, or you have the tis package loaded after mgcv and the former is masking the latter.
If you didn't load mgcv, add library('mgcv') to your script before fitting the gam().
If you have mgcv loaded but also loaded the tis package, both packages provide a ti() function and it seems the tis one is being found first on the search path. Your best option is to use the fully qualified name for the ti() so that you inform R as to which ti() you want to use.
Try:
league_model_19 <- gam(SHOT_MADE_FLAG ~ mgcv::ti(LOC_X) +
mgcv::ti(LOC_Y) +
mgcv::ti(LOC_X, LOC_Y),
data = shots)
You could also change the script to load mgcv after tis but this might cause later problems when you use the ti() from the tis package; in which case you could use tis:ti() in those places.

"Points" not available for .C() for package "dprep"

I want to perform discretization in order to perform mutual information based feature selection for supervised learning R.
library(dprep)
data(iris)
iris.discme=disc.mentr(my.iris,1:5)
I am getting following error:
Error in .C("Points", as.double(x), as.integer(n), mpoint = double(n),
: "Points" not available for .C() for package "dprep"
Any help would be appreciated.
This looks like a bug in the library "dprep". The error is telling you that there is no C function called "Points", even though disc.mentr() attempted to call this function. I would notify the authors of dprep.
I just looked on CRAN and did not see a library called "dprep". If it was available earlier, that may be a sign that it has been taken down.

ClusterLongData kml package export to scv

I am clustering time series in R using package KmL. I have read both manual and paper how to use this package, but Im not very clear how to export the results (data frame, where each trajectories are assign to some clusters e.g.
trajectory (i), time1, time2, time3, clustername)
I have read several answers here Output from 'choice' in R's kml
but if I do the same (run choice(myCld, typeGraph= "bmp")) my R says:
~ Choice : menu ~ 'Arrow' : change partition 'Space' : select/unselect a partition ... etc. e : change the display (both)
~ 'Return' when its done ~
And only thing that is saved to my library is myCld.Rdata and it runs for very long time without any more results. (my dataset: N trajectories, with time= 1:53) I want to have csv. files as manual suggest (objectName-Cx-y-Clusters.csv)
I am also not very clear WHERE should I PRESS on "Return" or "Arrow"? There is no option to press on anything in my Rstudio workplace.
I am really a beginner with R so any help would be appreciated. Thanks!
I am not sure that kml is compatible with Rstudio. The older version of Rstudio did not handle instruction like getGraphicsEvent that was used by choice.

pscl package of R

I was running the xyplot of presidential elections in the pscl package and got the error
data(presidentialElections)
library(lattice)
xyplot(demVote ~ year | state,
panel=panel.lines,
ylab="Democratic Vote for President (percent)",
xlab="Year",
data=presidentialElections,
scales=list(y=list(cex=.6),x=list(cex=.35)),
strip=strip.custom(par.strip.text=list(cex=.6)))
Error:
Error in recordGraphics(drawGrob(x), list(x = x), getNamespace("grid")) :
invalid graphics state
I am a new user to R and would appreciate if anyone can help me to fix the error.
Try dev.off() and then re-run the same code.
I personally use tinn r to program in R and find it very useful, you can close/open R easily and still send command lines in. (you can even DL different versions of R to load for different packages)

Resources