0

I have a markdown file that I have just finished knitting. The file is discussing what I have done in my R project for a data analytics course. I am receiving so many error messages after the knit within my report however this code all worked before while I was working in r projects. for example here. the error appears at the end.

Cleaning and Filtering Step 1: heartrate_data heartrate_data is a very large data set so I filter ‘heartrate_data’ to contain data for user_6 only

heartrate_data <- filter(heartrate_data, Id == "2022484408")
and then filter for date

heartrate_data <- heartrate_data %>% filter(grepl('4/12/2016', Time))
and then re label Id to user_6

heartrate_data[heartrate_data == "2022484408"] <- "user_6"
## Error in as.POSIXlt.character(x, tz, ...): character string is not in a standard unambiguous format

So, my question is if there is no actual error within this code do I need to go through and give attention to all these error messages showing up? I have many more within my markdown. The errors seem un related though because this is how I coded while in my R projects. Is there a way to refrain R knit from including these error messages? Thank you

Here are a few more examples.

view(sleep_datatwo)
## Error in view(sleep_datatwo): object 'sleep_datatwo' not found

sleep_datatwo <- aggregate(TotalHoursAsleep ~ User, sleep_data, mean)
## Error in eval(predvars, data, env): object 'User' not found

sleep_datatwo <- sleep_datatwo %>% 
  rename(
    avg_hours_asleep = TotalHoursAsleep)
## Error in rename(., avg_hours_asleep = TotalHoursAsleep): object 'sleep_datatwo' not found

sleep_datatwo is in my environment, object User is within my sleep_data data frame.

Thank you!

r2evans
  • 141,215
  • 6
  • 77
  • 149
Stackstudent_09
  • 131
  • 1
  • 9
  • (1) `view` in an r-markdown document makes little sense to me, perhaps `glimpse` or `str` _maybe_, but ... not sure. (2) It doesn't matter what is in your global environment, R-markdown documents are run with a clean environment. Use `params:` in your document, see https://bookdown.org/yihui/rmarkdown/parameterized-reports.html. – r2evans May 24 '22 at 00:29
  • So use parameterized reports to exclude these errors from the knit somehow? @r2evans – Stackstudent_09 May 24 '22 at 00:41
  • Use parameterized reports to pass data from the calling environment so that the rmarkdown document actually sees them. – r2evans May 24 '22 at 01:00

1 Answers1

2

The extremely short answer is yes, you can prevent the errors from interfering with knitting like so:

```{r error=TRUE message=FALSE}
your code here
```

See this question here for more explanation, and more details on different chunk options can be found here. However, if you want to display the results of your code, it would probably be best if your code actually ran, right?

All of your errors are object not found errors. I think the problem you are having is that the RMarkdown document does not contain any of the data from your R project. It doesn't matter if the data is loaded in your R environment or in another file within the project. You need to load your data into the RMarkdown document specifically. You can either do this within a code block in the document:

```{r}
data <- read_csv("data.csv")
```

Or by using params as @r2evans suggested. In the header of the R Markdown file:

---
title: Your Title Here
output: html_document
params:
  data: data.csv
---

Also, just fyi, you need to do this with packages too. I recommend you load in all necessary packages and documents in one code block. If you don't want the code to display in the output:

```{r echo=FALSE, results='hide', message=FALSE}
library("tidyverse")
library("car")
data <- read_csv("data.csv")
```

To test that everything is loaded correctly before knitting:

In RStudio - Press the broomstick button in the "Environment" tab to erase all environmental variables. Restart R under the "Session" dropdown menu to start a new clean session.

In base R - Just close R and reopen it to erase all environmental variables and previously loaded packages.

Make sure your RMarkdown file loads in all necessary packages and files, then try knitting again.

eva bacas
  • 65
  • 4
  • (1) `{r error=TRUE message=FALSE}` needs commas (and the last block is missing one, too); (2) `error=TRUE` is the default, that should change the errant behavior. Your points of loading the data or passing params is the key component here, well-explained. – r2evans May 24 '22 at 01:47
  • Okay thank you. I understand the parameters better now. However, these errors can not be from this as I have code within the markdown that loads data.. "First I imported sleepDay_merged, weightLogInfo_merged, heartrate_seconds_merged, and hourlyIntensities data sets." sleep_data <- read.csv("/cloud/project/capstone project/R Projects/sleepDay_merged.csv") weightLog_data <- read.csv("/cloud/project/capstone project/R Projects/weightLogInfo_merged.csv") heartrate_data <- read.csv("/cloud/project/capstone project/R Projects/heartrate_seconds_merged.csv") – Stackstudent_09 May 24 '22 at 21:31
  • I am not required to put these in parameters as they are part of code chunks correct? – Stackstudent_09 May 24 '22 at 21:31