【发布时间】:2014-02-28 04:58:27
【问题描述】:
我有一个闪亮的应用程序,它使用renderDataTable 显示我的数据的漂亮 html 表。然后我想做一些基本的统计数据,子集数据和计算方法以及其他一些数据。
使用renderTable 显示结果时,我发现日期列未以日期格式显示。从图中可以看出区别。这两个表都是从同一个闪亮 Web 应用程序中的同一个数据集生成的。你能解释一下发生了什么吗?
在这里你可以看到 ui.R 和 server.R。在这个脚本中,我只想显示一个表格,并且对不同的输出感到惊讶。
ui.R
library(shiny)
# Estructura de la página (paneles)
shinyUI(pageWithSidebar(
# Título superior
headerPanel(""),
# Panel lateral izquierdo - selección de datos
sidebarPanel(
helpText("Selecciona las fechas y el tipo de datos.
Pulsa el botón Actualizar."),
selectInput("torre", "Torre:",
list("Agres" = "mariola",
"Alfàs del Pi" = "shelada",
"Altura" = "altura",
"Vistabella del Maestrat" = "vistabella",
"Xàtiva" = "xativa")),
selectInput("tipo", "Intervalo de datos",
list("Diezminutales" = "-datos-10m.csv",
"Diarios" = "-datos-diarios.csv",
"Mensuales" = "-datos-mensuales.csv")),
dateInput('date1',
label = 'Fecha inicial',
value = Sys.Date()),
dateInput('date2',
label = 'Fecha final.',
value = Sys.Date()),
submitButton("Actualizar"),
helpText("
Descarga de datos tabulados en formato CSV."),
downloadButton('downloadData','Descargar datos')
),
# Panel principal (presentación de gráficas)
mainPanel(
tabsetPanel(
tabPanel("Inicio",
h3("Consulta de datos"),
p(HTML("Some info.")),
tableOutput("view")
),
tabPanel("Ayuda", htmlOutput("ayuda"),id="ayuda"),
tabPanel('Tabla de datos', dataTableOutput("table1")),
tabPanel('Medias', tableOutput("table2"))
)
)
))
和server.R
library(shiny)
library(plyr)
library(lubridate)
library(scales)
options(shiny.transcode.json = FALSE)
# Funciones
shinyServer(function(input,output){
filename=reactive({
paste0(input$torre,input$tipo)
})
day1=reactive({
as.POSIXct(input$date1)
})
day2=reactive({
as.POSIXct(input$date2)
})
# Ayuda
introFile <- './ayuda.txt'
ayuda <- readChar(introFile, file.info(introFile)$size)
output$ayuda <- renderText({HTML(ayuda)})
datos2=reactive({
fn = filename()
f = read.csv(fn,header=T, sep=",",na.strings="-99.9")
f$date = as.Date(f$date)
f
})
datos=reactive({
d1 <- as.Date(day1())
d2 <- as.Date(day2())
datos2a = datos2()
with( datos2a , datos2a[ date >= d1 & date <= d2, ] )
})
# Tabla de datos
output$table1 = renderDataTable({
datos()
}, options = list(aLengthMenu = c(12, 20, 40), iDisplayLength = 12))
output$table2 = renderTable({
datos()
})
output$downloadData <- downloadHandler(
file = c('data.csv'),
content = function(file) {
write.csv(datos(), file)
}
)
})
提前感谢您的帮助
【问题讨论】:
-
为什么你期望
renderDataTable和renderTable有相同的输出?它们完全不同。然后,您应该能够通过一个闪亮的小示例重现相同的行为(无需拥有所有其他 UI 组件/服务器功能)。你有很长的嘈杂不可重现的例子。 -
@agstudy 我希望得到相同的输出,因为我认为它们只是显示数据的方式,以前不知道我必须格式化数据。我假设
renderTable知道日期是某种日期格式,因为f$date = as.Date(f$date)感谢您的回答。 -
@agstudy 下次我会尽我最大的努力来处理可重现的示例。谢谢。
-
请注意,renderTable 不能很好地处理日期。您需要将它们转换为字符。参见例如discussion
-
是的@Vincent 这就是重点。在这种情况下,我将在玩日期时使用
renderDataTable。谢谢
标签: r shiny shiny-server