how to solve "no visible binding for global variable" note?

I see this has been solved but here is a dplyr/rlang specific suggestion: you can use the .data pronoun cf this dplyr vignette,

"If this function is in a package, using .data also prevents R CMD check from giving a NOTE about undefined global variables (provided that you’ve also imported rlang::.data with @importFrom rlang .data )."

  dens <- data.frame(stats::density(x, n=2^10, adjust=1)[c("x","y")]) %>%
    dplyr::mutate(section = cut(.data$x, breaks=breaks)) %>%
    dplyr::group_by(.data$section) %>%
    dplyr::mutate(prob = paste0(round(sum(.data$y)*mean(diff(.data$x))*100),"%"))

Note that I don't think the code you posted generates the NOTE for prob, you must use prob somewhere else as well?

22 Likes