If I use source code then everything is working but when I create package/function then it is giving me an error. Am I missing anything?
test package
test <- function() {
library("data.table")
dd <- mtcars
setDT(dd)
dd$cnt <- 1
eval(parse(text=paste0("dd <- unique(dd[,list(cnt, mpg, cyl)])")))
eval(parse(text=paste0("dd1 <- dd[order(mpg, cyl)]")))
print(dd1)
}
create a package using R CMD build on Linux
test 
   |__R
      |__test.r
   |__DESCRIPTION
   |__NAMESPACE
R CMD build test
R CMD INSTALL test_1.0.tar.gz
Content of DESCRIPTION
Package: test
Title: test
Version: 1.0
Authors@R: person("xyz", "xyz", email = "xyz@xyz.com",
                  role = c("aut", "cre"))
Description: test
Depends: R (>= 3.6.0)
License: test
Encoding: UTF-8
LazyData: true
Imports:
    data.table
use function in R
library("test")
test()
Get an error as below
Error in [.data.frame(x, i, j) : object 'cnt' not fo
R code without package and it is working
library("data.table")
dd <- mtcars
setDT(dd)
dd$cnt <- 1
eval(parse(text=paste0("dd <- unique(dd[,list(cnt, mpg, cyl)])")))
eval(parse(text=paste0("dd1 <- dd[order(mpg, cyl)]")))
print(dd1)
