automated data analysis workflows
I have the following code in my packagename.R
file in the packagename
folder.
I know I am going to use certain functions from other packages routinely, so I want to import them once instead of typing them out all over the place. I thought I should be able to do that with the above approach, but it's not working in my RStudio.
#' packagename: Package containing tools support project work.
#'
#' This package provides a central repository for methods to facilitate
#' project work
#' @importFrom dplyr bind_rows mutate filter group_by ungroup
#' @importFrom purrr pluck map map_chr map_dbl
#' @importFrom lubridate ymd_hms
#' @importFrom odbc odbc
#' @importFrom DBI dbConnect dbDisconnect
#' @importFrom stringr str_detect str_replace_all
#' @docType package
#' @name packagename
NULL
In another file topic.R
I have:
do_thing <- function(x) str_replace_all(x, " ", "_"))
When I call do_thing
it tells me:
Error in str_replace_all(x, " ", "_") :
could not find function "str_replace_all"
Is there something more I need to add, or perhaps should I do something differently?
Based on the comments to the question, I needed to atleast regenerate my NAMESPACE
, which apparently was generated manually (since I started the project using the RStudio GUI), so roxygen
wouldn't update it. I am interested in how I can do this from the UI as well.
This is what my build menu looks like:
My NAMESPACE
file includes these imports but my test script still fails when trying to run the functions. Is this not possible as C. Braun suggests?
Although this is not a new question, it has been viewed thousands of time therefore, I guess, it should be answered.
It is not that easy to do it like that. The best way to proceed is to use the function use_import_from
provided by the usethis
package (included in the devtools
package): usethis::use_import_from(package = "dplyr", fun = bind_rows)
.
This will create/append a file named mypackage-package.R
where mypackage
is the name of your package. Its content looks like that:
#' @keywords internal
"_PACKAGE"
## usethis namespace: start
#' @importFrom data.table :=
#' @importFrom dplyr across
#' @importFrom dplyr group_by
#' @importFrom dplyr mutate
#' @importFrom dplyr n
#' @importFrom dplyr summarise
#' @importFrom magrittr %<>%
#' @importFrom magrittr %>%
#' @importFrom stats stepfun
## usethis namespace: end
NULL
And when you load/build the package, your namespace will be updated with something like:
importFrom(data.table,":=")
importFrom(dplyr,across)
importFrom(dplyr,group_by)
importFrom(dplyr,mutate)
importFrom(dplyr,n)
importFrom(dplyr,summarise)
importFrom(magrittr,"%<>%")
importFrom(magrittr,"%>%")
importFrom(stats,stepfun)
I hope that can help you.