I have R code that runs a chunk of code that computes columns of statistics for each group in a dataframe. It works as expected when running the code directly in the console with the dataframe.
However, I want to make a package containing this function. When I wrap the original code in a function that takes in a dataframe as an argument and returns the passed in dataframe with the new columns, the resulting columns are not the same.
I was wondering if it has something to do with the scope used in R, but after multiple variations on my code, I can't seem to figure the problem out. I have run into this before as well.
EDIT: I found that when I source the code, it says that it cannot find a function that I have defined above it. This is the region when my code is giving different results because I have wrapped the other function call in a try catch block
Example:
otherFunction <- function(df, ...){
...
}
myFunction <- function(df){
result <- tryCatch({
otherFunction(df, ...)
}, error = function(e) {
NA # the result is always being set to NA
})
return result
}
...
myDf <- ...
myFunction(myDf)
error: "could not find function "otherFunction""