Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knitr behavior with date objects

Tags:

date

r

knitr

Say I have the following object inside a knitr chunk:

df <- as.Date(c("2013-10-01", "2013-10-02", "2013-10-03"))

Now, when I try to run this:

`r min(df)`  # that would be \Sexpr{min(df)} for a TeX file

I get the following error:

Error in Math.Date(x) : abs not defined for "Date" objects
Calls: knit ... .inline.hook -> format_sci -> vapply -> FUN -> Math.Date

Execution halted

knitr terminated with status 1

Is this a bug? How can I work around it?

like image 246
Waldir Leoncio Avatar asked Oct 03 '13 18:10

Waldir Leoncio


1 Answers

I don't know if this will qualify as an answer, but yes, that does seem to be a bug--I wonder what other classes are affected!

A simple but possibly inconvenient workaround would be to convert your output to "character" first. In other words, try something like:

```{r}
df <- as.Date(c("2013-10-01", "2013-10-02", "2013-10-03"))
```

Here's a paragraph with `r as.character(min(df))`.
like image 180
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 14 '22 08:10

A5C1D2H2I1M1N2O1R2T1