'R error message output goes off the slide in beamer presentation R markdown

I'm using R markdown to make a beamer presentation.

When I try to include an error message output from R on the slide it goes off the page.

Here is my code:

---
title: "Untitled"
author: "John Smith"
date: '2022-04-29'
output: beamer_presentation
---


This error message goes off the page

```{r cars, echo = TRUE, error=TRUE}
summary(carrrrrrrrrrrrrrrrs)
```

And, this is what the slides look like: enter image description here

How do I keep the code on the page, either by putting it on multiple lines or reducing font size?



Solution 1:[1]

You can use the same technique as in Change representation (background-color & frame) of chunks in RMarkdown (with Beamer-Presentations) to redefine the verbatim environment in such a way that it uses the listings package. The listings package then allows you to both add line breaks and adjust the font size to your liking:

---
title: "Untitled"
author: "John Smith"
date: '2022-04-29'
output: 
  beamer_presentation:
    keep_tex: true
header-includes:
  - \let\verbatim\undefined
  - \let\verbatimend\undefined
  - \usepackage{listings}
  - \lstnewenvironment{verbatim}{\lstset{breaklines=true,basicstyle=\ttfamily\footnotesize}}{}
---


This error message goes off the page

```{r cars, echo = TRUE, error=TRUE}
summary(carrrrrrrrrrrrrrrrs)
```

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1