Import '%>% from magrittr package


#1

I’m building my first package. For now it only contains one function:

#' Cleans GA Data
#'
#' Takes ga_data and cleans it
#' @param x a data frame from GA API
#' @inheritParams tidyr::separate
#' @importFrom magrittr `%>%`
#' @return New source and medium columns
#' @export


ga_clean_data <- function(data) {

  data <- data %>%
    separate(sourceMedium, into = c("source", "medium"), sep = "\\/")

  return(data)

}

But I’m getting the following error:

Error in data %>% separate(sourceMedium, into = c("source", "medium"),  : 
  no se pudo encontrar la función "%>%"

May you tell me why is this happening?


#2

Hi @o_gonzales, is "importFrom magrittr %>%" in your NAMESPACE file too?


#3

Hi Edgar, thank you for your answer.

I only have:

exportPattern("^[[:alpha:]]+") in NAMESPACE.

I was having the same issue with “separate” from Tidyr not been found, and solved it with:

@inheritParams tidyr::separate

So I thought I could solve the magrittr error in the same way, but apparently not. May you tell me exactly what to add on “NAMESPACE”?


#4

Ok, I just let roxygen2 write the NAMESPACE for me, so I know it’s correct, this reference may be of help: http://r-pkgs.had.co.nz/namespace.html


#5

@edgararuiz I’m come across another issue.

Now, my NAMESPACE is generated with this:

# Generated by roxygen2: do not edit by hand

export(ga_clean_data)
export(square)
importFrom(magrittr,"`%>%`")

But when I do “check” on it, I get:


==> devtools::check()

Updating googleAnalyticsReports documentation
Loading googleAnalyticsReports
Warning: object ‘`%>%`’ is not exported by 'namespace:magrittr'

May you take a guess on that? Thank you.


#6

Oh, that’s because we need to remove the single tick from the reference, try:

#' @importFrom magrittr %>%

I just tested that to confirm


#7

Solved, thank you for your guidance :clap:


#8

As described in this quoted response from @mara, if your question has been answered, please mark the reply that answered it as the solution. Thanks!