简体   繁体   中英

R Markdown error in code when knit to HTML

I am trying to run code chunks in my markdown document. I have an R script that runs all the code that I need without any issues. Then, when I copy and paste the code into the markdown document, the code will run within the chunk, but will fail when trying to knit into an output document (html/pdf).

I had to create a safe.ifelse function to prevent r from converting my dates to a numeric format as discussed here .

The error appears to be with the code:

safe.ifelse = function(cond, yes, no){structure(ifelse(cond, yes, no), class = class(yes))
}

The error message I get is:

Line 121 Error in structure(ifelse(cond,yes,no), class = class(yes)) : could not find function "days" Calls: ... transform.data.frame ->eval->eval-> safe.ifelse-> structure Execution halted

The line of code following my safe.ifelse function is

seminoma1 = transform(seminoma1, recur.date = safe.ifelse(salvage.tx=="Yes",
date.diagnosis + days(pmax(time.rad, time.chemo, na.rm=TRUE)), NA))

Any help would be appreciated. Thanks.

I'm still too new to comment, but the only time I get an error like that is when I forget to define a function/variable or forget to source a package.

Since days() isn't part of R's base package, I think you need to add:

```{r echo = FALSE}
library("lubridate")
```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM