0

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.

Community
  • 1
  • 1
jt.wingate
  • 49
  • 1
  • 8
  • 2
    Is that the `days` function from the `lubridate` package? It looks from the error message (`could not find function "days"`) like you haven't loaded the package before calling the function. – eipi10 Nov 10 '15 at 20:25
  • without the transform, what does it do? I think your `safe.ifelse` function is fine – Harold Ship Nov 10 '15 at 21:33

1 Answers1

0

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")
```
Phantom Photon
  • 768
  • 2
  • 10
  • 20
  • This solves the problem. I had the lubridate loaded, but I did not call it in the code chunk. Adding library("lubridate") to the beginning of the chunk fixed it. – jt.wingate Nov 10 '15 at 21:44
  • `lubridate` was loaded only in your interactive session. R Studio opens a new, separate R session each time you compile an rmarkdown document, so you need to load any necessary packages within the rmarkdown document. – eipi10 Nov 10 '15 at 23:41