如何将一个块中的数据绘制到另一个块中?

How to plot data from one chunk in another chunk?

我正在创建一个尝试应用 ARIMA 模型的博客。每个块由数据准备中的一个步骤组成。最后一步是绘制数据。但是,我无法终生获取最后一个块来使用之前块中的数据。

我试过 cache=TRUE 全局和本地。我试过 ref.label 和 dependson。不管它不起作用。源码不包含任何CACHE命令,不过我试过了。

```{r packages, message=FALSE}
library(quantmod)
library(tseries)
library(timeSeries)
library(forecast)
library(xts)
```

### Data Preparation

Time to pull the data. This line of code pulls daily prices including volume.
```{r pull, message=FALSE, eval=FALSE}
getSymbols('DANSKE.CO', from='2014-08-01', to='2019-08-01', src = 'yahoo')
```

I'm only going to use the adjusted close price. I simply reassign the Dansk Bank variable to only contain the adjusted close data.
```{r clean, message=FALSE, eval=FALSE}
DANSKE.CO <- DANSKE.CO[,4]
```

Next I'm transforming the prices by taking the log. This can help achieve lower variance before the differencing. Furthermore, much finance litterature often assumes prices are log-normal distributed and I'm no position to question the status quo right now.  
```{r log, message=FALSE, eval=FALSE}
DANSKE.CO <- log(DANSKE.CO)
```

Finally I'm interested in the log-returns not log-price. 
```{r returns, message=FALSE, eval=FALSE}
DANSKE.CO <- diff(DANSKE.CO, lag=1)
DANSKE.CO <- DANSKE.CO[!is.na(DANSKE.CO)] #Removes the first row since it does not contain the daily return.
```


Alright. Let's look at the data.
```{r plot_data, echo=FALSE, message=FALSE}
plot(DANSKE.CO, main='Danske Bank')
```

Error in plot(DANSKE.CO, main = "Danske Bank") : 
  object 'DANSKE.CO' was not found
Call: local ... withCallingHandlers -> withVisible -> eval -> eval -> plot

如评论中所述,问题可能是由于使用 eval = FALSE 块选项引起的。

DANSKE.CO 不是 created/modified 在你的 R Markdown 块中,因为你正在使用 eval = FALSE 块选项。 eval = FALSE 选项告诉 R Markdown 文档 not 运行 块中的代码。从您的块中删除这些设置很可能会解决您的问题。

请参阅 Yihui Xie 的 R Markdown 书中的第 2.6 章,该书的作者对 R Markdown 选项进行了更深入的解释。

https://bookdown.org/yihui/rmarkdown/r-code.html