Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extendr project template #321

Merged
merged 21 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* `use_extendr()` sets `publish = false` in the `[package]` section of the `Cargo.toml` (#297).
* `use_extendr()` correctly handles calls with `path` not equal to `"."` (current folder), or when there is no active `{usethis}` project (#323).
* Fixes an issue in pre-defined set of known features: added `either` (#338)
* `create_extendr_package()` allows user to create project directory using RStudio's **Project Command**. (#321)

# rextend 0.3.1

Expand Down
55 changes: 55 additions & 0 deletions R/create_extendr_package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

#' Create package that uses Rust
#'
#' @description
#' This function creates an R project directory for package development
#' with Rust extensions.
#'
#' @inheritParams usethis::create_package
#' @param ... arguments passed on to `usethis::create_package()` and
#' `rextendr::use_extendr()`
#'
#' @return Path to the newly created project or package, invisibly.
#' @keywords internal
#'
#' @noRd
JosiahParry marked this conversation as resolved.
Show resolved Hide resolved
create_extendr_package <- function(path, ...) {
kbvernon marked this conversation as resolved.
Show resolved Hide resolved

# error if usethis is not installed
rlang::check_installed("usethis")

Check warning on line 19 in R/create_extendr_package.R

View check run for this annotation

Codecov / codecov/patch

R/create_extendr_package.R#L19

Added line #L19 was not covered by tests

args <- rlang::list2(...)

Check warning on line 21 in R/create_extendr_package.R

View check run for this annotation

Codecov / codecov/patch

R/create_extendr_package.R#L21

Added line #L21 was not covered by tests

# hunch is that rstudio project text input widgets return empty strings
# when no value is given, want to make sure it is NULL so `use_extendr()`
# handles it correctly
nullify_empty_string <- function(x) {
if (rlang::is_string(x) && nzchar(x)) x else NULL

Check warning on line 27 in R/create_extendr_package.R

View check run for this annotation

Codecov / codecov/patch

R/create_extendr_package.R#L26-L27

Added lines #L26 - L27 were not covered by tests
}

args <- purrr::map(args, nullify_empty_string)

Check warning on line 30 in R/create_extendr_package.R

View check run for this annotation

Codecov / codecov/patch

R/create_extendr_package.R#L30

Added line #L30 was not covered by tests

# build package directory, but don't start a new R session with
# it as the working directory! i.e., set `open = FALSE`
usethis::create_package(
path,
fields = list(),
rstudio = TRUE,
roxygen = args[["roxygen"]] %||% TRUE,
check_name = args[["check_name"]] %||% TRUE,
open = FALSE

Check warning on line 40 in R/create_extendr_package.R

View check run for this annotation

Codecov / codecov/patch

R/create_extendr_package.R#L34-L40

Added lines #L34 - L40 were not covered by tests
)

# add rust scaffolding to project dir
use_extendr(
path,
crate_name = args[["crate_name"]],
lib_name = args[["lib_name"]],
quiet = TRUE,
overwrite = TRUE,
edition = args[["edition"]] %||% TRUE

Check warning on line 50 in R/create_extendr_package.R

View check run for this annotation

Codecov / codecov/patch

R/create_extendr_package.R#L44-L50

Added lines #L44 - L50 were not covered by tests
)

invisible(path)

Check warning on line 53 in R/create_extendr_package.R

View check run for this annotation

Codecov / codecov/patch

R/create_extendr_package.R#L53

Added line #L53 was not covered by tests

}
33 changes: 33 additions & 0 deletions inst/rstudio/templates/project/extendr.dcf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Binding: create_extendr_package
Title: R package with extendr
Subtitle: Create an R package with Rust extensions.
Caption: Create an R package with Rust extensions.

Parameter: roxygen
Widget: CheckboxInput
Label: Use roxygen2
Default: On
Position: left

Parameter: check_name
Widget: CheckboxInput
Label: Validate package name
Default: On
Position: left

Parameter: crate_name
Widget: TextInput
Label: Rust crate name
Position: right

Parameter: lib_name
Widget: TextInput
Label: Rust library name
Position: right

Parameter: edition
Widget: SelectInput
Label: Rust edition
Fields: 2021, 2018
Default: 2021
Position: right
6 changes: 5 additions & 1 deletion vignettes/package.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ rextendr::use_extendr()
#> • Please run `rextendr::document()` for changes to take effect.
```

Now we are just one step away from calling Rust functions from R. As the message says, we need to run `rextendr::document()`.
For developers who use RStudio, we also provide a project template that will call `usethis::create_package()` and `rextendr::use_extendr()` for you. This is done using RStudio's **Create Project** command, which you can find on the global toolbar or in the File menu. Choose "New Directory" then select "R package with extendr." You can then fill out the details to match your preferences.

Once you have the project directory setup, we strongly encourage you to run `rextendr::rust_sitrep()` in the console. This will provide a detailed report of the current state of your Rust infrastructure, along with some helpful advice about how to address any issues that may arise.

Assuming we have a proper installation of Rust, we are just one step away from calling Rust functions from R. As the message above says, we need to run `rextendr::document()`.
But, before moving forward, let's look at the files added.

## Package structure
Expand Down
Loading