I have a table of data which updates every minute. I contains energy monitoring data…
- User
I have a table of data which updates every minute. I contains energy monitoring data from Smart Plugs around my home. Here is a sample of the table:
r$>knitr::kable(head(data))
today_runtime month_runtime today_energy month_energy local_time electricity_charge current_power name 460 2086 101 2019 2022-12-19 07:40:02 [0, 0, 685] 20.869 TV 460 2456 12 485 2022-12-19 07:40:02 [0, 0, 164] 37.789 Server 460 2256 277 3213 2022-12-19 07:40:03 [0, 0, 835] 135.370 Diana 460 1454 310 2873 2022-12-19 07:40:03 [0, 0, 976] 98.497 Jordan 461 2087 102 2020 2022-12-19 07:41:01 [0, 0, 685] 20.910 TV 461 2457 13 486 2022-12-19 07:41:01 [0, 0, 164] 16.862 Server I currently have an R script which displays a single plot from this data:
library(tidyverse) library(ggplot2) library(readxl) library(ggprism)
Read in the p110 energy csvs from ./out/
- Assistant
To create a Shiny app that will display the plot of power consumption over time, you will need to install and load the
shinylibrary in R. You will also need to create a user interface (UI) and a server function in R.Here is some sample code for creating a Shiny app that displays the plot:
code - User
Let's make the Shiny app more interesting. I would like to be able to choose which lines (TV, Server, Diana, Jordan) are shown on the plot via a checkbox. I'd like another line to be added to the plot (also optional via checkbox) which represents the sum of all the other lines.
- Assistant
To allow the user to select which lines are shown on the plot via checkboxes in the Shiny app, you can use
checkboxGroupInputin the UI andreactivein the server function.Here is some sample code for creating a Shiny app that allows the user to select which lines are shown on the plot and displays a sum of all the selected lines:
- User
My data are updated in real time every minute. Is there a way to alter this code such that,, when the app is being viewed, it would keep the plot up to date in real time without having to reload the app? If so, you don't need to write out the full implementation, just some snippets.
- Assistant
Yes, you can use the
invalidateLaterfunction in Shiny to update the plot in real time without having to reload the app.To do this, you will need to wrap the code that creates the plot in a reactive expression, and then use the
invalidateLaterfunction to specify how often the reactive expression should be invalidated and re-evaluated.