Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testthat fails, yet same code works in a standard test #322

Closed
hofnerb opened this issue Nov 17, 2015 · 3 comments
Closed

testthat fails, yet same code works in a standard test #322

hofnerb opened this issue Nov 17, 2015 · 3 comments

Comments

@hofnerb
Copy link

hofnerb commented Nov 17, 2015

In my package papeR I have the following lines in my code

## overwrite standard generic
labels <- function(object, ...)
    UseMethod("labels")

## per default fall back to standard generic
labels.default <- function(object, ...)
    UseMethod("base::labels")

If I am using

labels(matrix(1:10))

in a standard file in tests, e.g. in tests/regtest-prettify.R everything works as expected and I get

## [[1]]
## [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10"
## 
## [[2]]
## [1] "1"

The same is true if I run the code after installing the package.

Yet, if I put this code in tests/testthat/test-labels.R, I get (essentially) the following error

no applicable method for 'base::labels' applied to an object of class "c('matrix', 'integer', 'numeric')"

Now, I wonder
a) why this happens
b) why this happens only with testthat
c) how I can debug and solve this.

@kevinushey
Copy link
Collaborator

This fails directly for me (ie, is not a testthat issue):

## overwrite standard generic
labels <- function(object, ...)
    UseMethod("labels")

## per default fall back to standard generic
labels.default <- function(object, ...)
    UseMethod("base::labels")

labels(matrix(1:10))

Either way, do you really want to override the generic? Can't you just implement a labels method for your own class?

@hofnerb
Copy link
Author

hofnerb commented Nov 17, 2015

That's true. But

install_github("hofnerb/papeR")
library("papeR")
labels
labels(matrix(1:10))

does work, though.

In essence I currently do not need to overwrite the standard generic. However, I wanted to add a special behaviour such that I can extract variable labels by

df <- data.frame(x = 1:3, y  = 4:6)
df <- as.ldf(df)
labels(df$x)

which should return

attr(df$x, "variable.label")

I was thinking of something along the lines of

labels.default <- function(object, ...) {
    if (!is.null(attr(object, "variable.label"))) {
        return(attr(object, "variable.label"))
    }
    UseMethod("base::labels")
}

which wouldn't change standard behaviour per se. [We had this discussion already elsewhere btw].


However, I was just thinking to also give a new (additional) class to all "columns" of the data frame to allow method dispatch to work.

Still, I am puzzled why this happens that way.

@hadley
Copy link
Member

hadley commented Dec 2, 2015

I think you want base::labels(object, ...), not UseMethod("base::labels"). But I agree with @kevinushey that override a generic is a bad idea.

@hadley hadley closed this as completed Dec 2, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants