yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.36k stars 873 forks source link

Add support for `# %%` for chunk demarcation in `knitr::spin` #2307

Closed kylebutts closed 7 months ago

kylebutts commented 7 months ago

Hi,

The R VSCode extension has nice support for inline code chunks for R scripts using the # %% comment to separate code chunks. This small change would integrate with this and allow a seamless .R notebook script to report pipeline

This will also help with https://github.com/quarto-dev/quarto-cli/issues/6660 allowing to render .R scripts with quarto https://quarto.org/docs/prerelease/1.4/script.html

Running knitr::spin("test.R", format = ".qmd) on this

#'---
#'title: "test"
#'---

# %% setup
#| message = FALSE,
#| warning = FALSE
library(tidyverse)

v = seq(-pi, pi, 0.01)
data = data.frame(
  x = v, y = sin(v)
)

# %% 
ggplot() + 
  geom_point(
    aes(x = x, y = y),
    data = data
  )

produces a valid qmd document:

---
title: "test"
---

```{r setup}
#| message = FALSE,
#| warning = FALSE
library(tidyverse)

v = seq(-pi, pi, 0.01)
data = data.frame(
  x = v, y = sin(v)
)
ggplot() + 
  geom_point(
    aes(x = x, y = y),
    data = data
  )
CLAassistant commented 7 months ago

CLA assistant check
All committers have signed the CLA.