rr-markdownreprex

R+reprex: create a reproducible example when rendering a R markdown file


I cannot give an example, otherwise I would not ask this question at all. I love the reprex package for R. However, sometimes I write some simple R markdown code with I process with rmarkdown::render(). Is there a way I can use the reprex package on an R markdown file to generate a reproducible example which is easy to post, for instance, on this site?

Many thanks


Solution

  • You could use the reprex_render function from reprex package to render .R or .Rmd files.

    This is a wrapper around rmarkdown::render() that enforces the "reprex" mentality.

    Here is a reproducible example:

    reprex::reprex_render(input = "reprex_test.Rmd")
    

    Output viewer:

    enter image description here


    Example Rmarkdown file called reprex_test.Rmd:

    ---
    title: "Untitled"
    output: html_document
    date: "2024-02-22"
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    ## R Markdown
    
    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>.
    
    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.