Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

inline R returns 0 instead of correct value?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading