Reputation: 141
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!
Upvotes: 0
Views: 2452
Reputation: 83
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.
Upvotes: 2