48241

How to show formatted R output with results='asis' in rmarkdown

<h3>Question</h3>

Is there a way of showing formatter R output in rmarkdown/knitr when using results = 'asis'?

An example would be the following function:

myfun <- function() { cat("hello!\n") cat(c("one" = 1, "two" = 2)) }

Then, this chunk will print the second cat on a new row:

```{r} myfun() ```

But this will ignore the formatting from myfun:

```{r, results = "asis"} myfun() ```

Is there a way of keeping results='asis' but at the same time keep the output of myfun formatted as intended?


<h3>Answer1:</h3>

You can use the knitr chunk option results = "asis" if you are happy to add two or more spaces at the end of the line. That is, instead of "hello\n", you need to write "hello \n" to trigger the line break.

Example R Markdown code:

--- output: html_document --- ```{r} myfun <- function() { cat("hello! \n") cat(c("one" = 1, "two" = 2)) } ``` ```{r results = "asis"} myfun() ```

Gives

Why the blank spaces? It's because two spaces at the end of a line are used to indicate a hard line break in markdown. For example, this quote is taken from Pandoc's Markdown (which is the default markdown flavour R Markdown uses):

<blockquote>

Paragraphs
A paragraph is one or more lines of text followed by one or more blank lines. Newlines are treated as spaces, so you can reflow your paragraphs as you like. If you need a hard line break, put two or more spaces at the end of a line.

</blockquote>

来源:https://stackoverflow.com/questions/52631689/how-to-show-formatted-r-output-with-results-asis-in-rmarkdown

Recommend

  • How to solve CORS problem of my Django API?
  • Why no memory leak?
  • Django: copy data from one database to another
  • multi column sorting of datagrid view:
  • How do I use the private key from a PFX certificate stored in Azure Key Vault in .NET Core 2?
  • ObjectDisposedException on a ASP.NET Core 2.0 MVC custom database initializer class
  • Angular 4 Animation won't trigger on route change
  • Issue while running Linux Command using Java?
  • Unity3D display Google Admob slow
  • Fill polygon with pattern doesn't work with leaflet
  • Perl is respecting '
  • Tooltip on Hover of select box options generated by ng-options in Angularjs
  • How to pivot table for year in SQL?
  • Azure function C#: Create or replace document in cosmos db on HTTP request
  • Muxing a H.264 Annex B & AAC stream using libavformat with vcopy/acopy
  • How to place UI widgets on top of multiple Z ordered Surface Views in Android
  • Should be localization part of domain in DDD
  • How to Enable Systemd service in openshift/jenkins-1-centos7 docker container?
  • I am trying to create an app in android to insert data into sql server through a web service.
  • Possible steps to improve SQL Server query performance
  • limit python input to int
  • Forward slash vs backward slash for file path in git bash
  • Dynamic reference casting depending on actual object type
  • date changes on export kendoGrid
  • Unable to connect to AWS RDS through PDO
  • How to put an object in the air?
  • Is possible having two COM STA instances of the same component?
  • Sample deviceQuery cuda program
  • Android studio import problems. (Apktool)
  • Checking for valid enum types from protobufs
  • playing mp3 from nsbundle
  • Building JavaFX 2.0 App on Mac, deploying on Windows
  • Rotating Towards Path in OpenGL
  • C++ STL stack pop operation giving segmentation fault
  • `$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?
  • Google App Engine backend servlet not responding
  • Make checkout phone field optional for specific countries in WooCommerce
  • Excel VBA : conditional formatting of sheet1 cells from sheet2 values in excel 2007
  • XEP-0166: Jingle protocol implementation for voice/video chat in iOS
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?