4

I try to build an R-package that uses the library tidyverse.

The description file looks like the following:

Package: myFirstPackage
Title: A initial package
Version: 0.0.1.0
Authors@R: 
    person(given = "Test",
           family = "Test",
           role = c("aut", "cre"),
           email = "first.last@example.com",
           comment = c(ORCID = "YOUR-ORCID-ID"))
Description: a description.
Imports: tidyverse
License: GPL-2
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1

So I added tidyverse to the Imports section.

My R-Code looks like the following:

myfunction<-function(){

  x<-tibble(
    id = c(1, 2, 3, 4),
    name = c("Louisa", "Jonathan", "Luigi", "Rachel"),
    female = c(TRUE, FALSE, FALSE, TRUE)
  )
  x %>% pull(id)
}

#' MyDemoFunction
#'
#'
#' @import tidyverse
#' @export

say_hello<-function(){
  cat("here1")
  myfunction()
}

Also here, I have the import.

I am working with R-Studio and as soon as I pressed Check, I get:

> checking R code for possible problems ... NOTE
  myfunction: no visible global function definition for 'tibble'
  myfunction: no visible global function definition for '%>%'
  myfunction: no visible global function definition for 'pull'
  myfunction: no visible binding for global variable 'id'
  Undefined global functions or variables:
    %>% id pull tibble

You find the repo of the project here.

In that question the problem was solved by importing function by function but I want to have complete import of the package: I simply want to use all function of tidyverse without defining each of them explicitly. In the question here it was mentioned that the import has to be in the description which I have already!

user3579222
  • 1,103
  • 11
  • 28
  • you could create a file with globals – akrun Jun 06 '21 at 19:13
  • I added an addtional r file with the following content if(getRversion() >= "2.15.1") utils::globalVariables(c(".")) However, it doesn' t help (see https://github.com/anewruser/r_package_project/blob/master/R/globalR.R) – user3579222 Jun 06 '21 at 19:16

1 Answers1

2

My solution for a clean check. You need R 4.1.0 to use |> operator.

plainSrc.R:

####This File represents a package used for Index Management

### Overall approach of this package ###
# In order to avoid a static hard coding of the index members
# we use a web-based approach based on finanzen.de from there we
# get the index members for which we offer the data.

#' @import dplyr
#'
myfunction<-function(){

  x <- dplyr::tibble(
    id = c(1, 2, 3, 4),
    name = c("Louisa", "Jonathan", "Luigi", "Rachel"),
    female = c(TRUE, FALSE, FALSE, TRUE)
  )
  x |> dplyr::pull(one_of("id"))
}

#' MyDemoFunction
#'
#'
#' @export
#'
say_hello<-function(){
  cat("here1")
  myfunction()
}

and change Imports in DESCRIPTION from tidyverse to dplyr.

polkas
  • 3,797
  • 1
  • 12
  • 25
  • works, but why is tidyverse not working - it should be a super set of dplyr (https://dplyr.tidyverse.org/#installation) – user3579222 Jun 06 '21 at 19:45
  • 3
    TL;DR tidyverse is simple wrapper to make a few library at once. This is quite tricky to understand. `tidyverse` is not targeted to be used inside other packages. It is a package which offer an easy way to library many packages at once, check the onAttach function (invoked when library(tidyverse)) - https://github.com/tidyverse/tidyverse/blob/master/R/zzz.R. The functions e.g. from dplyr could not be accessed by :: inside tidyverse. BTW. be careful to NOT use library at .R files inside package. Sometimes we use `if (!require("package")) stop("you need x to proceed")`. – polkas Jun 06 '21 at 19:54