R package with dependency on shiny gives RJSONIO warning - r

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

Related

What's the right way to import functions for suggested packages?

Importing other packages' functions within one's own package is easy enough (example), but that requires having a hard dependency (Imports in the DESCRIPTION file) on that package. In my case, it is a suggested package, so I am getting a warning: '::' or ':::' import not declared on R CMD check.
So right now, I am recreating the functions locally, but this is problematic as when both packages are loaded, there is another warning The following objects are masked from [the other package]. They're the same, so it's not a big issue, but enough to be annoying. I can't imagine there's not a better practice than this?
In case we need actual code for demo, here is the problematic imports for my package: https://github.com/rempsyc/lavaanExtra/blob/main/R/save_as_x.R

Conflicted package in R package building

I am creating my first package, here I am making some var estimations, the functions are running, however, I use packages that have the same function names.
Before writing the package I made an R script with the function and tests if it works, but at the top of my script I used the following code:
invisible(lapply(c("tibble","readxl","dplyr","stringr", "tidyr", "vars", "conflicted","forecast", "lubridate"), library, character.only = T))
conflict_prefer("select","dplyr")
conflict_prefer("lag", "dplyr")
conflict_prefer("filter", "dplyr")
The conflicted package chose the functions select, lag, and filter comes from the dplyr package rather from the stats package.
So I have not figured out how to use the conflict_prefer function inside the package.
Should they be the first lines of my function?
There is a roxygen way to prefer same-name functions?
I ask this because I get this warning:
> devtools::load_all()
i Loading FAVAR.MEF
Warning messages:
1: replacing previous import ‘dplyr::filter’ by ‘stats::filter’ when loading ‘FAVAR.MEF’
2: replacing previous import ‘dplyr::lag’ by ‘stats::lag’ when loading ‘FAVAR.MEF’
3: replacing previous import ‘stats::filter’ by ‘dplyr::filter’ when loading ‘FAVAR.MEF’
4: In setup_ns_exports(path, export_all, export_imports) :
Objects listed as exports, but not present in namespace: favar_est
Thanks in advance!!
If you are writing your own package and using external dependencies, you should not load them through repeated calls to library.
The proper way to do it is to state your dependencies in the DECRIPTION file of your package, which will mean that your dependencies are put on the search path in the correct order when your package is loaded. In your case, this removes the need for conflict_prefer, as dplyr will be higher up on the search path than stats. It also makes your package portable, because anyone who installs your package will have any missing dependencies installed automatically according to the packages listed in your DESCRIPTION file. Furthermore, doing it this way allows you to specify a minimum version of the dependency, so that anyone who already has an older version of the dependency installed will not come up against an obscure error when they try to use your package.
The DESCRIPTION file resides in the root directory of your package. It is a simple text file.
You need only add:
Depends:
tibble,
readxl,
dplyr,
stringr,
tidyr,
vars,
conflicted,
forecast,
lubridate
within this file, and your dependencies will be loaded with your package.

Imports - functions with the same name but from different package

I'm working on update of my package. However I'm struggling with dependencies/imports. I use two conflicted packages - ggplot2 and psych and their functions alpha and of course alpha object of ggplot2 differs from alpha function of psych package.
When building package I get an warning:
Warning: replacing previous import 'ggplot2::alpha' by 'psych::alpha' when loading 'ShinyItemAnalysis'
Hence I wont be able to publish my package on CRAN (untill I solved this warning). Is there any easy way how to avoid this import conflict?
Note that when using roxygen2 package to build NAMESPACE from fellow .R scripts, it is very helpful to delete the NAMESPACE file and run roxygen2 again. Solves many problems.

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

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}

How to properly use functions from other packages in a R package

I am a bit confused about this. I have an R package that has a small function (not a mayor part of the package) in which the principal function of the psych package is called. How do I correctly specify this in DESCRIPTION and NAMESPACE?
Setting Depends: psych in DESCRIPTION makes sure the psych package is loaded every time my package is loaded. This works, but it seems redundant for such a small part of my package.
Setting Suggests: psych and entering a require("psych") in the function is what I do now, however this does not work if psych is not installed, and seems to be the wrong way of doing this (writing R extensions says that suggest is meant mainly for examples).
I think I need to import the function. I tried setting Imports: psych in DESCRIPTION and importFrom(psych,"principal") in NAMESPACE. This works, but on a computer that does not has psych installed it gives an error when loading my package.
The basic question you need to answer is: "do you want the function to be available to all users of the package without further effort?". If yes, then use imports + the appropriate namespace declarations, if no, then use suggests and print an informative error message if require("psych") returns FALSE.
I don't understand your import related complaint that: "but on a computer that does not has psych installed it gives an error when loading my package". This is also true if your package is in depends!

Resources