.data pronoun and "no visible binding for global variable"

I am trying to eliminate the devtools::check note regarding "no visible binding for global variable", similar to this thread from 2019, using the .data pronoun indicated in programming with dplyr.

Set up

I am working on Windows 10 with the following installations:

Software / package Version
R 4.0.3
RStudio 1.3.1073
devtools 2.3.2
usethis 1.6.3
tidyverse 1.3.0
broom 0.7.1
rlang 0.4.8

Function initial

#' Computes a tidy correlation
#'
#' @param data input data set
#' @param var1 name of variable 1
#' @param var2 name of variable 2
#'
#' @return A tibble with the Pearson correlation and the p-value
#' @export
#'
#' @examples
#' compute_corr(data = faithful, var1 = eruptions, var2 = waiting)
compute_corr <- function(data, var1, var2){

  stats::cor.test(
      x = data %>% dplyr::pull({{var1}}),
      y = data %>% dplyr::pull({{var2}})
    ) %>%
    # tidy up results ----
    broom::tidy() %>%
    # retain and rename relevant bits ----
    dplyr::select(
      correlation = estimate,
      pval = p.value
    )
}

results in this note

-- R CMD check results ------------------------------------- ralph 0.0.0.9000 ----
Duration: 39.6s

> checking R code for possible problems ... NOTE
  compute_corr: no visible binding for global variable 'estimate'
  compute_corr: no visible binding for global variable 'p.value'
  Undefined global functions or variables:
    estimate p.value

0 errors √ | 0 warnings √ | 1 note x

Function modification 1

Here I added @importFrom and .data pronouns.

#' Computes a tidy correlation
#'
#' @param data input data set
#' @param var1 name of variable 1
#' @param var2 name of variable 2
#'
#' @return A tibble with the Pearson correlation and the p-value
#' @export
#'
#' @examples
#' compute_corr(data = faithful, var1 = eruptions, var2 = waiting)
#'
#' @importFrom rlang .data
compute_corr <- function(data, var1, var2){

  stats::cor.test(
      x = data %>% dplyr::pull({{var1}}),
      y = data %>% dplyr::pull({{var2}})
    ) %>%
    # tidy up results ----
    broom::tidy() %>%
    # retain and rename relevant bits ----
    dplyr::select(
      correlation = .data$estimate,
      pval = .data$p.value
    )
}

results in this note

-- R CMD check results ------------------------------------- ralph 0.0.0.9000 ----
Duration: 3s

> checking package dependencies ... ERROR
  Namespace dependency not required: 'rlang'
  
  See section 'The DESCRIPTION file' in the 'Writing R Extensions'
  manual.

1 error x | 0 warnings √ | 0 notes √

Function modification 2

Here I deleted @importFrom and kept .data pronouns.

#' Computes a tidy correlation
#'
#' @param data input data set
#' @param var1 name of variable 1
#' @param var2 name of variable 2
#'
#' @return A tibble with the Pearson correlation and the p-value
#' @export
#'
#' @examples
#' compute_corr(data = faithful, var1 = eruptions, var2 = waiting)
compute_corr <- function(data, var1, var2){

  stats::cor.test(
      x = data %>% dplyr::pull({{var1}}),
      y = data %>% dplyr::pull({{var2}})
    ) %>%
    # tidy up results ----
    broom::tidy() %>%
    # retain and rename relevant bits ----
    dplyr::select(
      correlation = .data$estimate,
      pval = .data$p.value
    )
}

results in this note

-- R CMD check results ------------------------------------- ralph 0.0.0.9000 ----
Duration: 47.4s

> checking R code for possible problems ... NOTE
  compute_corr: no visible binding for global variable '.data'
  Undefined global functions or variables:
    .data

0 errors √ | 0 warnings √ | 1 note x

How can I make this function note free? Thank you.

1 Like

UPDATE: I solved my own problem. :grinning:

Function modification 1 works, provided that rlang is also specified as an import in the description file - can use usethis::use_package("rlang"). That error was really throwing me off!

-- R CMD check results ------------------------------------- ralph 0.0.0.9000 ----
Duration: 43.9s

0 errors √ | 0 warnings √ | 0 notes √```
4 Likes

you should mark your own answer as the solution :wink:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.