character(0) warnings when running devtools::load_all(".") in RStudio - r

I have an R package that I've been building in RStudio, let's called it my_pkg. When I run devtools::load_all(".") within RStudio (specifically using the Ctrl + Shift + L shortcut), I get the following message:
Loading my_pkg
Warning messages:
1: character(0)
2: character(0)
3: character(0)
4: character(0)
5: character(0)
All of the functions in the package work fine. My NAMESPACE and DESCRIPTION files are complete with no syntax errors. When I run ?my_pkg, however, the help file does not match the specifications provided in the DESCRIPTION file. When I remove the Imports from DESCRIPTION, there is no more character(0) warning message. Of course, I need those imports. When I change Imports to Suggests, there is character(0) warning message.
Here is the description file content, with some stuff changed to protect IP.
Package: scoutdroid
Title: This is where the title is.
Version: 0.1
Authors#R: "Ben Hanowell <benjamin.hanowell#redfin.com> [aut, cre]"
Description: This is where the description is.
Depends:
R (>= 3.1.0)
Imports:
dplyr,
lubridate,
mboost,
randomForestSRC,
RODBC,
stringr
License: file LICENSE
LazyData: true
And here is NAMESPACE.
# Generated by roxygen2 (4.0.1): do not edit by hand
import(RODBC)
import(dplyr)
import(lubridate)
import(mboost)
import(parallel)
import(randomForestSRC)
import(stringr)
When I use the RStudio Build & Reload button in the Build tab, I get the following warnings:
** preparing package for lazy loading
Warning: replacing previous import by 'lubridate::intersect' when loading 'scoutdroid'
Warning: replacing previous import by 'lubridate::setdiff' when loading 'scoutdroid'
Warning: replacing previous import by 'lubridate::union' when loading 'scoutdroid'
edit Added some more details to help folks understand what might be going on.
edit 2 I also added the DESCRIPTION file, although I don't provide the full package, which is proprietary.
edit 3 Added NAMESPACE.
edit 4 Added warnings that occur when using RStudio Build & Reload button in the Build tab.

After some dialoge in the comments, we figured out that the empty warnings that load_all is giving you are actually initiated when loading the package because of function name conflicts.
The issue is that you are importing a function from a package, then overwriting that function. When that happens R throws warnings as you saw when you clicked "Build & Reload" in RStudio:
Warning: replacing previous import by 'lubridate::intersect' when loading 'scoutdroid'
Warning: replacing previous import by 'lubridate::setdiff' when loading 'scoutdroid'
Warning: replacing previous import by 'lubridate::union' when loading 'scoutdroid'
It looks like load_all may be attempting to muffle those warnings (just a guess) which is why you see character(0) instead of the actual warnings. (These particular warnings are difficult to silence.)
It is generally not a good idea to import an entire package's namespace. You should instead import only the symbols you need. See this post of mine for more.
The solution is to use importFrom instead of import in your NAMESPACE file.

It can also be due to a broken link in the roxygen2 documentation. For example when you link to a function external to your package with the wrong name, say \link[stringi]{STRI_C} instead of \link[stringi]{stri_c}

Related

Resolve conflicting functions in R package DESCRIPTION file imports

I am trying to build an R package that depends on the following packages: heatmaply, stats, and igraph. I've created a DESCRIPTION file that includes the following:
Imports:
heatmaply,
stats,
igraph
However, when I try to build, I'm getting the following warnings ("myPkg" is a placeholder for my actual package name here):
Warning messages:
1: replacing previous import 'heatmaply::normalize' by 'igraph::normalize' when loading 'myPkg'
2: replacing previous import 'igraph::decompose' by 'stats::decompose' when loading 'myPkg'
3: replacing previous import 'igraph::spectrum' by 'stats::spectrum' when loading 'myPkg'
Notably, I'm not actually using any of the conflicting functions. But because the entire package is listed as a dependency, the conflicts are an issue. Is there an elegant way to solve this? I know that I can use import::from() inline to import only the functions I need, but I prefer not to do that because inline imports are considered poor practice.
I have solved the problem. I was able to fix it by doing the following:
Removing all #import statements at the beginning of the functions I defined.
Including pkgName:: before each function call.

Building binary package R

I am new to R and I am trying to make a standalone executable so that my scripts can be run without development tools. I have created multiple R scripts containing different functions and have been using a main.r script to connect the other scripts. I have been using RStudio and using Source on each file to add them to the Global Environment and finally using Source on my main file to start executing my program. When attempting to build a binary package through:
Build > Build Binary Package
I was getting the error:
ERROR: The build directory does not contain a DESCRIPTION
file so cannot be built as a package.
So I created a package and now the error I get is
** preparing package for lazy loading
Error in reorderPopulation(pop_fitness_list) :
could not find function "reorderPopulation"
Error : unable to load R code in package 'EAtsp'
ERROR: lazy loading failed for package 'EAtsp'
* removing 'C:/Users/Ryan/AppData/Local/Temp/RtmpsXbv0j/temp_libpath27ec59515c59/EAtsp'
Error: Command failed (1)
Execution halted
Exited with status 1.
Can someone explain to me how to fix this problem?
EDIT: I have since added roxygen comments to each of my functions and they are all displaying within the NAMESPACE file but still have the same issue.
These are the files my R directory contains:
fitness.r
initDataset.r
main.r
operators.r
selection.r
The functions within fitness.r can be found from main.r with no problem so I moved the reorderPopulation function which was previously in selection.r to fitness.r and it can be found. Why can the functions inside the selection.r file and possibly the others not be found?
There's nothing reproducible, so I'll go through a hacked example that works, perhaps you can use it as a template for explaining what is different and why yours should still work.
./DESCRIPTION
Package: Porteous96
Title: This package does nothing
Version: 0.0.0.9000
Authors#R: person('r2evans', email='r2evans#ignore.stackoverflow.com', role=c('aut','cre'))
Description: This package still does nothing
Depends: R (>= 3.3.3)
License: MIT
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.0.1
(Go ahead and try to send an email there ... I don't think it'll bug me ...)
./NAMESPACE
After create:
# Generated by roxygen2: fake comment so roxygen2 overwrites silently.
exportPattern("^[^\\.]")
After document:
# Generated by roxygen2: do not edit by hand
export(reorderPopulation)
(Regardless, this file needs no manually editing, assuming you are either using roxygen2 with its #' #export clause, or you are using the default "export almost everything" without roxygen2.)
./R/reorderPopulation.R
#' Do or do not
#'
#' (There is no try.)
#' #param ... any arguments ultimately ignored
#' #return nothing, invisibly
#' #export
reorderPopulation <- function(...) {
cat("do nothing\n")
invisible(NULL)
}
unorderPopulation <- function(...) {
reorderPopulation()
cat("should not be found\n")
invisible(NULL)
}
./R/zzz.R
I added this file just to try to "find" one of the exported functions from within this package.
.onLoad <- function(libname, pkgname) {
reorderPopulation("ignored", "stuff")
}
I can get away with assuming the function is available, per ?.onLoad:
Note that the code in '.onLoad' and '.onUnload' should not assume
any package except the base package is on the search path.
Objects in the current package will be visible (unless this is
circumvented), but objects from other packages should be imported
or the double colon operator should be used.
Build and Execute
I actually started this endeavor with a template directory created by starting in the intended directory and running:
devtools::create(".")
# Creating package 'Porteous96' in 'C:/Users/r2/Projects/StackOverflow'
# No DESCRIPTION found. Creating with values:
# Package: Porteous96
# Title: What the Package Does (one line, title case)
# Version: 0.0.0.9000
# Authors#R: "My Real Name <myreal##email.address.com> [aut,cre]"
# Description: What the package does (one paragraph).
# Depends: R (>= 3.3.3)
# License: Call for information, please
# Encoding: UTF-8
# LazyData: true
# * Creating `Porteous96.Rproj` from template.
# * Adding `.Rproj.user`, `.Rhistory`, `.RData` to ./.gitignore
However, you can easily just use the samples I provided above and move forward without calling create. (It also includes some other files, e.g., ./.gitignore, ./Porteous96.Rproj, and ./.Rbuildignore, none of which are required in the rest of my process here. If you have them and they have non-default values, that might be good to know.)
From there, I edited/created the above files, then:
devtools::document(".")
# Updating Porteous96 documentation
# Loading Porteous96
# do nothing
# First time using roxygen2. Upgrading automatically...
# Writing NAMESPACE
# Writing reorderPopulation.Rd
(The reason you see "do nothing" above and below is that I put it in a function named .onLoad, triggered each time the library is loaded. This includes during devtools::document and devtools::install as well as the obvious library(Porteous96).
One side-effect of that is that a ./man/ directory is created with the applicable help files. In this case, a single file, reorderPopulation.Rd, no need to show it here.
devtools::install(".")
# Installing Porteous96
# "c:/R/R-3.3.3/bin/x64/R" --no-site-file --no-environ --no-save --no-restore \
# --quiet CMD INSTALL "C:/Users/r2/Projects/StackOverflow/Porteous96" \
# --library="C:/Users/r2/R/win-library/3.3" --install-tests
# * installing *source* package 'Porteous96' ...
# ** R
# ** preparing package for lazy loading
# ** help
# *** installing help indices
# ** building package indices
# ** testing if installed package can be loaded
# *** arch - i386
# do nothing
# *** arch - x64
# do nothing
# * DONE (Porteous96)
# Reloading installed Porteous96
# do nothing
For good measure, I close R and re-open it. (Generally unnecessary.)
library(Porteous96)
# do nothing
(Again, this is dumped to the console because of .onLoad.)
reorderPopulation()
# do nothing
unorderPopulation()
# Error: could not find function "unorderPopulation"
Porteous96:::unorderPopulation()
# do nothing
# should not be found
Wrap-Up
I'm guessing this does not solve your problem. It highlights about as much as I could glean from your question(s). Perhaps it provides enough framework where you can mention salient differences between my files and yours. Though answers are not meant for pre-solution discussion, I think it is sometimes necessary and useful.
After help from #r2evans I have managed to find a solution to the problem.
My main.r file was just a bunch of function calls with no function wrapping them. So I wrapped the function calls in a function and the function now looks as follows:
mainFunction <- function() {
source("R/initSetup.r")
initSetup()
...
}
initSetup.r contains more source() calls to the other files that I use. The program is then run using the command mainFunction() in the R console

Building R Package Error: Objects listed as exports, but not present in namespace

I am building R package. Recently, I deleted and renamed several functions in R/allFunctions.R. I had previously been able to automatically update NAMESPACE, but for some reason, I am not able to now, and get some errors as follows:
library(packageName)
library(roxygen2)
library(devtools)
install()
ERROR: loading failed
* removing ‘/Library/Frameworks/R.framework/Versions/3.1/Resources/library/packageName’
* restoring previous ‘/Library/Frameworks/R.framework/Versions/3.1/Resources/library/packageName’
Error: Command failed (1)
document()
Updating packageName documentation
Loading packageName
Warning message:
In setup_ns_exports(pkg, export_all) :
Objects listed as exports, but not present in namespace: functionOne, functionTwo
I see that clearly I have some objects that are not present in namespace that are listed as exports. However, I removed all #export in the allFunctions.R file. I see in NAMESPACE that some newly named function names are not there, and that some old (since renamed) function names are still there. I could change it by hand, but I know that is dangerous, and want to avoid those poor techniques.
If you have any ideas, please let me know! Thank you.
#jtr13's Answer worked for me as well. Just run devtools::document() a second time and the warning goes away.
> devtools::document()
Updating pavm documentation
Loading pavm
Writing NAMESPACE
Deleting evlCalcTime.Rd
Deleting initTimeStamp.Rd
Warning message:
In setup_ns_exports(pkg, export_all) :
Objects listed as exports, but not present in namespace: evlCalcTime,
initTimeStamp
> devtools::document()
Updating pavm documentation
Loading pavm
I just did a similar thing: I deleted 3 exported functions from R/allFunctions.R and ran devtools::document(). This gave me the following error:
Warning message:
In setup_ns_exports(pkg, export_all) :
Objects listed as exports, but not present in namespace: getAccounts, getClients, getDeposits
I solved the problem by manually deleting the 3 export() functions from the NAMESPACE file.
In my experience, this is often a typo-mismatch between function definition and roxygen statement, especially when camelCase notation is involved, for example:
#' #export functionOne
functionone <- function() { ... }

Package development: run_examples could not find function

I'm developing an R package, pk, say. I've successfully installed it and used it, and now I'm trying to clean up the examples with the help of the run_examples function in the devtools package. However, it crashes on the very first example:
> require(pk)
> require(devtools)
> run_examples("~/[full path]/pk")
Updating pk documentation
Running 45 example files in pk
--------------------------------------------------------------------------------
Loading pk
Running examples in pk-package.Rd
--------------------------------------------------------------------------------
1> ########################################################################
1> ## Simulate a dataset ... blah blah
1> set.seed(1)
1> x = my_pk_fun(a = 1)
Error in eval(expr, envir, enclos) : could not find function "my_pk_fun"
Loading pk
As I already loaded my package (using require), what more do I have to do to make my_pk_fun visible to run_examples? I've checked that my_pk_fun is indeed present in my R session.
UPDATE:
Following the comment by Dirk, I examined my namespace file, and found it to be completely empty. As suggested (though not recommended) by the documentation, I inserted exportPattern("^[^\\.]"), which, it seems, is supposed to export all of the functions, including my_pk_fun. However, upon repeating the experiment above, (a) I get the same error, and (b) the contents of the namespace file are deleted! Why does run_examples empty my namespace file?
I conjecture a resolution of my own question:
One detail that I omitted is that before I could get run_examples to do anything at all, it required me to first install the roxygen2 package (even though roxygen2 is listed only under "suggests" in the devtools documentation!).
When digging through the source code of run_examples, I did indeed find some use of roxygenize. It has been previously noted that roxygenizing a package can have the effect of rewriting the NAMESPACE file.
To be explicit, the reason that run_examples empties my NAMESPACE file is that (1) run_examples roxygenized my package while (2) I never included any # #export command (or any other roxygen2-speak, for that matter) in my source files.
Conclusion: Unless you are building your package within the roxygen2 framework, and including all of your .Rd documentation as comments in your source code, then do not use run_examples! It seems like there should be a warning about this in the run_examples documentation.
A way out: If you really must use run_examples, and you're willing to learn a little bit about roxygen, which is actually pretty cool, then start here.

R package with dependency on shiny gives RJSONIO warning

My package CTDesignExplorer uses shiny (and shinyIncubator). When I include
Depends: shiny
in the DESCRIPTION file, there are warnings upon loading the package in RStudio:
Warning in .simpleDuplicateClass(def, prev) :
the specification for S3 class “AsIs” in package ‘RJSONIO’ seems equivalent to one from >package ‘BiocGenerics’ and is not turning on duplicate class definitions for this class
In command line R, loading shiny gets multiples warnings; in addition to "AsIs", with "connect", "file", "pipe", and "textConnection".
At https://github.com/joey711/phyloseq/issues/128, the issue is supposedly solved 6 months ago, but here it is, even after update.packages("RJSONIO"). Version is 1.0-3, from 2013-03-27.
At https://stat.ethz.ch/pipermail/bioc-devel/2013-March/004177.html, the suggestion was to remove RJSONIO from NAMESPACE. But it's not there (or in DESCRIPTION) in my package. In shiny, it's under Imports in DESCRIPTION.
This probably has no impact on code execution, but it's hard to be sure. Anyway, sure is ugly for my users to see.
I think it might help to switch Shiny from Depends to Imports in your package. And, if necessary, use importFrom in your NAMESPACE file to import specific Shiny functions, instead of bringing in all of Shiny.
http://cran.r-project.org/doc/manuals/R-exts.html#Specifying-imports-and-exports

Resources