Skip to content

Instantly share code, notes, and snippets.

@micstr
Created July 4, 2017 14:19
Show Gist options
  • Save micstr/a4cad0706b73b41971611ef8e244d2ba to your computer and use it in GitHub Desktop.
Save micstr/a4cad0706b73b41971611ef8e244d2ba to your computer and use it in GitHub Desktop.
Fix TZ showing in Shiny tables
# Fix TZ showing in tables : drawdown Shiny example
# micstr 4 July 2017
# shiny 0.12. broke the perfapp drawdown tables now showing dates with Timezone e.g.
# 2016-04-30T00:00:00Z
#"POSIXt objects are now serialized to JSON in UTC8601 format (like
#"2015-03-20T20:00:00Z"), instead of as seconds from the epoch. If you
#have a Shiny app which uses sendCustomMessage() to send datetime
#(POSIXt) objects, then you may need to modify your Javascript code to
#receive time data in this format."
library(PerformanceAnalytics)
library(shiny)
library(DT) # is order right?
# get frag of data
returns <- structure(c(NA, 0.002, 0.0001, -0.0026, 0.03, -0.008), class = c("xts", "zoo"),
.indexCLASS = c("POSIXct", "POSIXt"),
tclass = c("POSIXct","POSIXt"),
.indexTZ = "UTC", tzone = "UTC",
index = structure(c(1414800000,
1417305600,
1419984000,
1422662400,
1425081600, 1427760000),
tzone = "UTC",
tclass = c("POSIXct","POSIXt")),
.Dim = c(6L, 1L), .Dimnames = list(NULL, "nav"))
server <- function(input, output) {
output$drawdownTable <- DT::renderDataTable({
out <- table.Drawdowns(returns)
# This is how you fix TZ
out$From <- as.Date(out$From)
out$To <- as.Date(out$To)
out$Trough <- as.Date(out$Trough)
out}, options = k.dt.opt
)
}
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("obs", "ignore me:", min = 6, max = 20, value = 12)
),
mainPanel(DT::dataTableOutput("drawdownTable"))
)
)
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment