3
votes

Is there a way to parse text as is in rmarkdown, like the <pre> tag for HTML but for docx (Microsoft Word) export with knitr? When I use the tag <pre> it hides it in words... I could use a R code chunk and commenting it out but maybe there is a better way...

I would like to copy the output from R and paste it in my text markdown so it is always there...

For example

[1] “Results”  
1   2   3   4   5   6   7

gives the following in Word after knitr export:

[1] “Noeuds” 2 3 4 5 6 7

I would like to keep the same formatting as the example..

This is what I use at the moment with an R code chunk:

```{r}
#[1] “Results”  
#    1   2   3   4   5   6   7
```
1
are you looking for asis=TRUE in the chunk options? yihui.name/knitr/options/#text-results (your question isn't entirely clear)Ben Bolker
not really, I run the code in R, I copy paste the result in my rmarkdown file as normal text (without chunk) , but I want to keep the initial formating. Hope that is clearer..:/R. Prost
but why are you copy and pasting? Can you please post a screenshot (not usually a good idea, but useful in this case) showing the formatting output you want in the final docx file? (I know you don't know how to generate it automatically, but you should be able to do the formatting by hand in Word to illustrate what you want ...)Ben Bolker
I think there's a better solution, but in markdown triple-backtick (<code>```</code>: probably won't render right in a comment) denotes a generic code chunk (knitr won't try to execute it)Ben Bolker
I appreciate the time you put in thinking about it :) I wanted to have the text as under "For example" but without the grey box. The triple tick works but leaves a grey box. But that's close enough for me :P thks !R. Prost

1 Answers

3
votes

Either indent the text block by four spaces, or put it in a fenced code block, e.g.,

```
[1] “Results”  
1   2   3   4   5   6   7
```

See Pandoc's documentation on verbatim (code) blocks.