Skip to content

Commit 026b533

Browse files
committed
rename to qmd_to_r_script and mark as experimental
1 parent 181c272 commit 026b533

File tree

5 files changed

+43
-32
lines changed

5 files changed

+43
-32
lines changed

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
export(add_spin_preamble)
44
export(check_newer_version)
5-
export(extract_r_code)
65
export(is_using_quarto)
76
export(new_blog_post)
7+
export(qmd_to_r_script)
88
export(quarto_add_extension)
99
export(quarto_available)
1010
export(quarto_binary_sitrep)

R/utils-extract.R

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
#' Extract R code from Quarto document
1+
#' Convert Quarto document to R script
2+
#'
3+
#' @description
4+
#' `r lifecycle::badge("experimental")`
25
#'
36
#' Extracts R code cells from a Quarto document and writes them to an R script
47
#' file that can be rendered with the same options. The Markdown text is not
58
#' preserved, but R chunk options are kept as comment headers using Quarto's
69
#' `#|` syntax.
710
#'
11+
#' This function is still experimental and may slightly change in
12+
#' future releases, depending on feedback.
13+
#'
814
#' @param qmd Character. Path to the input Quarto document (.qmd file).
915
#' @param script Character. Path to the output R script file. If `NULL`
1016
#' (default), the script file will have the same name as the input file
@@ -36,16 +42,16 @@
3642
#'
3743
#' @examples
3844
#' \dontrun{
39-
#' # Extract R code from a Quarto document
40-
#' extract_r_code("my-document.qmd")
45+
#' # Convert a Quarto document to R script
46+
#' qmd_to_r_script("my-document.qmd")
4147
#' # Creates "my-document.R"
4248
#'
4349
#' # Specify custom output file
44-
#' extract_r_code("my-document.qmd", script = "extracted-code.R")
50+
#' qmd_to_r_script("my-document.qmd", script = "extracted-code.R")
4551
#' }
4652
#'
4753
#' @export
48-
extract_r_code <- function(qmd, script = NULL) {
54+
qmd_to_r_script <- function(qmd, script = NULL) {
4955
if (!file.exists(qmd)) {
5056
cli::cli_abort(
5157
c(

man/extract_r_code.Rd renamed to man/qmd_to_r_script.Rd

Lines changed: 12 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/utils-extract.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
# extract_r_code() errors on wrong qmd
1+
# qmd_to_r_script() errors on wrong qmd
22

33
Code
4-
extract_r_code("nonexistent.qmd")
4+
qmd_to_r_script("nonexistent.qmd")
55
Condition
66
Error:
77
! File 'nonexistent.qmd' does not exist.
88
> Please provide a valid Quarto document.
99

10-
# extract_r_code() errors on existing script
10+
# qmd_to_r_script() errors on existing script
1111

1212
Code
13-
extract_r_code(resources_path("purl-r.qmd"), script = r_script)
13+
qmd_to_r_script(resources_path("purl-r.qmd"), script = r_script)
1414
Condition
15-
Error in `extract_r_code()`:
15+
Error in `qmd_to_r_script()`:
1616
! File <r script> already exists.
1717
> Please provide a new file name or remove the existing file.
1818

19-
# extract_r_code() ignore other language code
19+
# qmd_to_r_script() ignore other language code
2020

2121
Code
22-
extract_r_code(resources_path("purl-r-ojs.qmd"), r_script)
22+
qmd_to_r_script(resources_path("purl-r-ojs.qmd"), r_script)
2323
Message
2424
Extracting only R code cells from 'resources/purl-r-ojs.qmd'.
2525
> Other languages will be ignored (found ojs).

tests/testthat/test-utils-extract.R

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
test_that("extract_r_code() errors on wrong qmd", {
1+
test_that("qmd_to_r_script() errors on wrong qmd", {
22
expect_snapshot(
33
error = TRUE,
4-
extract_r_code("nonexistent.qmd")
4+
qmd_to_r_script("nonexistent.qmd")
55
)
66
})
77

8-
test_that("extract_r_code() errors on existing script", {
8+
test_that("qmd_to_r_script() errors on existing script", {
99
r_script <- withr::local_tempfile(pattern = "purl", fileext = ".R")
1010
file.create(r_script)
1111
expect_snapshot(
1212
error = TRUE,
13-
extract_r_code(resources_path("purl-r.qmd"), script = r_script),
13+
qmd_to_r_script(resources_path("purl-r.qmd"), script = r_script),
1414
transform = function(x) {
1515
gsub("(! File ).*( already exists.)", "\\1<r script>\\2", x)
1616
}
1717
)
1818
})
1919

20-
test_that("extract_r_code() writes R file that renders", {
20+
test_that("qmd_to_r_script() writes R file that renders", {
2121
skip_if_no_quarto()
2222
r_script <- withr::local_tempfile(pattern = "purl", fileext = ".R")
2323

2424
announce_snapshot_file(name = "purl.R")
2525

2626
expect_snapshot_file(
27-
path = extract_r_code(
27+
path = qmd_to_r_script(
2828
resources_path("purl-r.qmd"),
2929
script = r_script
3030
),
@@ -46,29 +46,29 @@ test_that("extract_r_code() writes R file that renders", {
4646
)
4747
})
4848

49-
test_that("extract_r_code() do nothing on file with no code", {
49+
test_that("qmd_to_r_script() do nothing on file with no code", {
5050
skip_if_no_quarto()
5151
expect_message(
52-
expect_null(extract_r_code(resources_path("purl-no-cell.qmd"))),
52+
expect_null(qmd_to_r_script(resources_path("purl-no-cell.qmd"))),
5353
"No code cells found"
5454
)
5555
expect_false(file.exists(resources_path("purl.R")))
5656
})
5757

58-
test_that("extract_r_code() do nothing on file with only other language code", {
58+
test_that("qmd_to_r_script() do nothing on file with only other language code", {
5959
skip_if_no_quarto()
6060
expect_message(
61-
expect_null(extract_r_code(resources_path("purl-py.qmd"))),
61+
expect_null(qmd_to_r_script(resources_path("purl-py.qmd"))),
6262
"No R code cells found.*: python"
6363
)
6464
expect_false(file.exists(resources_path("purl.R")))
6565
})
6666

67-
test_that("extract_r_code() ignore other language code", {
67+
test_that("qmd_to_r_script() ignore other language code", {
6868
skip_if_no_quarto()
6969
r_script <- withr::local_tempfile(pattern = "purl", fileext = ".R")
7070
expect_snapshot(
71-
extract_r_code(resources_path("purl-r-ojs.qmd"), r_script),
71+
qmd_to_r_script(resources_path("purl-r-ojs.qmd"), r_script),
7272
)
7373
expect_true(file.exists(r_script))
7474
})

0 commit comments

Comments
 (0)