3

I am developing an R package where this is available in the DESCRIPTIONS file

Imports: 
    dplyr,
    ggplot2,
    ncdf4

And I have an example function where I use the third dependency

testFun <- function(file, lat, long){
  ncfname <- file.path(file,fsep = .Platform$file.sep)
  xfile <- nc_open(ncfname) #Opens the NetCDF file
  lat <- ncvar_get(xfile, 'lat') #Extracts all latitudes

  ...Calculations

  return(XYZ)
}

When I Build and Reload the package, and I run the function, it could not find function "nc_open".

BUT, it works when I replace it with ncdf4::nc_open

Am I supposed to prefix packagename:: to every dependency I use in the code? or am I missing something?

Ordinarily, I would like all the dependencies to be installed from the DESCRIPTIONS and it's functions available for use without requiring the package prefix everytime.

maximusdooku
  • 5,242
  • 10
  • 54
  • 94

2 Answers2

7

Either:

  • explicitly prefix the function with the package it's from: ncdf4::nc_open(...)

Or:

  • add a line in your NAMESPACE file importFrom(ncdf4, nc_open) and then in your code, call the function without the package: nc_open(...)

Rather than adding an importFrom line for every function you want to import, you can also use import(ncdf4) to snarf everything from that package.

Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
  • I think explicit prefix might even help with code clarity. So I'll keep it simple and stick to it. I would like to not maintain another list of imports. – maximusdooku Sep 18 '17 at 23:19
  • You don't need an `importFrom()` for every function, just one per package: `importFrom(ncdf4, nc_open, nc_close, nc_foo, nc_bar)`. – Joshua Ulrich Sep 18 '17 at 23:24
  • @hong What benefit does importing parts of the package have compared to the whole package? Does it matter in anyway? I can just do import() and not worry about importFrom but I would like to know what it impacts in terms of computations, size etc. – maximusdooku Sep 18 '17 at 23:57
  • I inserted import(ggplot2), rebuilt, restarted etc, but it seems to have no impact. And the error is same as before. Any idea? – maximusdooku Sep 19 '17 at 00:00
1

The easiest way and most correct way is to directly pull your function from the package without opening any packages which might obliterate someone's current environment.

Try this:

xfile <- ncdf4::nc_open(ncfname)

It should access what you need without conflicts. That is the current preferred method because it leaves things as it found them for your users. It also makes it easy for people to KNOW what is happening should they go exploring.

sconfluentus
  • 4,693
  • 1
  • 21
  • 40
  • If possible, I would like a reference for this being the right away. Is it available in any of the Documentation? it seems cumbersome, though safer. – maximusdooku Sep 18 '17 at 23:17
  • Go to Hadley WIckam's site http://r-pkgs.had.co.nz/namespace.html it clearly states unless you are using most of a package (like many database packages which function OVER DBI do) that :: is preferred. – sconfluentus Sep 18 '17 at 23:22