My question is related to this one: Function that returns a function in R but, I am attempting to create an R package. I have the following function which uses a data frame stored in sysdata.rda called NInput. I'd like to return a function using the approxfun() function. I'd like the user of this package to be able to call Nfun(Dates) where Dates would be the usual input to the function created with approxfun().
#' A function to return interpolated N input data
#'
#' function to return interpolated N input data for decimal dates
#' @param Dates A scalar or vector of decimal input dates
#' @keywords nitrogen
#' @export
#' @examples
#' Nfun()
Nfun <- function(){
NInput$Date
NInput$Ninput
Nfun <- approxfun(NInput$Date,NInput$Ninput,rule=2)
}
However, when I install my package with this function and call Nfun(1900) in my R session after loading the package, I get:
Error in Nfun(1900) : unused argument (1900)
if I set
Nfun <- Nfun()
outside of the Nfun.R script, in my active R session after loading the package, my function appears in the global environment and I can call
Nfun(1900)
[1] 0.7846106
But, I don't want the user to have to do this. I'm thinking I'm making some basic mistake with how to define my package function, but I'm stumped.
Your implementation of Nfun
does not have argument, hence the error occurs.
If I understand correctly, you want to make a function by approxfun
and Nfun
returns the value of the function for user input, correct?
If so, the following should work.
Nfun <- function(x){
NInput$Date
NInput$Ninput
Nfun <- approxfun(NInput$Date,NInput$Ninput,rule=2)(x)
}