Hi - I'm new to R Markdown and have a pretty basic question. I'm trying to document a script that I wrote in R that uses sandwiches multiple chunks of R code between text. Everything seems to knit correctly until I try to include a plot. In one chunk, I define a value as "param_a", but later in a subsequent chunk ...
{r param_a, echo=FALSE}
hist(param_a)
I get the following error
"Error in hist(param_a) : Object 'param_a' not found
Calls <Anonymous ... withCallingHandlers -> withVisible -> eval -> eval -> hist Execution halted"
Is there something I need to do to have param_a carry over between chunks, or an option I need to invoke?
No, this should work.
Can you show us both complete chunks. I.e the chunk were you define parm_a and the one you use it in.
Enclose the chunks in four backticks (if you use the </> button you get only three)
Update:
Using the standard template gives me a nice histogram:
---
title: "Untitled"
author: "My name"
date: "6/25/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r param_a0, echo=FALSE}
set.seed(2020)
param_a= sample(1:10,25,replace=T)
```
```{r param_a, echo=FALSE}
hist(param_a)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.