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

Support for .Rmd, .Rpres & .Rnw #98

Closed
AdamSpannbauer opened this issue May 14, 2019 · 5 comments
Closed

Support for .Rmd, .Rpres & .Rnw #98

AdamSpannbauer opened this issue May 14, 2019 · 5 comments

Comments

@AdamSpannbauer
Copy link
Collaborator

Related to #95

To support file extensions outside of .R well some additional logic will be needed. If we choose to mirror the support packrat has then this includes adding support for .Rmd / .Rpres & .Rnw.

I'm not familiar with .Rnw tbh. packrat handles it with their helper packrat:::fileDependencies.Rnw which uses utils::Stangle then runs their R dependency finder on the output.


Below is a potential start to a string based dependency finder for .Rmd / .Rpres

rmd_lines = readLines("test.Rmd")

# Find lines that start with ```{r
chunk_start_lines = grep("^```\\{r\\b", rmd_lines)

# Find lines that start with ```
potential_chunk_end_lines = grep("^```", rmd_lines)

# Rm ```{r lines from candidates list of chunk enders
potential_chunk_end_lines = setdiff(potential_chunk_end_lines, chunk_start_lines)

rmd_code_chunks = lapply(chunk_start_lines, function(chunk_start_line) {
  # Find which candidate endline occurs most soon after an r code chunk start line
  all_line_diffs = potential_chunk_end_lines - chunk_start_line
  valid_line_diffs = all_line_diffs[all_line_diffs > 0]
  chunk_end_line_diff = valid_line_diffs[which.min(valid_line_diffs)]
  chunk_end_line = potential_chunk_end_lines[all_line_diffs == chunk_end_line_diff]

  # Subset codechunk lines (drop ``` lines)
  rmd_lines[(chunk_start_line + 1):(chunk_end_line - 1)]
})

rmd_r_code = unlist(rmd_code_chunks)

Contents of "test.Rmd":

---
title: "Untitled"
output: html_document
---

```{r, setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{bash, setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

mgsub::mgsub
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
library(dplyr)
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
@bmewing
Copy link
Owner

bmewing commented May 14, 2019

How critical is it that we support beyond .R and .Rmd/.Rpres?

@AdamSpannbauer
Copy link
Collaborator Author

Probably not too critical. Maybe just work in support for md/pres and then come back to Rnw if requested? As far as I know, I've never come across a Rnw in the wild.

@bmewing
Copy link
Owner

bmewing commented May 14, 2019 via email

@bmewing
Copy link
Owner

bmewing commented Oct 12, 2019

@AdamSpannbauer this was addressed in #105 right?

@AdamSpannbauer
Copy link
Collaborator Author

@bmewing yep

Per this thread, we abandoned support for .Rnw. Longterm all this parsing would be better done by renv if they publish to CRAN with their parser exported (open issue for this in #58)

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

No branches or pull requests

2 participants