Home > OS >  inline R returns 0 instead of correct value?
inline R returns 0 instead of correct value?

Time:03-25

The inline code simply takes an already existing variable. The R code chunk returns the correct value, but the inline code returns 0.

My code:

---
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

My output:

enter image description here

CodePudding user response:

It is probably converted to integer:

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

try this instead:


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