Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve R Markdown (Knit) "'closure' is not subsettable"?

I am trying to use RMarkdown (Knit) for the first time to produce pdf. The default file (File > New File > R Markdown) works well, it shows the generated pdf when compiled. For example, the following code runs,

```{r cars}
summary(cars)
```

However, if I just change cars with "myData," it does not compile and shows,

Error in object[[i]] : object of type 'closure' is not subsettable
Calls: <Anonymous> ... withVisible -> eval -> eval -> summary -> summary.default
Execution halted

I have "myData" loaded in the global-environment and can do other operations in original R script. Can someone please provide some guideline. Thank you very much for your time.

like image 514
Droid-Bird Avatar asked Oct 28 '25 17:10

Droid-Bird


1 Answers

Running an Rmarkdown file starts a new R session.

Within the new session, you can load the data.frames that are stored in the data package, but other datasets must be loaded from within the Rmarkdown document.

To get myData to show up in your Rmarkdown document,

  1. save the file somewhere with save in your current R session
  2. then in your Rmarkdown document, use load to open up the data set

So, in your current R session:

save(myData, file="<path>/myData.Rdata")

and in your Rmarkdown file:

```{r myDataSummary}
load("<path>/myData.Rdata")
summary(myData)
```

If your data is stored as a text file, and you don't wish to store a separate .R file, use read.csv or friend directly within your Rmarkdown file.

```{r myDataSummary}
myData <- read.csv("<path>/myCSV.csv")
summary(myData)
```
like image 189
lmo Avatar answered Oct 31 '25 08:10

lmo