13

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?

wdkrnls
  • 4,548
  • 7
  • 36
  • 64
  • 3
    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. – C. Braun Feb 15 '18 at 22:23
  • 1
    so what do your NAMESPACE and DESCRIPTION files look like? – RolandASc Feb 15 '18 at 22:23
  • 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? – wdkrnls Feb 15 '18 at 22:24
  • 1
    note that `roxygen` does not append to / overwrite manually maintained NAMESPACE files – RolandASc Feb 15 '18 at 22:27
  • 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. – wdkrnls Feb 15 '18 at 22:29
  • I can't believe there is no way to regenerate the `NAMESPACE` from the RStudio GUI. – wdkrnls Feb 15 '18 at 22:34
  • `ctrl-shift-d` doesn't regenerate the `NAMESPACE` for you? – C. Braun Feb 15 '18 at 22:37
  • No, it seems to duplicate the line at cursor. – wdkrnls Feb 15 '18 at 22:38
  • For me it calls `Build -> Document` which calls this command: `devtools::document(roclets=c('rd', 'collate', 'namespace', 'vignette'))` – C. Braun Feb 15 '18 at 22:42
  • @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? – wdkrnls Feb 15 '18 at 22:49
  • 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. – wdkrnls Feb 15 '18 at 22:53
  • @C.Braun with regards to your first comment: I don't want to bring in everything since I still want a relatively clean `NAMESPACE`. – wdkrnls Feb 15 '18 at 22:56
  • @wdkrnls Do you have `devtools` installed and can run the `document` function directly? – C. Braun Feb 15 '18 at 23:03
  • @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). – wdkrnls Feb 15 '18 at 23:09
  • @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. – C. Braun Feb 15 '18 at 23:15
  • 2
    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. – SymbolixAU Feb 15 '18 at 23:37
  • 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 automatically – RolandASc Feb 16 '18 at 09:27
  • Taking SymbolixAU's lead, I discovered the checkbox I needed. It unfortunately led to me to this issue: https://stackoverflow.com/questions/29372288/build-reload-in-rstudio-on-windows-devtoolsdocument-says-devtools-not-fou – wdkrnls Feb 16 '18 at 16:19

0 Answers0