在RStudio版本1.0.143中,使用以下R Markdown,我得到一个没有正确呈现POSIXlt的HTML。
这是降价来源:
---
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)`
这是我用“Knit to HTML”命令得到的结果:
Timestamp is: 0, 0, 14, 4, 5, 117, 0, 154, 0
虽然我原以为:
Timestamp is: 2017-06-04 14:00:00 UTC
我的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
答案 0 :(得分:1)
作为一种解决方法,您可以在函数中使用as.character
:
get_timestamp <- function(filename)
{
dt <- substr(filename,3,14)
as.character(strptime(dt,"%y%m%d%H%M%S",tz="UTC"))
}
Markdown解析器必须使用对象的内部表示而不是输出格式。
> 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"