Putting this as an answer for visibility: this happens if you try to run by selecting all in the Rmd and pressing enter like you would with a normal R script. RStudio tries to run this all as R code, including the markdown parts, leading to the errors you saw.
You can avoid this by running an individual chunk by clicking the green play button or by selecting one of the run options in the dropdown at the top of the Rmd editor.
The problem could be due to the object being changed in the global environment in an earlier session and that session got saved in the global ennvironment. It is better to not save anything in the global environment, while ending the Rstudio session (or R console). One option would be to call the data(cars) again so that we get the original dataset
—
title: “Untitled”
output:
html_document: default
‘html_document:’: default
—
“`{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
“`
“`{r cars}
data(cars)
summary(cars)
-output
One option to avoid these kind of surprises is to use the “Don’t save” option while quitting the session