Skip to content

Commit bd30629

Browse files
authored
Merge pull request #8 from pachadotdev/roxygen
Roxygen
2 parents 1487985 + 5eff766 commit bd30629

File tree

14 files changed

+375
-14
lines changed

14 files changed

+375
-14
lines changed

R/register.R

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,16 @@ cpp_register <- function(path = ".", quiet = !is_interactive(), extension = c(".
7777
cli::cli_alert_success("generated file {.file {basename(r_path)}}")
7878
}
7979

80-
8180
call_entries <- get_call_entries(path, funs$name, package)
8281

8382
cpp_function_registration <- glue::glue_data(funs, ' {{
8483
"_cpp11_{name}", (DL_FUNC) &_{package}_{name}, {n_args}}}, ',
8584
n_args = viapply(funs$args, nrow)
8685
)
8786

88-
cpp_function_registration <- glue::glue_collapse(cpp_function_registration, sep = "\n")
87+
cpp_function_registration <- glue::glue_collapse(cpp_function_registration, sep = "\n")
8988

90-
extra_includes <- character()
89+
extra_includes <- character()
9190
if (pkg_links_to_rcpp(path)) {
9291
extra_includes <- c(extra_includes, "#include <cpp11/R.hpp>", "#include <Rcpp.h>", "using namespace Rcpp;")
9392
}
@@ -215,35 +214,75 @@ generate_init_functions <- function(funs) {
215214
}
216215

217216
generate_r_functions <- function(funs, package = "cpp11", use_package = FALSE) {
218-
funs <- funs[c("name", "return_type", "args")]
217+
funs <- funs[c("name", "return_type", "args", "file", "line", "decoration")]
219218

220219
if (use_package) {
221220
package_call <- glue::glue(', PACKAGE = "{package}"')
222221
package_names <- glue::glue_data(funs, '"_{package}_{name}"')
223222
} else {
224-
package_names <- glue::glue_data(funs, '`_{package}_{name}`')
223+
package_names <- glue::glue_data(funs, "`_{package}_{name}`")
225224
package_call <- ""
226225
}
227226

228-
funs$package <- package
229227
funs$package_call <- package_call
230228
funs$list_params <- vcapply(funs$args, glue_collapse_data, "{name}")
231229
funs$params <- vcapply(funs$list_params, function(x) if (nzchar(x)) paste0(", ", x) else x)
232230
is_void <- funs$return_type == "void"
233231
funs$calls <- ifelse(is_void,
234-
glue::glue_data(funs, 'invisible(.Call({package_names}{params}{package_call}))'),
235-
glue::glue_data(funs, '.Call({package_names}{params}{package_call})')
232+
glue::glue_data(funs, "invisible(.Call({package_names}{params}{package_call}))"),
233+
glue::glue_data(funs, ".Call({package_names}{params}{package_call})")
236234
)
237235

238-
out <- glue::glue_data(funs, '
239-
{name} <- function({list_params}) {{
240-
{calls}
241-
}}
242-
')
236+
# Parse and associate Roxygen comments
237+
funs$roxygen_comment <- mapply(function(file, line) {
238+
if (file.exists(file)) {
239+
comments <- extract_roxygen_comments(file)
240+
matched_comment <- ""
241+
for (comment in comments) {
242+
# Check if the comment directly precedes the function without gaps
243+
if (line == comment$line + 1) {
244+
matched_comment <- comment$text
245+
break
246+
}
247+
}
248+
matched_comment
249+
} else {
250+
""
251+
}
252+
}, funs$file, funs$line, SIMPLIFY = TRUE)
253+
254+
# Generate R functions with or without Roxygen comments
255+
out <- mapply(function(name, list_params, calls, roxygen_comment) {
256+
if (nzchar(roxygen_comment)) {
257+
glue::glue("{roxygen_comment}\n{name} <- function({list_params}) {{\n\t{calls}\n}}")
258+
} else {
259+
glue::glue("{name} <- function({list_params}) {{\n {calls}\n}}")
260+
}
261+
}, funs$name, funs$list_params, funs$calls, funs$roxygen_comment, SIMPLIFY = TRUE)
262+
263+
out <- glue::trim(out)
243264
out <- glue::glue_collapse(out, sep = "\n\n")
244265
unclass(out)
245266
}
246267

268+
extract_roxygen_comments <- function(file) {
269+
lines <- readLines(file)
270+
roxygen_start <- grep("^/\\* roxygen start", lines)
271+
roxygen_end <- grep("roxygen end \\*/$", lines)
272+
273+
if (length(roxygen_start) == 0 || length(roxygen_end) == 0) {
274+
return(list())
275+
}
276+
277+
roxygen_comments <- mapply(function(start, end) {
278+
roxygen_lines <- lines[(start + 1):(end - 1)]
279+
roxygen_lines <- sub("^", "#' ", roxygen_lines)
280+
list(line = end, text = paste(roxygen_lines, collapse = "\n"))
281+
}, roxygen_start, roxygen_end, SIMPLIFY = FALSE)
282+
283+
roxygen_comments
284+
}
285+
247286
wrap_call <- function(name, return_type, args) {
248287
call <- glue::glue('{name}({list_params})', list_params = glue_collapse_data(args, "cpp11::as_cpp<cpp11::decay_t<{type}>>({name})"))
249288
if (return_type == "void") {

cpp11test/NAMESPACE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(roxcpp2_)
4+
export(roxcpp3_)
5+
export(roxcpp4_)
6+
export(roxcpp5_)
7+
export(roxcpp7_)
38
export(run_tests)
49
exportPattern("_$")
510
importFrom(Rcpp,sourceCpp)

cpp11test/R/cpp11.R

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,63 @@ rcpp_release_ <- function(n) {
156156
invisible(.Call(`_cpp11test_rcpp_release_`, n))
157157
}
158158

159+
notroxcpp1_ <- function(x) {
160+
.Call(`_cpp11test_notroxcpp1_`, x)
161+
}
162+
163+
#' @title Roxygenise C++ function II
164+
#' @param x numeric value
165+
#' @description Dummy function to test roxygen2. It adds 2.0 to a double.
166+
#' @export
167+
#' @examples roxcpp2_(1.0)
168+
roxcpp2_ <- function(x) {
169+
.Call(`_cpp11test_roxcpp2_`, x)
170+
}
171+
172+
#' @title Roxygenise C++ function III
173+
#' @param x numeric value
174+
#' @description Dummy function to test roxygen2. It adds 3.0 to a double.
175+
#' @export
176+
#' @examples roxcpp3_(1.0)
177+
roxcpp3_ <- function(x) {
178+
.Call(`_cpp11test_roxcpp3_`, x)
179+
}
180+
181+
#' @title Roxygenise C++ function IV
182+
#' @param x numeric value
183+
#' @description Dummy function to test roxygen2. It adds 4.0 to a double.
184+
#' @export
185+
#' @examples roxcpp4_(1.0)
186+
roxcpp4_ <- function(x) {
187+
.Call(`_cpp11test_roxcpp4_`, x)
188+
}
189+
190+
#' @title Roxygenise C++ function V
191+
#' @param x numeric value
192+
#' @description Dummy function to test roxygen2. It adds 5.0 to a double.
193+
#' @export
194+
#' @examples roxcpp5_(1.0)
195+
roxcpp5_ <- function(x) {
196+
.Call(`_cpp11test_roxcpp5_`, x)
197+
}
198+
199+
notroxcpp6_ <- function(x) {
200+
.Call(`_cpp11test_notroxcpp6_`, x)
201+
}
202+
203+
#' @title Roxygenise C++ function VII
204+
#' @param x numeric value
205+
#' @description Dummy function to test roxygen2. It adds 7.0 to a double.
206+
#' @export
207+
#' @examples
208+
#' my_fun <- function(x) {
209+
#' roxcpp7_(x)
210+
#' }
211+
#' @seealso \code{\link{roxcpp1_}}
212+
roxcpp7_ <- function(x) {
213+
.Call(`_cpp11test_roxcpp7_`, x)
214+
}
215+
159216
cpp11_safe_ <- function(x_sxp) {
160217
.Call(`_cpp11test_cpp11_safe_`, x_sxp)
161218
}

cpp11test/man/roxcpp2_.Rd

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

cpp11test/man/roxcpp3_.Rd

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

cpp11test/man/roxcpp4_.Rd

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

cpp11test/man/roxcpp5_.Rd

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

cpp11test/man/roxcpp7_.Rd

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

cpp11test/src/cpp11.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,55 @@ extern "C" SEXP _cpp11test_rcpp_release_(SEXP n) {
303303
return R_NilValue;
304304
END_CPP11
305305
}
306+
// roxygen1.cpp
307+
double notroxcpp1_(double x);
308+
extern "C" SEXP _cpp11test_notroxcpp1_(SEXP x) {
309+
BEGIN_CPP11
310+
return cpp11::as_sexp(notroxcpp1_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
311+
END_CPP11
312+
}
313+
// roxygen1.cpp
314+
double roxcpp2_(double x);
315+
extern "C" SEXP _cpp11test_roxcpp2_(SEXP x) {
316+
BEGIN_CPP11
317+
return cpp11::as_sexp(roxcpp2_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
318+
END_CPP11
319+
}
320+
// roxygen2.cpp
321+
double roxcpp3_(double x);
322+
extern "C" SEXP _cpp11test_roxcpp3_(SEXP x) {
323+
BEGIN_CPP11
324+
return cpp11::as_sexp(roxcpp3_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
325+
END_CPP11
326+
}
327+
// roxygen2.cpp
328+
double roxcpp4_(double x);
329+
extern "C" SEXP _cpp11test_roxcpp4_(SEXP x) {
330+
BEGIN_CPP11
331+
return cpp11::as_sexp(roxcpp4_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
332+
END_CPP11
333+
}
334+
// roxygen3.cpp
335+
double roxcpp5_(double x);
336+
extern "C" SEXP _cpp11test_roxcpp5_(SEXP x) {
337+
BEGIN_CPP11
338+
return cpp11::as_sexp(roxcpp5_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
339+
END_CPP11
340+
}
341+
// roxygen3.cpp
342+
double notroxcpp6_(double x);
343+
extern "C" SEXP _cpp11test_notroxcpp6_(SEXP x) {
344+
BEGIN_CPP11
345+
return cpp11::as_sexp(notroxcpp6_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
346+
END_CPP11
347+
}
348+
// roxygen3.cpp
349+
double roxcpp7_(double x);
350+
extern "C" SEXP _cpp11test_roxcpp7_(SEXP x) {
351+
BEGIN_CPP11
352+
return cpp11::as_sexp(roxcpp7_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
353+
END_CPP11
354+
}
306355
// safe.cpp
307356
SEXP cpp11_safe_(SEXP x_sxp);
308357
extern "C" SEXP _cpp11test_cpp11_safe_(SEXP x_sxp) {
@@ -574,6 +623,11 @@ static const R_CallMethodDef CallEntries[] = {
574623
{"_cpp11test_rcpp_sum_int_for_", (DL_FUNC) &_cpp11test_rcpp_sum_int_for_, 1},
575624
{"_cpp11test_remove_altrep", (DL_FUNC) &_cpp11test_remove_altrep, 1},
576625
{"_cpp11test_row_sums", (DL_FUNC) &_cpp11test_row_sums, 1},
626+
{"_cpp11test_roxcpp2_", (DL_FUNC) &_cpp11test_roxcpp2_, 1},
627+
{"_cpp11test_roxcpp3_", (DL_FUNC) &_cpp11test_roxcpp3_, 1},
628+
{"_cpp11test_roxcpp4_", (DL_FUNC) &_cpp11test_roxcpp4_, 1},
629+
{"_cpp11test_roxcpp5_", (DL_FUNC) &_cpp11test_roxcpp5_, 1},
630+
{"_cpp11test_roxcpp7_", (DL_FUNC) &_cpp11test_roxcpp7_, 1},
577631
{"_cpp11test_string_proxy_assignment_", (DL_FUNC) &_cpp11test_string_proxy_assignment_, 0},
578632
{"_cpp11test_string_push_back_", (DL_FUNC) &_cpp11test_string_push_back_, 0},
579633
{"_cpp11test_sum_dbl_accumulate2_", (DL_FUNC) &_cpp11test_sum_dbl_accumulate2_, 1},

cpp11test/src/roxygen1.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "cpp11/doubles.hpp"
2+
using namespace cpp11;
3+
4+
// Test: not documented + documented
5+
6+
// Not Roxygenised C++ function I
7+
[[cpp11::register]] double notroxcpp1_(double x) {
8+
double y = x + 1.0;
9+
return y;
10+
}
11+
12+
/* roxygen start
13+
@title Roxygenise C++ function II
14+
@param x numeric value
15+
@description Dummy function to test roxygen2. It adds 2.0 to a double.
16+
@export
17+
@examples roxcpp2_(1.0)
18+
roxygen end */
19+
[[cpp11::register]] double roxcpp2_(double x) {
20+
double y = x + 2.0;
21+
return y;
22+
}

0 commit comments

Comments
 (0)