【问题标题】:How to plot data from one chunk in another chunk?如何在另一个块中绘制一个块中的数据?
【发布时间】:2019-08-03 18:41:12
【问题描述】:

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

我在全局和本地都尝试过 cache=TRUE。我已经尝试过 ref.label 和依赖。不管它不起作用。源代码不包含任何 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

【问题讨论】:

  • 您究竟在哪里创建 DANSKE.CO 作为 R 的 .GlobalEnv 中的对象?另外,您到处都有 eval = FALSE。
  • 我添加 eval = FALSE 是罪魁祸首。谢谢!

标签: r r-markdown knitr


【解决方案1】:

如 cmets 所示,问题可能是由于使用了 eval = FALSE 块选项造成的。

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

参见 Yihui Xie 的 R Markdown 书的第 2.6 章,包作者对 R Markdown 选项的更深入解释。

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

【讨论】:

  • 谢谢!我通过删除所有 eval = FALSE 来解决它。我最初添加它是因为我想在后台添加代码,但不包含在博客文章中的输出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-04
  • 1970-01-01
  • 1970-01-01
  • 2019-02-13
  • 2023-03-18
  • 2021-10-26
相关资源
最近更新 更多