From b97073f540f460a26bb53d18a6de861f16f89b6c Mon Sep 17 00:00:00 2001 From: pvictor Date: Fri, 14 Feb 2020 17:33:24 +0100 Subject: [PATCH] example real time shiny --- examples/real-time.R | 96 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 examples/real-time.R diff --git a/examples/real-time.R b/examples/real-time.R new file mode 100644 index 0000000..761190b --- /dev/null +++ b/examples/real-time.R @@ -0,0 +1,96 @@ + +library(apexcharter) +library(shiny) + +ui <- fluidPage( + tags$h2("Real time charts with {apexcharter}"), + tags$hr(), + fluidRow( + column( + width = 4, + # verbatimTextOutput("res"), + apexchartOutput(outputId = "gauge") + ), + column( + width = 4, + apexchartOutput(outputId = "bar") + ), + column( + width = 4, + apexchartOutput(outputId = "line") + ) + ) +) + +server <- function(input, output, session) { + + rv <- reactiveValues(x = data.frame( + time = sort(Sys.Date() - 1:20), + value = round(rnorm(20, mean = 10)), + groups = sample(LETTERS, 20, TRUE), + hits = round(runif(20, 1, 100)), + stringsAsFactors = FALSE + )) + + r <- reactive({ + invalidateLater(1000) + rv$x <- rbind( + isolate(rv$x), + data.frame( + time = isolate(tail(rv$x$time, 1) + 1), + value = round(rnorm(1, mean = 10)), + groups = sample(LETTERS, 1, TRUE), + hits = round(runif(1, 1, 100)) + ) + ) + }) + + output$res <- renderPrint({ + tail(r(), 20) + }) + + output$line <- renderApexchart({ + apex(data = tail(r(), 20), mapping = aes(x = time, y = value), type = "spline") %>% + ax_chart(animations = list( + enabled = TRUE, easing = "linear", dynamicAnimation = list(enabled = FALSE) + )) %>% + ax_xaxis(range = 777600000) + }) + + output$bar <- renderApexchart({ + apex( + data = table(groups = r()$groups), + mapping = aes(x = groups, y = Freq), + type = "column" + ) + }) + + output$gauge <- renderApexchart({ + apex( + data = NULL, + type = "radialBar", + mapping = aes(x = "Last value", y = tail(r()$hits, 1)) + ) %>% + ax_plotOptions( + radialBar = radialBar_opts( + startAngle = -135, + endAngle = 135, + dataLabels = list( + name = list( + fontSize = "16px", + offsetY = 120 + ), + value = list( + offsetY = 76, + fontSize = "22px", + formatter = JS("function (val) {return val + ' hits';}") + ) + ) + ) + ) %>% + ax_stroke(dashArray = 4) + }) + +} + +shinyApp(ui, server)