简体   繁体   English

使用未在Knitr markdown文件中解析的R Studio Console命令

[英]Using R Studio Console commands not parsed in Knitr markdown file

Forgive the very basic question but I'm having some trouble getting the knitr feature to work just like typing in the console. 原谅一个非常基本的问题,但是我在使knitr功能像在控制台中键入一样工作时遇到了一些麻烦。

To recreate Download this csv file https://s3.amazonaws.com/udacity-hosted-downloads/ud651/reddit.csv Import it into R Studio calling it reddit so it appears in the Global Environment area of the IDE, Do this using the import dataset from text file menu button 重新创建下载此csv文件https://s3.amazonaws.com/udacity-hosted-downloads/ud651/reddit.csv将其导入R Studio并称为reddit,使其出现在IDE的“全局环境”区域中,请使用从文本文件菜单按钮导入数据集

In the console use code like str(reddit) to view the structure. 在控制台中,使用诸如str(reddit)之类的代码查看结构。

Now open a new R markdown file from the R Studio menu and include a chunk like the following 现在,从R Studio菜单中打开一个新的R markdown文件,并包含如下所示的代码块

Structure of Reddit
```{r}
str(reddit)
```

Knitting the file gets the following error 编织文件时出现以下错误

Error in str(reddit) : object 'reddit' not found Calls: ... withCallingHandlers -> withVisible -> eval -> eval -> str str(reddit)中的错误:找不到对象'reddit'调用:... withCallingHandlers-> withVisible-> eval-> eval-> str

I know its something very obvious but can't place my novice finger on it. 我知道它很明显,但不能将我的新手放在上面。 Any help would be appreciated 任何帮助,将不胜感激

You have to define the object in the Knitr code . 您必须在Knitr代码中定义对象。 Knitr, by design, operates in a clean room and has to be self-contained. 根据设计,针织机应在无尘室内运行,并且必须是独立的。 It doesn't look at your existing RStudio environment (in fact, that would be terrible because it would make the resulting code entirely unreproducible). 它不会查看您现有的RStudio环境(实际上,这将是可怕的,因为它将使所生成的代码完全不可再现)。

As an example, here's a knitr file which does minimally what you want to do: 例如,这是一个knitr文件,该文件可以最小程度地执行您想做的事情:

```{r}
source_file = 'http://s3.amazonaws.com/udacity-hosted-downloads/ud651/reddit.csv'
reddit = read.csv(source_file)
```

Structure of Reddit

```{r}
str(reddit)
```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM