0

I am trying to run the below code and generate a report using R Markdown . Unfortunately I keep getting the below error. The code is processed dataset is taken into the environment and I can see the plots being generated without any issues. Provided the code as well for reference. R Markdown is always troublesome when I use it :(. Appreciate any assistance please. Thank you

Error in attach(mydata1) : object 'mydata1' not found
    Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> attach
    Execution halted

Rmd:

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
### Load package and libraries

library(dplyr)
require(reshape)
library(ggplot2)
library(ggpubr)
library(DMwR)
library(mice)

```

```{r}
attach(mydata1)
head(mydata1)
summary(mydata1)
str(mydata1)
```
r2evans
  • 141,215
  • 6
  • 77
  • 149
Daisy
  • 1
  • 1
  • 1
  • 5
    Where does mydata1 come from? Just calling `attach` won't do it. [Actually, just don't call attach. ever ;)](https://stackoverflow.com/questions/10067680/why-is-it-not-advisable-to-use-attach-in-r-and-what-should-i-use-instead). We need to import or load `mydata1` properly, using depending on the data type, `load` or `read.table` or `read.csv` or something similar – dario Mar 10 '20 at 12:57
  • Thank you @dario. I imported the dataset using the import dataset option in the Environment tab. The dataset did get imported successfully and even got attached which is why the plots are good. Only R Markdown seem to not recognize it – Daisy Mar 10 '20 at 13:32
  • 1
    *I imported the dataset using the import dataset option in the Environment tab* - as I already said: We need to import or load `mydata1` properly. `knitr` doesn't know about the environment tab, it uses its own environment when kniting. Add the import statement to the `.Rmd` and it will work... (alternatively, and I do not recommend it, if you *really*, *really* want to use the global environment you could try [this answers](https://stackoverflow.com/questions/34029611/how-to-use-objects-from-global-environment-in-rstudio-markdown)) – dario Mar 10 '20 at 13:46
  • 1
    Daisy, one well to reproduce what the rmarkdown environment will look like is to ... restart R from scratch. If you do something with the RStudio environment (such as "import data"), then you need to know the code to do that *programamtically* (not with menus and mouse clicks) and insert that code into this document. That's intentional, and a big step towards *reproducibility* in reports. – r2evans Mar 10 '20 at 15:02
  • Thank you so much for the response. It helped. – Daisy Mar 15 '20 at 02:45

0 Answers0