2

#create package

devtools::create("./brainy",rstudio=F)

usethis::use_package("dplyr", type = "import", min_version = TRUE) ## set dependancy

I've a small function where I use %>% from dplyr. Also I use . when piping. I do not know where is . imported from or how to achieve its alignment when check()ing the package

My small R code looks like in :

add<-function(x,y){ 
#' Package provides sample data that are loaded with package loading. 
#' @param a and y are variables
#' #not RUN
#' @importFrom dplyr %>%

data_test<-data.frame(ID=c(seq(1:10)), NAME=c(paste("ID",seq(1:10),sep="_")))

data_test$NAME<-data_test$NAME %>%  gsub("_*","",.) # for getting warning about .

return(x+y)

}

There is a test data I create for asking the question and to reproduce it.

I run following things:

devtools::document()  
devtools::check()

I get warning as:

  add: no visible binding for global variable ‘.’
  Undefined global functions or variables:
    .

How do I set my code where it achieves agreement with check() with the .. The resource below suggests to put into the globalVariables, but where (function, file) do I set the global variable at?

Hopefully, last question - which of the following should I use and why:

usethis::use_package("dplyr", type = "Depends", min_version = TRUE)

usethis::use_package("dplyr", type = "import", min_version = TRUE)

https://www.r-bloggers.com/no-visible-binding-for-global-variable/

Tool versions

dplyr_1.0.0
usethis_1.6.1
devtools_2.3.1
rmarkdown_2.3
R 4.0.0 (2020-04-24)

Death Metal
  • 830
  • 3
  • 9
  • 26
  • 1
    Try `utils::globalVariables`? I have however not met issues with using `.` before. What's your `dplyr` version? See an example [here](https://github.com/Nelson-Gon/mde/search?q=global&type=). For your Depends vs imports question, only use Depends if your package cannot work without `dplyr` or if it significantly depends on `dplyr`. – NelsonGon Aug 06 '20 at 16:46
  • Hi, thanks for your response, yes my library cannot function without dplyr, I'll have `depend` within it. I've edited my post with addition of R libraries and their versions. – Death Metal Aug 06 '20 at 17:11
  • 1
    Looks fine although I personally would put it in imports since most people will have `dplyr` either way. Hadley is [quoted](https://stackoverflow.com/a/31962488/10323798) as suggesting `Imports`. Do you have a link to your package's code? It's a bit odd that you need to define `.` globally. – NelsonGon Aug 06 '20 at 17:15
  • No, I don't have link to the package. It is a small package. Would you be able to follow the steps I performed above from create and a script `small_function.R` with code and function name as is to reproduce? – Death Metal Aug 06 '20 at 17:22
  • 1
    Oh, I see what the issue is. Why do you want to use `.` with `gsub`? It can only be understood inside `dplyr` functions and not outside. Here `gsub` would expect something like dataframe names or just df$NAME. If you want to stick to `tidy` syntax, then use `mutate` and friends with `stringr` but this adds more dependencies. And maybe do not use `$` if you want to use the pipe? – NelsonGon Aug 06 '20 at 17:28
  • I see, makes sense. I edited code for `gsub`. However, that is only tip of the ice-berg in the function I've many lines of code where `.` is used while processing data of `data.table` class. For example: `dt_gene<-data.table(ID=c(seq(1:10)),SNP=c(paste("SNPA",seq(1:10),sep="_")))` `dt_gene %>% .[, c("SNP") := "SNP_names" ] ` – Death Metal Aug 06 '20 at 17:59
  • @NelsonGon if you can post your comment as an answer I'll accept it. – Death Metal Aug 07 '20 at 13:24
  • 1
    I have added an answer. It somehow became more complicated than I initially thought. – NelsonGon Aug 08 '20 at 16:18

1 Answers1

1

As requested, I turn my comments into an answer since these may also help someone else in the future.

It is normally possible to go around the no visible binding for global variable error by defining the variable with utils::globalVariables. Unfortunately that won't work with ..

Instead, one should stick to tidyverse-like syntax and use alternatives like mutate or .data. In tidyverse style, add(named add_fn here to avoid name conflicts) can be rewritten as follows:


add_fn<-function(x,y){
  #' Package provides sample data that are loaded with package loading.
  #' @param x some variable
  #' @param y another variable
  #' @importFrom dplyr %>% mutate


  data_test<-data.frame(ID=c(seq(1:10)),
                        NAME=c(paste("ID",seq(1:10),sep="_")))

  data_test<-data_test %>%
       mutate({{NAME = gsub("_*","",NAME)}})

  return(x+y)

}

The above uses {{}} from rlang to parse NAME via non standard evaluation.

Alternatively, in this specific example, one does not need to use the tidyverse at all:

add_fn<-function(x,y){
  #' Package provides sample data that are loaded with package loading.
  #' @param x some variable
  #' @param y another variable
  #' @importFrom dplyr %>% mutate


  data_test<-data.frame(ID=c(seq(1:10)),
                        NAME=c(paste("ID",seq(1:10),sep="_")))

  data_test$NAME<-gsub("_*","",data_test$NAME)

  return(x+y)

}

Check results:


-- R CMD check results ------------------------------------- brainy 0.0.0.9000 ----
Duration: 1m 38.7s

0 errors √ | 0 warnings √ | 0 notes √


NelsonGon
  • 13,015
  • 7
  • 27
  • 57