0
votes

Forgive the very basic question but I'm having some trouble getting the knitr feature to work just like typing in the console.

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

In the console use code like str(reddit) to view the structure.

Now open a new R markdown file from the R Studio menu and include a chunk like the following

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

I know its something very obvious but can't place my novice finger on it. Any help would be appreciated

1

1 Answers

3
votes

You have to define the object in the Knitr code. 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).

As an example, here’s a knitr file which does minimally what you want to do:

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

Structure of Reddit

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