extendr / rextendr

An R package that helps scaffolding extendr-enabled packages or compiling Rust code dynamically
https://extendr.github.io/rextendr/
Other
195 stars 28 forks source link

Error message emitted after running `use_rextendr()` #182

Closed Robinlovelace closed 2 years ago

Robinlovelace commented 2 years ago

Hi guys, great package. Just a heads-up on my experience when using it in a new project, not sure what the error message was (I guess an R folder is needed) but could certainly be better error message:

usethis::use_description()
✔ Writing 'DESCRIPTION'
Package: odr
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R (parsed):
    * First Last <first.last@example.com> [aut, cre] (YOUR-ORCID-ID)
Description: What the package does (one paragraph).
License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a
    license
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2
> rextendr::use_extendr()
✔ Creating 'src/rust/src'.
✔ Writing 'src/entrypoint.c'
✔ Writing 'src/Makevars'
✔ Writing 'src/Makevars.win'
✔ Writing 'src/.gitignore'
✔ Writing 'src/rust/Cargo.toml'.
✔ Writing 'src/rust/src/lib.rs'
✔ Writing 'R/extendr-wrappers.R'
Error in file(path, open = file_mode, encoding = "utf-8") : 
  cannot open the connection
In addition: Warning message:
In file(path, open = file_mode, encoding = "utf-8") :
  cannot open file '/mnt/57982e2a-2874-4246-a6fe-115c199bc6bd/orgs/atumWorld/odr/R/extendr-wrappers.R': No such file or directory

Thanks!

Ilia-Kosenkov commented 2 years ago

Interesting, how did you create the package folder? I get the first error at usethis::use_description(). Can you provide a minimum reproducible example? I assume your steps were

  1. Create temporary folder
  2. Navigate to folder
  3. ???
  4. usethis::use_description()
  5. rextendr::use_extendr()

Also, it could be nice if you can provide your version of R, {usethis} and {extednr}, e.g.

> R.version$version.string
> installed.packages()[c("usethis", "rextendr"), "Version"]
Robinlovelace commented 2 years ago

Thanks for the quick responses. All you need to do to reproduce the error, I think, is run the following command to create a project:

rstudioapi::openProject("test")

Please try it and let me know how you get on. In terms of package versions, see below.

R.version$version.string
#> [1] "R version 4.1.2 (2021-11-01)"
installed.packages()[c("usethis", "rextendr"), "Version"]
#>      usethis     rextendr 
#>      "2.1.5" "0.2.0.9000"

Created on 2022-02-15 by the reprex package (v2.0.1)

malcolmbarrett commented 2 years ago

Here's a reprex:

library(usethis)
library(rextendr)
path <- file.path(tempdir(), "my.pkg")
dir.create(path)
setwd(path)
proj_set(path, force = TRUE)
#> ✓ Setting active project to '/private/var/folders/
#> w7/8yv1j00s0bb3pfhmqc_rvd980000gn/T/RtmpfgerYV/my.pkg'
use_description()
#> ✓ Writing 'DESCRIPTION'
#> Package: my.pkg
#> Title: What the Package Does (One Line, Title Case)
#> Version: 0.0.0.9000
#> Authors@R (parsed):
#>     * First Last <first.last@example.com> [aut, cre] (YOUR-ORCID-ID)
#> Description: What the package does (one paragraph).
#> License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a
#>     license
#> Encoding: UTF-8
#> Roxygen: list(markdown = TRUE)
#> RoxygenNote: 7.1.2
rextendr::use_extendr()
#> ✓ Creating 'src/rust/src'.
#> ✓ Writing 'src/entrypoint.c'
#> ✓ Writing 'src/Makevars'
#> ✓ Writing 'src/Makevars.win'
#> ✓ Writing 'src/.gitignore'
#> ✓ Writing 'src/rust/Cargo.toml'.
#> ✓ Writing 'src/rust/src/lib.rs'
#> ✓ Writing 'R/extendr-wrappers.R'
#> Warning in file(path, open = file_mode, encoding = "utf-8"): cannot open file
#> '/private/var/folders/w7/8yv1j00s0bb3pfhmqc_rvd980000gn/T/RtmpfgerYV/my.pkg/R/
#> extendr-wrappers.R': No such file or directory
#> Error in file(path, open = file_mode, encoding = "utf-8"): cannot open the connection

Created on 2022-02-15 by the reprex package (v2.0.1)

Creating R/ fixes it, and we should definitely deal with a lack of that directory:

library(usethis)
library(rextendr)
path <- file.path(tempdir(), "my.pkg")
dir.create(path)
setwd(path)
proj_set(path, force = TRUE)
#> Omitted
use_description()
#> Omitted
dir.create("R")
rextendr::use_extendr()
#> ✓ Creating 'src/rust/src'.
#> ✓ Writing 'src/entrypoint.c'
#> ✓ Writing 'src/Makevars'
#> ✓ Writing 'src/Makevars.win'
#> ✓ Writing 'src/.gitignore'
#> ✓ Writing 'src/rust/Cargo.toml'.
#> ✓ Writing 'src/rust/src/lib.rs'
#> ✓ Writing 'R/extendr-wrappers.R'
#> ✓ Finished configuring extendr for package my.pkg.
#> • Please update the system requirement in 'DESCRIPTION' file.
#> • Please run `rextendr::document()` for changes to take effect.

Created on 2022-02-15 by the reprex package (v2.0.1)

That said, just running use_description() in a project is not the usual workflow for generating a package. create_package() does much more than just write a DESCRIPTION file (and for good reason). While the devtools ecosystem will still treat a project with a DESCRIPTION file as a package, you're missing out on the other legwork usethis does for you.

Robinlovelace commented 2 years ago

Great to see the reprex and can see the logic behind create_package(). However, not everyone uses usethis and flexibility is good. Related question: how would you use create_package() to create the DESCRIPTION and the R folder (and other good stuff!) building on your reprex below:

library(usethis)
library(rextendr)
path <- file.path(tempdir(), "my.pkg")
dir.create(path)
setwd(path)

Asking because a common and I think reasonable workflow is to create the folder outside of R first, do some things, and then turn it into a package later down the line.

malcolmbarrett commented 2 years ago

Not everyone uses usethis/devtools, but I think most people should use some sort of a template rather than remembering to set up all the structure themselves, e.g. with package.skeleton() or something.

Anyway, this is a simplified version of what usethis does:

library(usethis)
library(rextendr)
path <- file.path(tempdir(), "my.pkg")
dir.create(path)
setwd(path)
proj_set(path, force = TRUE)
#> ✓ Setting active project to '/private/var/folders/qn/
#> mk0qm8552zlgst0bnn2374tm0000gn/T/RtmpJk1dZw/my.pkg'
use_description()
#> ✓ Writing 'DESCRIPTION'
#> Package: my.pkg
#> Title: What the Package Does (One Line, Title Case)
#> Version: 0.0.0.9000
#> Authors@R (parsed):
#>     * First Last <first.last@example.com> [aut, cre] (YOUR-ORCID-ID)
#> Description: What the package does (one paragraph).
#> License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a
#>     license
#> Encoding: UTF-8
#> Roxygen: list(markdown = TRUE)
#> RoxygenNote: 7.1.2
use_namespace()
#> ✓ Writing 'NAMESPACE'
use_directory("R/")
#> ✓ Creating 'R/'
# optionally: use .Rproj
# use_rstudio()
use_extendr()
#> ✓ Creating 'src/rust/src'.
#> ✓ Writing 'src/entrypoint.c'
#> ✓ Writing 'src/Makevars'
#> ✓ Writing 'src/Makevars.win'
#> ✓ Writing 'src/.gitignore'
#> ✓ Writing 'src/rust/Cargo.toml'.
#> ✓ Writing 'src/rust/src/lib.rs'
#> ✓ Writing 'R/extendr-wrappers.R'
#> ✓ Finished configuring extendr for package my.pkg.
#> • Please update the system requirement in 'DESCRIPTION' file.
#> • Please run `rextendr::document()` for changes to take effect.

Created on 2022-02-15 by the reprex package (v2.0.1)

Of course, this is all a bit moot, since I obviously think we should just create R/. I was just making the suggestion to you since you were using usethis in your example, and create_package() is definitely the suggested workflow for creating packages with usethis

Ilia-Kosenkov commented 2 years ago

Ok, seems about right to manually create R/ folder. We indeed based our workflow on usethis::create_package(), which usually starts with an empty R/ folder.