1

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)
R007
  • 101
  • 1
  • 13
  • Look into the `package.skeleton()` function and see what it creates -- it takes more to have a package. Full details are in the _Writing R Extensions_ manual. (And my `pkgKitten` package helps with some post-processing of `package.skeleton()` which leaves some things you must edit which is confusing when you start out. RStudio also has a package generator helper, as do other packages.) – Dirk Eddelbuettel Feb 23 '20 at 17:45
  • Thanks, I added more details into the directory structure of packages. I will look into package.skeleton(). Just trying to see what is the simplest way to create a test package without using a lot of other dependant packages etc. – R007 Feb 23 '20 at 17:57
  • Good approach! In that case base R and its `package.skeleton()` function should be your new best friends. I just found it a little off-putting that it would never pass `R CMD check --as-cran` immediately after creation, hence `pkgKitten`. But I use the same approach as you: try first with the minimal amount of helpers to _reallly_ understand the minimal structure. – Dirk Eddelbuettel Feb 23 '20 at 18:19
  • Actually, years ago I gave a workshop where we created each file by hand and you could look at the slides / check if it still holds: http://dirk.eddelbuettel.com/papers/r_package_development_nov2014.pdf – Dirk Eddelbuettel Feb 23 '20 at 18:21
  • Some things are not quite right with your code. Use `dd[, cnt:=1]` to create a data.table column. And you should not need `eval(parse(txt))` but that is really a different question ... – Dirk Eddelbuettel Feb 23 '20 at 18:23
  • I tried using RStudio to create a package. My feeling is that there is some conflict with data.table. i.e. if I understood correctly then data.table intercepts data. frame functions if the object is type of data.table. Maybe during binary compilation time, there is something is happening. – R007 Feb 23 '20 at 18:24
  • using dd[, cnt:=1] also generates the same error. – R007 Feb 23 '20 at 18:31
  • 2
    There is no binary compilation, but I also found `data.table` sometimes needs extra care. Did you `import()` it in `NAMESPACE`? (And of course you should not have `library()` calls in R code in package. See what else `R CMD check` and/or the `check` button in RStudio say). – Dirk Eddelbuettel Feb 23 '20 at 18:38

1 Answers1

0

Thanks @Dirk Eddelbuettel. You are right and it worked. I found out that question have been answered on StackOverflow and also good documentation available.

https://cran.r-project.org/web/packages/data.table/vignettes/datatable-importing.html

Using data.table package inside my own package

R007
  • 101
  • 1
  • 13