Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I load dependencies in an R package?

Tags:

package

r

cran

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.

like image 785
maximusdooku Avatar asked Dec 19 '25 20:12

maximusdooku


1 Answers

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.

like image 141
Hong Ooi Avatar answered Dec 21 '25 10:12

Hong Ooi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!