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

时间:2019-08-03 18:41:12

标签: r r-markdown knitr

我创建了一个博客,尝试应用ARIMA模型。每个块都包含一个数据准备步骤。最后一步是绘制数据。但是,我无法获得最后一个块来使用以前块中的数据。

我已经尝试过cache = TRUE全局和局部。我试过ref.label和依赖。不管什么都不起作用。源ode不包含任何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

1 个答案:

答案 0 :(得分:1)

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

在您的R Markdown块中未创建

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

请参阅Yihui Xie的R Markdown书中的2.6章,对软件包作者的R Markdown选项进行了更深入的说明。

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