I am trying to push the following package to CRAN, but I keep getting an error on the check.
Error:
✓ checking R/sysdata.rda ...
WARNING
‘qpdf’ is needed for checks on size reduction of PDFs
✓ checking installed files from ‘inst/doc’ ...
✓ checking files in ‘vignettes’ ...
E checking examples (3s)
Running examples in ‘oRus-Ex.R’ failed
The error most likely occurred in:
> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: analyseStories
> ### Title: Analysing Stories
> ### Aliases: analyseStories
>
> ### ** Examples
>
> # Transform the stories
> fileUrl <- example_stories()
> stories <- analyseStories(fileUrl, 7)
Joining, by = "word"
Joining, by = "word"
Error in loadNamespace(name) : there is no package called ‘reshape2’
Calls: analyseStories ... loadNamespace -> withRestarts -> withOneRestart -> doWithOneRestart
Execution halted
Current problems:
The example is in orus::analyseStores(...) function.
The example actually runs and works on the pkgdown website.
The error appears only when doing devtools::check
I have tried multiple things:
This answer base::assign(".ptime", proc.time(), pos = "CheckExEnv") ERROR when using devtools::check suggested using dontrun{...}. It passes CRAN's check, but it was bounced by a person after a couple of days.
This answer R package fails devtools::check, because "could not find function" even though the function is imported in NAMESPACE suggested doing require on the missing library. I did require(reshape2) but the check still does not pass.
This answer "Could not find function" in Roxygen examples during CMD check suggests that I need to make all my functions public (exported). I don't want to do that. I tried doing orus:::some_function(...) to call to the non-exported functions inside analyseStores but it doesn't work either.
According to this one: R package build failed when checking examples the data is working and the function has the #export tag. Also, namespace is properly updated.
I have run out of options. Any idea of what is happening?
As #stefan suggested in the comments, I had to add reshape2 into the Suggested packages in the description file. I added using:
usethis::usepackage("reshape2", "Suggests")
Followed by regenerating the docs:
devtools:document()
Package is on its way to CRAN!
Related
Starting Situation:
I'm writing a small package of functions for myself only (not CRAN; on GitHub, but not public), and developing locally on the computer. Mostly this is me being a newbie at R and learning to write first package.
I'm using devtools and after load_all() and check(), I have been getting this "NOTE":
-- R CMD check results -------------------------------------------------------------------------------------------- MondelezR 0.1.0 ----
Duration: 21.1s
> checking dependencies in R code ... NOTE
Namespace in Imports field not imported from: 'tibble'
All declared Imports should be used.
0 errors v | 0 warnings v | 1 note x
Question:
Am I doing something wrong or is this a known/ expected problem that I can ignore?
Little more background:
I am using tibble()
In my package, "Find in Files" shows that I have used tibble in four files in different ways:
DESCRIPTION file:
[First Section of File Omitted]
Encoding: UTF-8
RoxygenNote: 7.2.0
Imports:
stringr,
dplyr,
purrr,
tibble,
magrittr
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
(I have not seen the message for the other imported packages.)
FUNCTION: mdlz_otm_filter.R
[omitted]
#' #examples
#' df_otm_final <- tibble::tibble(
[omitted]
I am only using tibble in the example, not in the function itself, and the relevant portion is shown above.
DOCUMENTATION: mdlz_otm_filter.Rd
The roxygen2 documentation created from the above function shows the exact same example, but as documentation.
TEST THAT: test-mdlz_make_KEY1.R
test_that("POSTAL LANE2 works as expected", {
df_test <- tibble::tibble(ORIG_ZIP = c("18615", "12345", "a5J 1u8"),
DEST_ZIP = c("1234", "23456", "i9y2b4"),
FINAL_KEY = c("18615-01234","12345-23456","A5J1U8-I9Y2B4"))
expect_identical(mdlz_make_POSTAL_LANE(df_test$ORIG_ZIP,
df_test$DEST_ZIP),
df_test$FINAL_KEY)
})
Attempt to remove tibble from DESCRIPTION
I tried removing tibble from Imports: on the DESCRIPTION file, but as I expected would happen, I got this instead:
-- R CMD check results -------------------------------------------------------------------------------------------- MondelezR 0.1.0 ----
Duration: 26.6s
> checking for unstated dependencies in examples ... WARNING
'::' or ':::' import not declared from: 'tibble'
> checking for unstated dependencies in 'tests' ... WARNING
'::' or ':::' import not declared from: 'tibble'
0 errors v | 2 warnings x | 0 notes v
So... warnings are worse than notes I figure.
Research:
Google search to start with brought me to these posts:
RStudio Community Meta-Package This guy's problem is that he needs to use functions in every package he's trying to put in his meta-package. My issue is I'm already using tibble and getting the note regardless.
SO devtools R CMD check NOTE But this one doesn't seem to apply because I AM using tibble in my package, and this guy is trying to remove it.
Help?
I don't know how to clear the note, if I should worry about it at all, or why I'm getting it since I am using tibble as shown above. Trying to learn, so an expository answer is appreciated. Thank you in advance.
It seems devtools check function is looking for an importFrom tag with the package tibble in some of your function documentation roxygen Docs.
Adding #importFrom tibble tibble to the functions documentation which use the library tibble might remove the note.
I'm building a package that imports {sf}, and more specifically I use st_length() in one of my functions.
I initially added only {sf} to my package "Imports", but when I checked it I got a few {lwgeom} related errors:
Running examples in 'gtfstools-Ex.R' failed
The error most likely occurred in:
> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: get_trip_speed
> ### Title: Get trip speed
> ### Aliases: get_trip_speed
>
> ### ** Examples
>
> data_path <- system.file("extdata/spo_gtfs.zip", package = "gtfstools")
>
> gtfs <- read_gtfs(data_path)
>
> trip_speed <- get_trip_speed(gtfs)
Error in sf::st_length(trips_geometries) :
package lwgeom required, please install it first
This error happens when the examples are running, but some similar errors happen with the tests.
Then I added {lwgeom} to Imports. The check runs fine, but in the end I get a note: NOTE: Namespaces in Imports field not imported from: 'lwgeom'
What's the best practice when dealing with cases like this? Should I just keep track of this note and send it as a comment to CRAN during the package submission process?
You can consider adding the {lwgeom} package in Suggests field of your package DESCRIPTION file. It should do the trick.
The Suggests != Depends article by Dirk Eddelbuettel refers to a relevant bit of Writing R Extensions (WRE) that might be useful to this case.
Section 1.1.3.1 (suggested packages) reads (as of 2021-03-12):
Note that someone wanting to run the examples/tests/vignettes may not have a suggested package available (and it may not even be possible to install it for that platform). The recommendation used to be to make their use conditional via if(require("pkgname")): this is OK if that conditioning is done in examples/tests/vignettes, although using if(requireNamespace("pkgname")) is preferred, if possible.
However, using require for conditioning in package code is not good practice as it alters the search path for the rest of the session and relies on functions in that package not being masked by other require or library calls. It is better practice to use code like
if (requireNamespace("rgl", quietly = TRUE)) {
rgl::plot3d(...)
} else {
## do something else not involving rgl.
}
So while just adding {lwgeom} to Suggests works, we may stumble upon the issue where someone that runs a "lean installation" (i.e. without suggested packages) of my package won't be able to use the functions that rely on {lwgeom}.
More importantly, if an author of a package that I am importing decides to run a reverse dependency check on my package while not installing suggested packages, the check would fail because I'd have a few examples, tests and vignettes bits failing due to not having {lwgeom} available.
Thus, in addition to listing it in Suggests, I added some checks on examples and vignettes like suggested by WRE:
*examples/vignette context*
# the examples below require the 'lwgeom' package to be installed
if (requireNamespace("lwgeom", quietly = TRUE)) {
... do something ...
}
In the functions that require {lwgeom} I added:
if (!requireNamespace("lwgeom", quietly = TRUE))
stop(
"The 'lwgeom' package is required to run this function. ",
"Please install it first."
)
And added this bit to the tests of such functions (using {testthat}):
if (!requireNamespace("lwgeom", quietly = TRUE)) {
expect_error(
set_trip_speed(gtfs, "CPTM L07-0", 50),
regexp = paste0(
"The \\'lwgeom\\' package is required to run this function\\. ",
"Please install it first\\."
)
)
skip("'lwgeom' package required to run set_trip_speed() tests.")
}
Recently I built an R package and wanted to publish it in CRAN. I had checked it in my local machine (WIN10 R>=3.6) and it showed there was 0 WARNING, 0 NOTE and 0 ERROR. Then I uploaded it to the CRAN. However, the CRAN CHECK showed that I had one WARNING in LINUX: here is the raw log.
Flavor: r-devel-linux-x86_64-debian-gcc
Check: package dependencies, Result: WARNING
Requires orphaned package: 'flare'
The package flare is in the "Imports". I checked the R POLICIES and found it said "Orphaned CRAN packages should not be strict requirements". However, if I change the dependency to "Suggests", I cannot use the function slim in the package flare. How can I adjust it so that I can pass the CRAN CHECK?
The standard use of a package in Suggests: is to test it before you use it. So for a function bar() from package foo, change your code from
res <- bar(a,b,c) # foo in NAMESPACE as imports
to
res <- NA
if (requireNamespace("foo", quietly=TRUE) {
res <- foo::bar(a,b,c) # package foo in Suggests
} else {
warning("Would need foo for bar") # message optional
}
There is no other way to appease the CRAN check. (Besides adopting the orphaned package but that is a whole different ball game.)
UPDATE
I have completed the package and it is hosted online at https://github.com/iembry-USGS/ie2misc.
Since the error message in the original post was not helpful, I attempted to roxygenize the package to see if that would work or not. Below are the commands and the error message.
library(roxygen2)
roxygenize(".", roclets = "rd")
# First time using roxygen2. Upgrading automatically...
# Error in parse(n = -1, file = file, srcfile = NULL,
# keep.source = FALSE) :
# 1:1: unexpected input
# 1: �
^
I am assuming that the unexpected input is referring to a character, but I don't know which file has the character in question.
Any assistance would be helpful.
Thank you.
UPDATE End
I am working on creating a package that contains 3 functions. I've been successful at creating 3 other packages using 1 function, but not with this package.
I have included the contents of the DESCRIPTION file below. Below that content is the code and the error that I am receiving when attempting to document this package.
Thank you.
Package: ie2misc
Title: Irucka Embry's Miscellaneous functions created while he was a
CNTS USGS Contractor.
Version: 1.0.0
Authors#R: person("Irucka", "Embry", , "", c("aut", "cre"))
Depends: R (>= 3.0.0), tcltk, data.table (>= 1.9.4)
Imports: openxlsx, gWidgets2, gWidgets2tcltk, stringi, qdap
Suggests: Rcpp (>= 0.11.5)
Maintainer: Irucka Embry <iembry#usgs.gov>
Description: Irucka Embry's Miscellaneous functions (processing exp files,
psf files, etc.) created while he was a Cherokee Nation Technology Solutions
(CNTS) USGS Contractor.
URL: https://gitlab.com/iembry/ie2misc
BugReports: https://gitlab.com/iembry/ie2misc/issues
License: CC0
Collate:
'ie2misc.R'
'psfFileChangeBATCH.R'
'psfFileChange.R'
'expFileOutput.R'
LazyData: true
Encoding: UTF-8
Then I run:
setwd("ie2misc"); library(devtools); document();
Updating documentation
Loading
Error in if (pkg$package == "devtools") { : argument is of length zero
I still don't know what the problem is, but I created an empty package with library(devtools). Then, I copied most of the ie2misc files into the newly created package. I also rewrote the DESCRIPTION file in RStudio. (Usually I use the Kate text editor for working on all files for R packages.) Once those steps were completed, I was able to document, check, and build the package.
For instance, I have an environment myEnv which lives in my package Test. So why on earth does getPackageName(myEnv) return the current time "2014-02-03 17:17:23" instead of "Test"??
# In /R/Test.R
myEnv <- new.env()
print(getPackageName(myEnv))
# Now build in RStudio:
==> Rcmd.exe INSTALL --no-multiarch --with-keep.source Test
<other messages here>
** preparing package for lazy loading
[1] "2014-02-03 17:17:23"
Warning in getPackageName(myEnv) :
Created a package name, '2014-02-03 17:17:23', when none found
<etc etc etc>
I don't see this behaviour or its reasoning documented anywhere. Indeed, this can wreak havoc, as clearly demonstrated by this question, hence every time I create an environment I have to remember to do something like setPackageName("Test", myEnv) to associate it with my package.
This just seems superfluous and unnecessary, so why have this behaviour?
Try getting the parent environment of your env instead of the environment itself.
This seems to work at least for a simple example I'm installing using devtools, so who knows what tricks Hadley does with environments in there:
> parent.env(myEnv)
<environment: namespace:Test>
> getPackageName(parent.env(myEnv))
[1] "Test"
This doesn't work for other objects defined in the package:
> foo
function(){
}
<environment: namespace:Test>
> getPackageName(parent.env(foo))
Error in parent.env(foo) : argument is not an environment
you just have to get the environment thus:
> getPackageName(environment(foo))
[1] "Test"