Error message in R: "error to replicate an object of type 'closure'"

49 Views Asked by At

I am creating an R package where one function uses a vector that was created in another function. The vector is successfully created and assigned to the global environment (because I was having issues with calling a vector created in other function) in the first function:

sites <- function(...) {
  s <- c(...)
  assign(x = 's', value = s, envir = .GlobalEnv)
}

Then in the second function, I tried to call the vector s to influence the output:

dates <- function(date) {
  d <- rep(date, length(s))
  print(d)
}

However, when I run these functions, I get an error:

Error in rep(date, length(s)) : 
  attempt to replicate an object of type 'closure'

I saw the same error here but did not fully understand it. I'm thinking the problem is with the environments or the functions being in a package, but I don't know how to fix it or understand why I keep getting this error.

Thanks in advance!

I tried:

sites(1, 2, 3, 4, 5)
dates("10/24/2023")

I expected:

[1] "10/24/2023" "10/24/2023" "10/24/2023" "10/24/2023" "10/24/2023"
0

There are 0 best solutions below