#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)
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 √