aphalo / ggpmisc

R package ggpmisc is an extension to ggplot2 and the Grammar of Graphics
https://docs.r4photobiology.info/ggpmisc
94 stars 6 forks source link

Error with inherited method #33

Closed johnnl15 closed 1 year ago

johnnl15 commented 1 year ago

receiving error message with stat_poly_eq:

Warning message: Computation failed in stat_poly_eq(): unable to find an inherited method for function ‘isa’ for signature ‘"formula"’

Would appreciate advice on this issue. Thank you.

aphalo commented 1 year ago

@johnnl15 Thanks for the report! Could you please let me know what version of R and ggplot2 you are using as well as 'ggpmisc' version. If possible, I would appreciate if you provide a simple example or reprex that triggers the error. This looks like a bug, but I haven't seen this error during testing, so some additional information would help me track down the origin of the problem.

johnnl15 commented 1 year ago

Thank you for your response, Dr. Aphalo. Here's my example below. I have yet to solve it but manually added using geom_text with my equation/correlations, much less efficient than your method. I recently installed WGCNA package and had to update alot of packages so I wonder if this created conflicts.

Script: R.version.string

library(ggpmisc) #ggpmisc version 0.5.1 library(ggplot2) #ggplot2 version 3.3.6 my.formula <- y ~ x

df<-data.frame(x = c(5, 8, 7, 4, 3, 9, 6, 1, 10, 2), y = 1:10)

ggplot(df,aes(x = x,y= y))+ geom_point()+ geom_smooth(method = "lm", se=FALSE,formula = my.formula) + stat_poly_eq(formula = my.formula, aes(label = paste(..eq.label..,..adj.rr.label.., ..p.value.label..,sep = "~~~")), size=2)

Output:

R.version.string [1] "R version 4.0.4 (2021-02-15)"

library(ggpmisc) #ggpmisc version 0.5.1 library(ggplot2) #ggplot2 version 3.3.6 my.formula <- y ~ x

df<-data.frame(x = c(5, 8, 7, 4, 3, 9, 6, 1, 10, 2), y = 1:10)

ggplot(df,aes(x = x,y= y))+geom_point()+geom_smooth(method = "lm", se=FALSE,formula = my.formula) +stat_poly_eq(formula = my.formula, aes(label = paste(..eq.label..,..adj.rr.label.., ..p.value.label..,sep = "~~~")), size=2) Warning message: Computation failed in stat_poly_eq(): unable to find an inherited method for function ‘isa’ for signature ‘"formula"’

aphalo commented 1 year ago

I run your example without getting an error. I am on R 4.2.1. And the package is also tested with R 4.1.0 series. Before I install R 4.0.4 to test with it, could you please check if you have defined a function with name isa() that overrides the isa() method from base R?

For example run base::isa(x ~ y, "formula") which should return TRUE and isa(x ~ y, "formula") which may trigger an error if another isa() function is called. This is to confirm that I have fixed in the code for the next version the bug you reported.

The bug in the package code is that I did not add base:: to ensure that the correct version of the function is called even if a new definition is created either by the user or attached from another package.

Many thanks for reporting this bug, your help is much appreciated. Next time, please, do not close an issue unless you are sure the problem is not in the package, even if you have found a work-around. Bug reports are of great help in making code error free. In this case, the bug was in code I just added to the most recent version of 'ggpmisc'.

johnnl15 commented 1 year ago

I ran on my windows computer (R 4.0.4) version and get this:

base::isa(x ~ y, "formula") Error in get(name, envir = ns, inherits = FALSE) : object 'isa' not found

Then I tried on my mac computer (R 4.1.1) and get this:

base::isa(x ~ y, "formula") [1] TRUE

I believe it is an issue with any R version < 4.1, perhaps not having isa. So perhaps recommeding for users to update above 4.1 is necessary. Thank you so much Dr. Aphalo!

aphalo commented 1 year ago

Thanks a lot! I will make R 4.1.0 a requirement, or change the code to make it work with earlier versions. I leave the issue open until I fix the code.

aphalo commented 1 year ago

Replaced isa() with inherits() which should work with R < 4.1.0. Testing is pending.