21622

How to merge and print multiple code chunks in RMarkdown?

<h3>Question</h3>

My Rmarkdown document looks something like this.

--- yaml metadata --- ```{r} x <- 10 ``` Some code explanation ```{r} y <- 10 ``` Some more code explanation ```{r} z <- x + y ``` The final output ``` # 10 ```

Since I am following the concepts of literate programming, how to print these multiple code chunks stitched together, so I can get the whole working code printed out as follows without the code explanation. Also, can I select specific code chunks and not all and print them out?

x <- 10 y <- 10 z <- x + y
<h3>Answer1:</h3>

A trick is to use knitr's ref.label="" chunk option (which takes one or more block labels). It requires that you label your chunks (at least the ones you want to repeat). For demonstration, I've "hidden" (echo=FALSE) one of the blocks to show that the output can be offset (as in https://stackoverflow.com/a/30243074/3358272) though it is still executed in-place.

--- output: md_document --- ```{r block1} x <- 10 ``` Some code explanation, next code block hidden here but still evaluated ```{r block2, echo = FALSE} y <- 10 ``` Some more code explanation ```{r block3} z <- x + y ``` The final output ``` # 10 ``` # Annex 1 Each block individually: ```{r showblock1, ref.label='block1', eval=FALSE} ``` ```{r showblock2, ref.label='block2', eval=FALSE} ``` ```{r showblock3, ref.label='block3', eval=FALSE} ``` # Annex 2 If you want them more compactly concatenated: ```{r showblocks, ref.label=c('block1','block2','block3'), eval=FALSE}

Produces this markdown file when rendered:

x <- 10 Some code explanation, next code block hidden here but still evaluated Some more code explanation z <- x + y The final output # 10 Annex 1 ======= Each block individually: x <- 10 y <- 10 z <- x + y Annex 2 ======= If you want them more compactly concatenated: x <- 10 y <- 10 z <- x + y

You can render into whatever format you want, the results should be similar.

来源:https://stackoverflow.com/questions/52952797/how-to-merge-and-print-multiple-code-chunks-in-rmarkdown

Recommend

  • How to convert many rows into Columns in SQL Server?
  • Setting TableView generic type from FXML?
  • Haskell logbase error
  • What is the use and meaning of %2d in the C printf function?
  • RestSharp PUT XML, RestSharp is sending it as GET?
  • ProgressDialog.show inside AsyncTask stop my Program from execution
  • Fitbit oauth2 public API stopped working. Giving error - Sorry Its not you.. Its us
  • How to bind caliburn.micro view model to WP8 LongListSelector control?
  • Two way binding not working in ng-bootstrap radio button in an angular reactive form
  • XCUITest: Running tests across two apps in a coordinated fashion
  • Querying a collection of embeddable elements using eclipselink
  • search the database based on the value selected from database
  • maximizing profit for given stock data via DP
  • Ellipsis directive with title
  • Find a directory using wildcard in Inno Setup
  • Pass multiple lines of stdin input to interactive Java command line program, non-interactively
  • Detect when MathJax has finished loading in UIWebView
  • getting the values of checkboxes in a checkboxlist control
  • the IBM_JAVA error for running jobs in Hadoop 2.2.0
  • connect.cookieParser and connect.session
  • Python sum values in tuple in a list in a dictionary?
  • Threads and Concurrent Modification Exception working with a list
  • Validating my form with Jquery
  • How to change the host IP sent in emails to new GitLab users to a publicly visible IP, not the local
  • Java Collections.shuffle() weird behaviour [closed]
  • Query takes almost two seconds but matches only two rows - why isn't the index helping?
  • What is the difference between dynamically creating a script tag and statically embed a script tag?
  • Regex not working in java 1.5
  • Google Spreadsheet Script to Blink a range of Cells
  • how to snap two objects in runtime in unity?
  • Using redis as an LRU cache for postgres
  • Another “Cannot make static reference…” Question
  • Why isn't stemDocument stemming?
  • Bad automatic Triangulation with Mayavi for coloring a surface known only by its corner
  • Cross compile glibc for arm, got undefined reference to some unwind functions
  • Using Service Component Runtime
  • How to mutate multiple variables without repeating codes?
  • No OpKernel was registered to support Op 'Conv2D' with these attrs