How to avoid loading a deprecated package?

ggplot2
aes

#1

I am trying to develop a R package with RStudio called CF. I discovered that Clean and Rebuild fails because:

  • Error in aes(x = gdpPercap, y = lifeExp, size = pop, color = continent, : could not find function "aes"
    Error : unable to load R code in package ‘CF.

aes is a deprecated package. At the same time, it is a function used by gganimate, ggplot2 and gapminder.

For example, aes(x = gdpPercap, y = lifeExp, size = pop, color = continent, … . aes is a function defined in the gapminder package. The data of x, y, … are also from the gapminder package. The gapminder package loaded into CF and well as gganimate and ggplot2.

Regardless, CF fails to load. The installed libraries are insufficient to stop Clean and Rebuild from attempting to load the deprecated package of aes.

What is code is necessary to avoid loading a deprecated package?


#2

did you try just writing it as ggplot2::aes() inside of your ggplot function? It looks like it is not trying to load the package aes but rather is trying to use a function for which it can not find (unless you included require(ggplot2) in your function, which is not recommended).


#3

No. I have not. I think I follow.

I have a function inside my R package that uses ggplot.

p <- ggplot(dfx, aes(t, x, color = Vehicle, frame = step)) +
geom_path(aes(cumulative = TRUE, group = Vehicle)) +
ggtitle(“Vehicles entering a Bottleneck”)
gganimate(p, title_frame = FALSE)

As you can see, it uses the ggplot function, which is part of the ggplot2 package. So, I will add a line of code aes <- ggplot2::aes().

In addition, I can also remove gapminder from Imports in DESCRIPTION.

I like your approach because gapminder and its data are not used in my package.

By the way, the function describes above works fine in R. My trouble is with RStudio Clean and Rebuild.

I will try your approach, see what happens, and get back to you.

Thanks,

Paul


#4

It is recommended that you explicitly define each functions namespace when you write a function for an R package so you do not run into any errors like the one in your first post. So you ggplot call inside the function should look like this:

p <- ggplot2::ggplot(dfx, ggplot2::aes(t, x, color = Vehicle, frame = step)) +
ggplot2::geom_path(ggplot2::aes(cumulative = TRUE, group = Vehicle)) +
ggplot2::ggtitle(“Vehicles entering a Bottleneck”)
gganimate::gganimate(p, title_frame = FALSE)

#5

No luck. I get the same error:

** preparing package for lazy loading
Error in aes(x = gdpPercap, y = lifeExp, size = pop, color = continent,  : 
  could not find function "aes"
Error : unable to load R code in package ‘CF’
ERROR: lazy loading failed for package ‘CF’
* removing ‘/Users/PJO/Library/R/3.4/library/CF’
* restoring previous ‘/Users/PJO/Library/R/3.4/library/CF’

Exited with status 1.

I am not an experienced developer. It is very likely that I did not execute your approach correctly. I did the following:

  1. aes <- ggplot2::aes()
  2. ggplot(dfx, ggplot2::aes(t, x, color = Vehicle, frame = step))
  3. Used Terminal to remove gapminder from PJO:library PJO$. search() still shows “package:gapminder”
  4. Removed gapminder from DESCRIPTION

Please let me know if I followed your suggestion properly.

I successfully ran my animation function in R with changes made above.

It is bothersome that the data from the gapminder package is still referenced in the Error message above.

Paul


#6

Your error message does not match the ggplot function that you reference. You should define the namespace of all functions that are not from base R in your function. In addition, any package that your reference in your function should be included in your Imports sections of your DESCRIPTION. You should find the line in your function that matches “aes(x = gdpPercap, y = lifeExp, size = pop, color = continent,” and then work from there. A good way to test that you have correctly defined namespace for all non-base R functions (outside of unit tests, which is recommended) is to make heavy use of the Ctrl + Shift + F10 command which will start a new R session in your RStudio. If you load your function into your global environment after using this restart and then run it, it will give you errors similar to the one you are receiving for any function that does not have its namespace defined.

The issue you are likely running into is that when you have the libraries you are using loaded locally, the function works. But you can not assume that all users will always have the same libraries loaded as you. So you want to explicitely state what package the functions you are using are from and then make sure the user has them installed (this is done by adding them to Imports) and then the function knows to use that package whether it is loaded or not (this is done by defining the namespace in your function as package::function()).

You can see that this works by starting a new R session (Ctrl + Shift + F10) and running any non-base command. If you include the namespace (package::function()) then it will work. If you do not include the namespace then it will send you an error like you are receiving (can not find function x)

If you are using datasets from a package, such a gapminder, then you need to reference them in the same way as functions when you first load them. It would look like this gapminder::gapminder


#7

As a short example, see what happens when you run the following code after using the Ctrl + Shift + F10 R restart keyboard shortcut:

select(mtcars, mpg, cyl, disp)
#> Error in select(mtcars, mpg, cyl, disp): could not find function "select"

dplyr::select(mtcars, mpg, cyl, disp)
#>                      mpg cyl  disp
#> Mazda RX4           21.0   6 160.0
#> Mazda RX4 Wag       21.0   6 160.0
#> Datsun 710          22.8   4 108.0
#> Hornet 4 Drive      21.4   6 258.0
#> Hornet Sportabout   18.7   8 360.0
#> Valiant             18.1   6 225.0
#> Duster 360          14.3   8 360.0
#> Merc 240D           24.4   4 146.7
#> Merc 230            22.8   4 140.8
#> Merc 280            19.2   6 167.6
#> Merc 280C           17.8   6 167.6
#> Merc 450SE          16.4   8 275.8
#> Merc 450SL          17.3   8 275.8
#> Merc 450SLC         15.2   8 275.8
#> Cadillac Fleetwood  10.4   8 472.0
#> Lincoln Continental 10.4   8 460.0
#> Chrysler Imperial   14.7   8 440.0
#> Fiat 128            32.4   4  78.7
#> Honda Civic         30.4   4  75.7
#> Toyota Corolla      33.9   4  71.1
#> Toyota Corona       21.5   4 120.1
#> Dodge Challenger    15.5   8 318.0
#> AMC Javelin         15.2   8 304.0
#> Camaro Z28          13.3   8 350.0
#> Pontiac Firebird    19.2   8 400.0
#> Fiat X1-9           27.3   4  79.0
#> Porsche 914-2       26.0   4 120.3
#> Lotus Europa        30.4   4  95.1
#> Ford Pantera L      15.8   8 351.0
#> Ferrari Dino        19.7   6 145.0
#> Maserati Bora       15.0   8 301.0
#> Volvo 142E          21.4   4 121.0

Created on 2018-03-01 by the reprex package (v0.2.0).


#8

Hi Tyler,

Thanks to you, I have made substantial progress on my project. All my plots and animations work using ggplot2::plot type commands.

I devtools::check()’d my R package under various scenarios without some kind of W, N or error. I cannot determine how to treat “mtcars”.

According to Hadley, I should put sysdata.rda in /data-raw folder after executing devtools::use_data(mtcars, internal = TRUE). In addition, he says that sysdata.rda need not be documented. There is a serious

The following is abridged output. I have underlined devtools::check() statements that I don’t know how to resolve them. Given my knowledge of RStudio, I don’t know if the W and N statements are important or not.

Given Hadley’s comments, I am disturbed by the fact that I received multiple warnings about “mtcars”.

Running /Library/Frameworks/R.framework/Resources/bin/R CMD build \
  /Users/PJO/Desktop/Basic --no-resave-data --no-manual
* checking for file ‘/Users/PJO/Desktop/Basic/DESCRIPTION’ ... OK
* preparing ‘Basic’:
* checking DESCRIPTION meta-information ... OK
* checking for LF line-endings in source and make files and shell scripts
* checking for empty or unneeded directories
* looking to see if a ‘data/datalist’ file should be added
* building ‘Basic_0.1.0.tar.gz’
── Checking ────────────────────────────────────────────────────────── Basic ──
W  checking DESCRIPTION meta-information ...
   Non-standard license specification:
     MIT(https://tldrlegal.com/license/mit-license)
   Standardizable: FALSE
N  checking top-level files
   Non-standard file/directory found at top level:
     ‘data-raw’
N  checking R code for possible problems (3.3s)
   test: no visible binding for global variable ‘mtcars’
   test: no visible binding for global variable ‘mpg’
   test: no visible binding for global variable ‘cyl’
   test: no visible binding for global variable ‘disp’
   Undefined global functions or variables:
     cyl disp mpg mtcars
   Consider adding
     importFrom("datasets", "mtcars")
   to your NAMESPACE file.
N  checking examples (43.8s)
   Examples with CPU or elapsed time > 5s
            user system elapsed
   cfanim 25.936  2.959  42.377
   
   See
     ‘/Users/PJO/Desktop/Basic.Rcheck/00check.log’
   for details.
Error: R CMD check found WARNINGs
Execution halted

When I put importFrom("datasets", "mtcars”) in NAMESPACE, I get the following ERROR.

Error : object ‘mtcars’ is not exported by 'namespace:datasets'
ERROR: lazy loading failed for package ‘Basic’

Gratefully,
Paul