Skip to content

Conversation

@klmr
Copy link
Contributor

@klmr klmr commented May 29, 2017

As suggested by @hadley in #273, here’s a suggested API for trivially adding single-file parsing support for Roxygen2.

At the moment the proposed change is pretty minimal (but already sufficient for my purposes): it adds a single, exported function parse_code that takes a file or a piece of R code as text, along with an environment of parsed R expressions, and parses the Roxygen tags associated with these objects, to be returned to the user.

I could alternatively/additionally imagine providing a roxigenize_file function (as shown in branch single-file-support-roxygenize).

I would greatly appreciate feedback to make this PR suitable for merging into Roxygen2.

Here’s why I think it’s useful:

I appreciate that “this is currently outside the scope of roxygen” as Hadley said. However, I think it’s a worthwhile addition because:

  1. It’s trivial to add and maintain (the current PR is a single function).
  2. The alternative would be for me or somebody else to create an exact copy of about 90% of roxygen2, maintained as a separate code base and separate CRAN project.
  3. It’s useful: the output of the function (after applying roclets, which are already part of the public API of roxygen2) can be fed to tools::parse_Rd and then to e.g. tools::Rd2txt, and displayed. In fact, that’s what ‹modules› is doing. If there’s demand, it would be trivial to refactor the “display help from Rd” functionality into a separate package.

@hadley
Copy link
Member

hadley commented May 30, 2017

The major downside of this is that it exposes an internal data structure, preventing me from making changes in the future.

@klmr
Copy link
Contributor Author

klmr commented May 30, 2017

Yes, and I would be more than happy to wrap this in some way.

However, in a way the structure is already part of the public API because it’s the input to the (public but mostly undocumented) roclet_process generic.

If it helps, this can always be left as a deliberately undocumented type, with the remark that it’s only useful as input to roclet_process. This is quite common; sometimes it’s called “opaque type”.

R/parse.R Outdated
#' Roxygen comment.
#' @param file_encoding The file encoding. Default: `"UTF-8"`.
#' @param text Instead of specifying `file`, users can also specify a character
#' vector `text`, containing the R code.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs indent. Maybe document with text?

Can you please add @keyword internal

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean “… document with file”? Otherwise I’m not sure what you mean by this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry.

R/parse.R Outdated
}

options <- list(wrap = wrap, markdown = markdown)
parse_blocks(file, env, registry = registry, global_options = options,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please follow new standard for wrapping long lines? (http://style.tidyverse.org/syntax.html#long-lines)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, here. Does this also apply to the parameter list in the function definition?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup

R/parse.R Outdated
parse_code <- function(file, env, registry = default_tags(), wrap = FALSE,
markdown = markdown_global_default,
file_encoding = "UTF-8", text) {
if (missing(file) && ! missing(text)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love interfaces where you have to specify exactly one of a set of arguments. I don't see any alternative here, but I think you need to make the structure clearer by placing file and text close together in the argument list. It might be better to default text = NULL and then condition on that.

* Mark function as “internal”
* Move `text` parameter to be adjacent to `file`, document together
* Make `text` parameter default to `NULL`
* Follow tidyverse style for wrapping long lines
@klmr
Copy link
Contributor Author

klmr commented Aug 17, 2017

Please take another look.

R/parse.R Outdated
if (missing(file) && ! missing(text)) {
#' @keywords internal
parse_code <- function(
file, text = NULL,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh except the rule for function blocks is that you indent with the ( in function. I need to document that in style.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, I found an example elsewhere in the code base. Makes sense.

@hadley hadley merged commit 7dee044 into r-lib:master Aug 17, 2017
@hadley
Copy link
Member

hadley commented Aug 17, 2017

Thanks!

@klmr klmr deleted the single-file-support branch May 25, 2021 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants