how do I use @importFrom so that it applies to a whole R package?
Asked Answered
B

0

14

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:

RStudio Build Menu

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?

Bikini answered 15/2, 2018 at 22:19 Comment(18)
afaik you have to include the @importFrom specifically at the top of every function that uses them. But why don't you just load the whole library if you are routinely using those functions across your package.Bedraggle
so what do your NAMESPACE and DESCRIPTION files look like?Laoag
Apparently my NAMESPACE just has exportPattern("^[[:alpha:]]+"). Maybe I have to run document() and not just rely on ctrl-shift-b. Is there any way to do that from RStudio outside of the console?Bikini
note that roxygen does not append to / overwrite manually maintained NAMESPACE filesLaoag
I see. So when I created my package from RStudio it created a manual NAMESPACE file. That's not ideal, but after deleting it and running document(), my NAMESPACE is looking right. Thanks! I'm will to accept that as an answer.Bikini
I can't believe there is no way to regenerate the NAMESPACE from the RStudio GUI.Bikini
ctrl-shift-d doesn't regenerate the NAMESPACE for you?Bedraggle
No, it seems to duplicate the line at cursor.Bikini
For me it calls Build -> Document which calls this command: devtools::document(roclets=c('rd', 'collate', 'namespace', 'vignette'))Bedraggle
@C.Braun I included a screenshot of my build menu. I'm using version 1.1.419 of RStudio (I'm actually using the server edition). What about you?Bikini
hmm... actually, even though it's in my namespace it still isn't being made available. I actually developed another package outside of Rstudio and it seemed to be working there. So I am surprised it's not working here.Bikini
@C.Braun with regards to your first comment: I don't want to bring in everything since I still want a relatively clean NAMESPACE.Bikini
@Bikini Do you have devtools installed and can run the document function directly?Bedraggle
@C. Braun Yes, that's what I'm doing now. I was just surprised I needed to. But, it still doesn't let my functions use these imports even if they are listed in the NAMESPACE, which I'm really surprised about since that isn't the behavior exhibited by a package I developed with Emacs/ESS on R 3.3.3 (which I wouldn't have thought would matter).Bikini
@wdkrnls, yes, that is strange I'm not sure what could have changed. Interestingly though, I just updated from an older version of RStudio to 1.1.423 and am noticing that "Document" exists in the "Build" menu for a session I have editing a package, but not for one that is just editing some scripts.Bedraggle
going back a couple of comments, go to build > Configure Build Tools > Configure and then check the boxes for letting roxygen build vignettes (I have all of them checked for my pacakges/ builds). These are unchecked by default when starting a package.Generator
in case it is still not working for you, have you also checked the DESCRIPTION file? The Imports: section there needs to list all the packages that you want to import from. Roxygen does not offer any means to do this automaticallyLaoag
Taking SymbolixAU's lead, I discovered the checkbox I needed. It unfortunately led to me to this issue: #29372788Bikini

© 2022 - 2024 — McMap. All rights reserved.