内联 R returns 0 而不是正确的值?

inline R returns 0 instead of correct value?

内联代码只需要一个已经存在的变量。 R 代码块 returns 正确值,但内联代码 returns 0.

我的代码:

---
title: "Test"
output: html_document
---

```{r include=FALSE, echo=FALSE, message=FALSE, warning=FALSE}
library(tidyverse)
library(lubridate)
first_date <- ymd("2019-01-28")
last_date <- ymd("2020-03-12")
last_date - first_date

my_interval <- interval(first_date, last_date)
number_of_days <- as.period(my_interval, unit = "day")
number_of_days

without lubridate, the time difference is `r last_date - first_date` days and with lubridate it's `r number_of_days` days

我的输出:

大概转换为整数:

> number_of_days
[1] "409d 0H 0M 0S"
> as.integer(number_of_days)
[1] 0

试试这个:


 without lubridate, the time difference is `r last_date - first_date` days and with lubridate it's `r as.character(number_of_days)` days