R Markdown: How to use my own dataframe?

Hey guys, I am feeling a bit silly here.

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.

Does it have a name?

Yes, it is called "df".

Where it is "summary(df)" it does not get read by RMArkdown

...
And what happens when you leave markdown and just directly put summary(df) into the console?

*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...

Oh forgot to reply to that one. Yeah when I write summary(df) I get a normal summary from the dataframe.

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

1 Like

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.

1 Like

Oh... I thought we could simply call what I had done in other scripts ... but this way my RMarkdown code will be huge... I am so confused right now

You could source the script that generates df or export the data as an intermediate output and load it into your Rmd file.

1 Like

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:

df <- data.frame()

my_env <- new.env()
my_env$df <- df

rmarkdown::render('My_Markdown.Rmd', envir = my_env)

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.

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.