0

I want to create a R Notebook/markdown output and am failing hard with formatting code chunk outputs.

When creating a code chunk where I don't specify the language, the code chunk is always printed with white background, whereas code chunks specified with the R language are rendered with grey background.

Update/Edit: the non-R code won't contain valid R objects/commands. Instead it just contains random text that I want to put into grey code boxes (could be some gibberish like "asdgfasdtcasdhsagd").

How can I also provide a grey background to the other/all code chunks?

Example (Rmd file)

---
title: "R Notebook"
output: html_notebook
---

```
TEST
ABC
```

    TEST
    ABC

```{r}
x <- 3
```

The code chunks for TEST/ABC are displayed with white background (but I want them to be grey). Any ideas? Thanks.

deschen
  • 10,012
  • 3
  • 27
  • 50
  • A possible solution is to use your own CSS file, e.g. https://bookdown.org/yihui/rmarkdown/html-document.html#custom_css – J_F Mar 30 '20 at 19:09

1 Answers1

0

You have a solution here

Basically, you can specify options in a css chunk or link to a css document (see here). For the first option for instance:

   ---
   output: html_document
   ---

    ```{css}
    .badCode {
     background-color: grey;
    }
    ```

    ```{r cars, class.source="badCode"}
     summary(cars) 
     ```

The second solution is preferrable if you want many elements to customize your theme.

Update from edit

With an external css

Maybe you could custom the following formatting elements

style.css:

.mystyle {
  background-color: grey;
  border-radius: 15px;
  border: 5px solid #ff0000;
}

And in your Rmd (don't forget to point to your css in the yaml preambule) :

::: {.mystyle}
I put elements non R-related there
:::

This styling elements are a recent feature in Rmarkdown. I used them in bookdown only but there is no reason it should not work for rendering. There are some explanations there

With css elements defined inside your document

   ---
   output: html_document
   ---

    ```{css}
    .mystyle {
     background-color: grey;
    }
    ```

    ```{r cars, class.source="badCode"}
     summary(cars) 
     ```

::: {.mystyle}
I put elements non R-related there
:::
linog
  • 5,786
  • 3
  • 14
  • 28
  • Hmm, I'm struggling getting this to work. It throws an error when I try to knit as an html document, probably because the code is no R code, whereas your code is still a valid R object (cars). So how can I make sure it works with non-R code? – deschen Mar 30 '20 at 19:41
  • ...which might be due to difference between previewing the output as an R notebook vs. knitting it as an html document (although both are html). Phew, this is confusing. – deschen Mar 30 '20 at 19:49
  • 1
    Not sure I get it. You want to put non R-code in your notebook : is it for preview or execution ? For preview, you can still use standard html_document class by putting chunks with `eval = FALSE` (e.g. ```{toto, eval = FALSE, class.source="badCode"}``` as header). If you want to evaluate the non R-language: it evaluates the language with `html_document` but not with `html_notebook` ? That's the problem ? – linog Mar 30 '20 at 20:08
  • I don't really know. It seems I don't get the difference between a notebook and a knitted html. When i knit an html, it just runs all the code and shows the code + results as a html. When "previewing notebook" it doesn't do so until I've actively run the code chunks. So adding the eval = FALSE thing helps for knitting an html, but for previwing as a notebook a must not run the respective code, because running the code chunk even with eval = FALSE will throw an error like `Error: object 'TEST.' not found`. – deschen Mar 31 '20 at 06:56
  • I realize this might not have been clear from my initial post that the non-R code doesn't contain valid R objects/commands. I'll update the post accordingly. – deschen Mar 31 '20 at 11:31
  • I updated my answer. Does it help you figuring out the solution ? – linog Mar 31 '20 at 11:45
  • Do I assume right that I need to provide the css code in an external style.css file or can this also go to my markdown document code? – deschen Apr 01 '20 at 13:25
  • It's not compulsory. I update my answer to give you an example (I just tried it into my laptop). It's just for **rendering** your custom language – linog Apr 01 '20 at 13:55
  • Is your output html_document or html_notebook? Because, using html_notebook, `class.source="badCode"` doesn't work at all – Salix Jun 30 '20 at 21:52