I have a shiny application that shows a nice html table of my data, using renderDataTable
. Then I wanted to make some basic statistics, subset data and calculate means and some other data.
When showing results with renderTable
, I found that date column was not shown in date format. In the figure ou can see the difference. Both tables are generated from the same dataset in the same shiny web app. Can you explain what's happening?
And here you can see ui.R and server.R. In this script I just want to show a table and was surprised by the different output.
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"))
)
)
))
and 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)
}
)
})
Thanks in advance for your help
There is no reason to have the same output from 2 different functions.
rendertable
uses xtable
to create the html tablerenderDataTable
uses external javascript library You can get the same date format, you should just format your date before calling renderTable
. Here a short complete example.
library(shiny)
dat <- data.frame(date=seq.Date(Sys.Date(),by=1,length.out=5),
temp = runif(5))
ui <- fluidPage(
fluidRow(column(4,dataTableOutput('dto')),
column(4,tableOutput('to')))
)
server <- function(input,output){
output$dto <- renderDataTable({dat})
dat$date <- format(dat$date,'%Y-%m-%d')
output$to <- renderTable(dat)
}
runApp(list(ui=ui,server=server))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With