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 gapminderpackage. The data of x, y, ... are also from the gapminderpackage. 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?
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).
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)
** 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:
aes <- ggplot2::aes()
ggplot(dfx, ggplot2::aes(t, x, color = Vehicle, frame = step))
Used Terminal to remove gapminder from PJO:library PJO$. search() still shows "package:gapminder"
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 gapminderpackage is still referenced in the Error message above.
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
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’