Import function from package into new package namespace

In my package I have redefined the find.package function to include custom search locations and would like data() to use my version of find.package() instead of the base version.

I have tried importing utils::data (as below). I have tried assigning data <- utils::data in my package but the namespace of my_package::data() is still utils. I have tried using conflicted.

#' Load data function
#'
#' @name data
#' @rdname data
#' @keywords internal
#' @export
#' @importFrom utils data
#' @usage data(foo)
NULL

If I copy/paste the utils::data() into my package the my_pkg::find.package() is used because the namespaces are the same. But I would prefer not to have an explicitly-coded copy of data in my package.

I just cannot find how (or if) I can import the utils::data() into my package's namespace or convince utils::data() to use my_pkg::find.package() instead of base::find.package()

You should be able to import and re-export the data() function without physically copying the code.
Try removing '@keywords internal' as this conflicts with '@export'. Also '@usage' seems redundant in this context.

Thank you for the suggestion, I tried those changes and have the same problem.

#' Load data function
#'
#' @name data
#' @importFrom utils data
#' @export
NULL

I have made an MWE here that is not working as expected on my computer. There is only one file in R/ that has 3 blocks. Otherwise I think it is a standard usethis template. It can be loaded via devtools::load_all() but you should see the below.

> devtools::load_all('.', export_all=FALSE)
ℹ Loading mwe
> environment(mwe::data)
<environment: namespace:utils>
> environment(mwe::find.package)
<environment: namespace:mwe>
>

The namespace for mwe:data I would expect to be mwe as it is for the find.package function.

You are correct, in which case I think there is no clean solution but to define a data() function in your package, which masks the one from 'utils' i.e. copy/paste.

The import/re-export is really meant for importing an S3 generic for which you have written a method.

Even if technically you could find a way to move data() into your package's namespace, this would in effect be tampering with an object that does not belong to your package and would probably not be considered best practice / fail R CMD check.

Thank you for your time, it is much appreciated. I will resort to the copy/paste approach and see if it causes any problems.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.