gpt4 book ai didi

r - 使用 Shiny renderTable 显示 POSIXt 对象

转载 作者:行者123 更新时间:2023-12-02 07:35:15 25 4
gpt4 key购买 nike

我正在制作一个应用程序来使用 Shiny 分析时间序列数据。我使用的数据如下所示:

                   V1     V2
1 2013-02-04 18:15:00 -4.746
2 2013-02-04 18:20:00 -4.745
3 2013-02-04 18:25:00 -4.746
4 2013-02-04 18:30:00 -4.747
5 2013-02-04 18:35:00 -4.747
6 2013-02-04 18:40:00 -4.747

我想在表格中绘制数据:

output$view <- renderTable({
head(datasubset(),
n=nrow(datasubset()))
})

这样做时,我在运行 Shiny 时遇到错误:

Error in Math.POSIXt(x + ifelse(x == 0, 1, 0)) : 
'abs' not defined for "POSIXt" objects

有人能解决这个错误吗?

更新:错误是由xtable引起的:renderTable 使用 xtable() 生成输出,看起来 xtable 一般不能很好地处理日期。

Winston Chang 已在此处提交了一个问题:https://github.com/rstudio/shiny/issues/129

解决方法可在:R: xtable and dates

最佳答案

查看基础包中的 strftime 函数。 Strftime 将 POSIXt 对象格式化为字符,并允许您指定格式。

在打印表格之前,您可以执行以下操作:
datasubset$V1 <- strftime(datasubset$V1, format="%Y-%m-%d %H:%M:%S")

关于r - 使用 Shiny renderTable 显示 POSIXt 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15500451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com