no visible global function definition for 'one_of'`

165 views
Skip to first unread message

Ayala Allon

unread,
Aug 18, 2015, 8:10:59 AM8/18/15
to Israel R User Group

Hey all,


I am running R CMD check using devtools::check() for my package and I encountered the following NOTE in the check:

* checking R code for possible problems ... NOTE prep: no visible global function definition for 'one_of'


The only place in which I use one_of in prep() is with this line:

raw_data <- dplyr::select(raw_data, -one_of(drop_vars))


Does anyone knows how can I solve this NOTE?

Bellow is my DESCRIPTION and NAMESPACE files.

Any help would be greatly appreciated


I also asked this in Stackoverflow 

http://stackoverflow.com/questions/32069800/no-visible-global-function-definition-for-one-of


Best,

Ayala

Here is how my DESCRIPTION file looks like:

Package: prepdat
Title: xxx
Version: 0.0.0.9000
Authors@R: person("Ayala S.", "Allon", email = "ayala...@gmail.com", role = c("aut", "cre"))
Description:xxx
Depends: R (>= 3.0.3)
License: GPL-3
LazyData: true
Imports: dplyr (>= 0.4.2),
    reshape2 (>= 1.4.1),
    psych(>= 1.5.4)
Suggests: knitr,
    testthat

And here is how my NAMESPACE file looks like

importFrom(dplyr,"%>%")
importFrom(psych,"harmonic.mean")
exportPattern("^[^\\.]")

Tal Galili

unread,
Aug 18, 2015, 8:26:01 AM8/18/15
to israel-r-...@googlegroups.com
Hi Ayala,

I believe you could use:

raw_data <- dplyr::select(raw_data, -dplyr::one_of(drop_vars))


Or add dplyr to your depends or imports in the DESCRIPTION file. But since it sounds like you are using dplyr only for an example, I would probably add it to the suggested field, and use the code I provided above.


Best,
Tal




----------------Contact Details:-------------------------------------------------------
Contact me: Tal.G...@gmail.com | 
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English)
----------------------------------------------------------------------------------------------


--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ayala Allon

unread,
Aug 18, 2015, 8:34:30 AM8/18/15
to Israel R User Group
Hi Tal,

Thank you for your answer.

When I try your suggestion I still get a NOTE:

* checking dependencies in R code ... NOTE
Missing or unexported object: 'dplyr::one_of' 

I did listed dplyr in the Import section of the DESCRIPTION file. Does that mean I don't need to use ::? Meaning I can do:
 raw_data <- select(raw_data, -dplyr::one_of(drop_vars))


Thanks,

Ayala
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-group+unsub...@googlegroups.com.

Tal Galili

unread,
Aug 18, 2015, 8:53:32 AM8/18/15
to israel-r-...@googlegroups.com
Hi Ayala,

Looking at this further, I see that one_of is indeed not exported from dplyr. That is, I get the following if running in R:

> dplyr::one_of
Error: 'one_of' is not an exported object from 'namespace:dplyr'

This means that I have to go inside the NAMESPACE to access it:
> dplyr:::one_of
function (vars, ...) 
{
    keep <- c(...)
    stopifnot(is.character(keep))
    match(keep, vars)
}
<environment: namespace:dplyr>


In such a case, what you could do is write the following (which should solve your problem):



# from: dplyr:::one_of
one_of <- function (vars, ...) 
{
    keep <- c(...)
    stopifnot(is.character(keep))
    match(keep, vars)
}
raw_data <- dplyr::select(raw_data, -one_of(drop_vars))



Just to clarify - R is right.
You are having your package depend on a function that is not officially supported in the general scope of the package (since this function is not exported).
This could actually be a question for Hadley to know why he did not export the function. But assuming you don'y want to go too deep into this, I would recommend you to just use the code I wrote above.

Tal






----------------Contact Details:-------------------------------------------------------
Contact me: Tal.G...@gmail.com | 
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English)
----------------------------------------------------------------------------------------------


To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.

Ayala Allon

unread,
Aug 18, 2015, 2:06:20 PM8/18/15
to Israel R User Group
Hi Tal,

Sure I do.
I will post a question in the dplyr google group (unless you have a better idea about where to post a question like this)

Thanks

Ayala
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-group+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-group+unsub...@googlegroups.com.

Ayala Allon

unread,
Aug 19, 2015, 4:40:54 AM8/19/15
to Israel R User Group
BTW

For now i decided not to use dplyr::one_of in my code. 

drop_vars is still a string vector (e.g drop_vars = c("prime_type", "font_size") with the name of the columns you want to drop
 
This is what I did instead:

    index_col <- c()
    # Reset i to 1
    i <- 1
    while (i <= length(drop_vars)) {
      index_col[i] <- which(colnames(raw_data) == drop_vars[i])
      i <- i + 1
    }
    keep_col <- -index_col
    raw_data <- raw_data[, keep_col]

It works great. The idea to use which I got from answer #2 in http://stackoverflow.com/questions/4427234/get-column-index-from-label-in-a-data-frame

Ayala
Message has been deleted

עמרי מנדלס‎

unread,
Aug 20, 2015, 3:12:47 AM8/20/15
to Israel R User Group
Hi Ayala,
No need for loops here. Instead you could use the %in% operator:

#dummy data
raw_data <- data.frame(a = runif(3),b = rnorm(3),prime_type = c('aa','bb','cc') , font_size=c(5,6,7))
drop_vars <- c("prime_type", "font_size")

raw_data <- raw_data[,!(colnames(raw_data) %in% drop_vars)]

Hope it helps,
Omri

Ayala Allon

unread,
Aug 20, 2015, 3:43:10 AM8/20/15
to Israel R User Group
Hi Omri,

Thanks for your suggestion. It is much shorter than mine so I'll use it. 
It works great.

Ayala
Reply all
Reply to author
Forward
0 new messages