0

In RStudio Version 1.0.143, with the following R Markdown, I get an HTML with the POSIXlt not properly rendered.

This is the markdown source:

---
title: "Untitled"
author: "Alessandro"
date: "05 giugno 2017"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
filename <- '61170604140000.txt'

get_timestamp <- function(filename)
{
  dt <- substr(filename,3,14)
  strptime(dt,"%y%m%d%H%M%S",tz="UTC")
}
```

## R Markdown

Timestamp is: `r get_timestamp(filename)`

And this is the result I get with "Knit to HTML" command:

Timestamp is: 0, 0, 14, 4, 5, 117, 0, 154, 0

While I would have expected:

Timestamp is: 2017-06-04 14:00:00 UTC

My sessionInfo:

> sessionInfo()
R version 3.4.0 (2017-04-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=Italian_Italy.1252  LC_CTYPE=Italian_Italy.1252    LC_MONETARY=Italian_Italy.1252
[4] LC_NUMERIC=C                   LC_TIME=Italian_Italy.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] compiler_3.4.0  backports_1.0.5 magrittr_1.5    rprojroot_1.2   htmltools_0.3.6 tools_3.4.0     yaml_2.1.14    
 [8] Rcpp_0.12.10    stringi_1.1.5   rmarkdown_1.5   knitr_1.15.1    stringr_1.2.0   digest_0.6.12   evaluate_0.10
Alessandro Jacopson
  • 18,047
  • 15
  • 98
  • 153

1 Answers1

1

As a work around, you can use as.character in your function:

get_timestamp <- function(filename)
{
  dt <- substr(filename,3,14)
  as.character(strptime(dt,"%y%m%d%H%M%S",tz="UTC"))
}

The Markdown parser must be using the internal representation of the object rather than the output format.

> str(strptime(substr(filename,3,14),"%y%m%d%H%M%S",tz="UTC"))
 POSIXlt[1:1], format: "2017-06-04 14:00:00"
> str(as.character(strptime(substr(filename,3,14),"%y%m%d%H%M%S",tz="UTC")))
 chr "2017-06-04 14:00:00"
Emer
  • 3,734
  • 2
  • 33
  • 47