The Byzantine world of environments in R Markdown and knitr

I'm trying to create a very simple R markdown and cannot get through sorting the environments. But before I even get to knitting the document I have a problem when running the operative code chunk. I read a file into input_df, filter it and put the result in output_df. It appears in my global environment when I run the chunk. Then I have View(output_df) as the next line in the chunk. It works. I see output_df.

Next line is to write output_df using the write_xlsx function. Error in is.data.frame(x) : object 'ouput_df' not found. Why can I see output_df in my global environment and why did the program see it in the immediately preceding line of code, but not in the code beginning with write_xlsx?

Here's the code for the example:

---
title: "attempt to create a reproduceable example"
author: "Don Fogel"
date: "`r Sys.Date()`"
output:
  prettydoc::html_pretty:
    theme: architect
    highlight: github
    toc: true
editor_options: 
  chunk_output_type: console
params:
      inspector_name: "Joe"
    
---


```{r setup, include=FALSE} 
knitr::opts_chunk$set(cache = TRUE, warning = TRUE, message = FALSE)
library(rmarkdown) 
library(tidyverse)  
library(readxl)      
library(writexl)     
library(shiny)
``` 

```{r main, echo=FALSE}
input_df <- read_excel("/Users/home/Dropbox/R/test_project_2/test_data_2.xlsx")
 output_df <- input_df %>%  
          filter(PayrollName == params$inspector_name)
View(output_file)
write_xlsx(ouput_df,"/Users/home/Dropbox/R/test_project_2/test_output_2.xlsx" )
```

Change outpu.df to output.df

3 Likes

Thank you. I'm an idiot for not seeing that. So I fixed that, and now my ultra-simple R markdown works. I was having a different problem before in my more complex working file. I'll see if I can avoid that issue or at least create a simple example of it.
Thanks, again.

1 Like

We all need a second pair of eyes😎

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it: