I think maybe I misunderstood your two examples, but let's take a step back. The first one should not generate warnings because there are already global variables--the argument names. Do you get warnings when you do that? I haven't been able to generate any.
Here's what I just did to double check:
library(usethis)
create_package("testpkg2") # I already have a testpkg ;p
use_mit_license()
use_package("dplyr")
use_r("summary")
in R/summary.R
, I put:
#' Here's a function
#'
#' @param data a data
#' @param group_var some var
#' @param weight_var this one too
#'
#' @export
#'
#' @importFrom dplyr %>%
my_summary_function <- function(data, group_var, weight_var) {
data %>%
dplyr::group_by({{group_var}}) %>%
dplyr::summarise(weighted_count = sum({{weight_var}}))
}
Then, after building the package, I run check. Here's the output, but no warnings generated! Running the function also works as expected.
devtools::check()
devtools::check()
#> Updating testpkg2 documentation
#> Writing NAMESPACE
#> Loading testpkg2
#> Writing NAMESPACE
#> ββ Building ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ testpkg2 ββ
#> Setting env vars:
#> β CFLAGS : -Wall -pedantic
#> β CXXFLAGS : -Wall -pedantic
#> β CXX11FLAGS: -Wall -pedantic
#> ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#> checking for file β/Users/malcolmbarrett/Google Drive/Active/reference/testpkg2/DESCRIPTIONβ ... β checking for file β/Users/malcolmbarrett/Google Drive/Active/reference/testpkg2/DESCRIPTIONβ
#> β preparing βtestpkg2β:
#> checking DESCRIPTION meta-information ... β checking DESCRIPTION meta-information
#> β checking for LF line-endings in source and make files and shell scripts
#> β checking for empty or unneeded directories
#> β building βtestpkg2_0.0.0.9000.tar.gzβ
#>
#> ββ Checking ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ testpkg2 ββ
#> Setting env vars:
#> β _R_CHECK_CRAN_INCOMING_REMOTE_: FALSE
#> β _R_CHECK_CRAN_INCOMING_ : FALSE
#> β _R_CHECK_FORCE_SUGGESTS_ : FALSE
#> β NOT_CRAN : true
#> ββ R CMD check βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#> * using log directory β/private/var/folders/03/9x7925g54mncswxx06wpkxl00000gn/T/RtmpU2g3U5/testpkg2.Rcheckβ
#> * using R version 3.6.1 (2019-07-05)
#> * using platform: x86_64-apple-darwin15.6.0 (64-bit)
#> * using session charset: UTF-8
#> * using options β--no-manual --as-cranβ
#> * checking for file βtestpkg2/DESCRIPTIONβ ... OK
#> * this is package βtestpkg2β version β0.0.0.9000β
#> * package encoding: UTF-8
#> * checking package namespace information ... OK
#> * checking package dependencies ... OK
#> * checking if this is a source package ... OK
#> * checking if there is a namespace ... OK
#> * checking for executable files ... OK
#> * checking for hidden files and directories ... OK
#> * checking for portable file names ... OK
#> * checking for sufficient/correct file permissions ... OK
#> * checking serialization versions ... OK
#> * checking whether package βtestpkg2β can be installed ... OK
#> * checking installed package size ... OK
#> * checking package directory ... OK
#> * checking for future file timestamps ... OK
#> * checking DESCRIPTION meta-information ... OK
#> * checking top-level files ... OK
#> * checking for left-over files ... OK
#> * checking index information ... OK
#> * checking package subdirectories ... OK
#> * checking R files for non-ASCII characters ... OK
#> * checking R files for syntax errors ... OK
#> * checking whether the package can be loaded ... OK
#> * checking whether the package can be loaded with stated dependencies ... OK
#> * checking whether the package can be unloaded cleanly ... OK
#> * checking whether the namespace can be loaded with stated dependencies ... OK
#> * checking whether the namespace can be unloaded cleanly ... OK
#> * checking dependencies in R code ... OK
#> * checking S3 generic/method consistency ... OK
#> * checking replacement functions ... OK
#> * checking foreign function calls ... OK
#> * checking R code for possible problems ... OK
#> * checking Rd files ... OK
#> * checking Rd metadata ... OK
#> * checking Rd line widths ... OK
#> * checking Rd cross-references ... OK
#> * checking for missing documentation entries ... OK
#> * checking for code/documentation mismatches ... OK
#> * checking Rd \usage sections ... OK
#> * checking Rd contents ... OK
#> * checking for unstated dependencies in examples ... OK
#> * checking examples ... NONE
#> * DONE
#> Status: OK
#> ββ R CMD check results ββββββββββββββββββββββββββββββββ testpkg2 0.0.0.9000 ββββ
#> Duration: 14.4s
#>
#> 0 errors β | 0 warnings β | 0 notes β
my_summary_function(iris, Species, Sepal.Length)
library(testpkg2)
my_summary_function(iris, Species, Sepal.Length)
> # A tibble: 3 x 2
#> Species weighted_count
#> <fct> <dbl>
#> 1 setosa 250.
#> 2 versicolor 297.
#> 3 virginica 329.
That's what I'd expect because those names already exist in the function as argument names.
The second example uses .data
but it actually doesn't need to, as I think you've surmised. Basically, .data
is for when you already know the name (although users can supply strings to .data
, eg. function(x = "some_var") .data[[x]]
).
When I say I'm moving more towards .data
, I mean it more in the sense of the programming with dplyr example, where I want to work with known variables that I would refer to by their bare names were I to be doing normal data analysis and not package dev. When users are supplying variables to work with, the approach you have should already work.
These approaches are also not in conflict. Here's an example where I group by Species
using .data
but then sum counts using a user-given variable.
# in the console:
use_package("rlang")
#' Here's another function
#'
#' @param data a data, probably iris
#' @param weight_var this one too
#'
#' @export
#'
#' @importFrom dplyr %>%
#' @importFrom rlang .data
summarize_by_species <- function(data, weight_var) {
data %>%
dplyr::group_by(.data$Species) %>%
dplyr::summarise(weighted_count = sum({{weight_var}}))
}
This generates no warnings in check()
, and it also works
summarize_by_species(iris, Sepal.Length)
library(testpkg2)
summarize_iris(iris, Sepal.Length)
#> # A tibble: 3 x 2
#> Species weighted_count
#> <fct> <dbl>
#> 1 setosa 250.
#> 2 versicolor 297.
#> 3 virginica 329.