Function formatting in R Markdown documents


I want to render function code in a R Markdown document. The problem is, that I don't like the formatting of the rendered function. Is there a possibility to nicely render a function in a R Markdown document?

Here is an example of an output I would like to format. I would like to format the stringr::str_to_upper function without the ## at the beginning of the rows and without the bytecode and environment information.

title: "Untitled"
date: "13 7 2020"
keep_md: yes

## function (string, locale = "en") 
## {
##     stri_trans_toupper(string, locale = locale)
## }
## <bytecode: 0x000001e74c8227d0>
## <environment: namespace:stringr>

Thank you very much for any help :slight_smile:

I can partially answer this!

When creating the chunk that contains str_to_upper, you can change the chunk options to modify the output decoration. To remove the ##, use

``` {r, comment=""}

If you want this as a global setting (so all chunks in document will lack the ## decoration), modify the setup chunk at the top:

```{r setup, include=FALSE}

I have no idea how to help with removing the bytecode or environment output, but here is where chunk options are further described?

1 Like

You can try something like this:

output: html_document

some text

```{r, comment=""}
body(fun = stringr::str_to_upper)

some more text

This yields this for me:

Hope this helps.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.