How do I use my own dataframe, that is stored in the global environment, in RMarkdown?
---
title: "Thing is stuck"
author: "Yo"
date: "6/10/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r df}
summary(cars)
summary(df)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
*Quitting from lines 19-21 (Untitled.Rmd) * Error in object[[i]] : object of type 'closure' is not subsettable Calls: ... withVisible -> eval -> eval -> summary -> summary.default Execution halted
That tells me you don't infact have a data.frame of yours called df;
In a fresh R-session, df is a function, type ?df in your console.
I would suggest, you review whats gone wrong given your expectation that you have a particular data.frame hasn't been borne out. Did you skip a step of creating it, or loading it in ?
A minor suggestion would be to not call it df, as df clashes with the function name, the benefit of so doing is that you are more likely to get a meaningful error message when you try to access it and its not there, and also would dodge an issue if you wanted to use the function.
I am not sure I follow.
My dataframe "df" is present, because I am using it.
I am creating plots, tables, doing calculations, etc. It just stops responding when I try to use it with RMarkdown.
As we speak I just called the dataframe and everything showed up without a problem.
The thing is that RMarkdown does not seem to recognize it.
I guess on closer reading of your response to this, you did not do it, but gave an error from the markdown ?
which I assume is the same as error from working on console...
honestly, your issue is unusual, and seemingly unreproducible, I would suggest you restart R, in case your session is messed up. I'd expect Rstudio markdown to have access to what your console can access
I suspect you are not just running code chunks interactively but knitting the document to html output in which case, the code is evaluated in a clean environment where df doesn't exist, you have to include in your Rmd file, the code for defining/loading df, otherwise it will only work interactively in your current working environment.
rmarkdown aways renders in a new environment so anything not defined in the rmarkdown file will not be available. However is is possible to pass objects in by using the render function and setting a base environment. For example:
Then in your Rmarkdown file df is essentially available.
It is also possible to pass in values using the params option, these need to be defined in the yaml header. Personally I find it easier to use an environment.