Adds rstudio

This commit is contained in:
2022-09-14 20:46:52 +02:00
parent 7969f4ee86
commit cd963090f2
3 changed files with 82 additions and 1 deletions

View File

@@ -27,7 +27,7 @@ services:
environment:
PASSWORD: secret
volumes:
- ./rstudio:/home/rstudio
- ./rstudio:/home/rstudio/smartmeter
ports:
- 8787:8787

View File

@@ -2,6 +2,8 @@ FROM rocker/tidyverse:latest
RUN apt-get update \
&& apt-get install -y libmariadb-dev \
libicu-dev liblzma-dev libpcre3-dev libpng-dev \
libv8-dev libbz2-dev libxml2-dev \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/ \
&& rm -rf /tmp/downloaded_packages/ /tmp/*.rds
@@ -10,6 +12,8 @@ RUN apt-get update \
RUN Rscript -e "install.packages(c('tidyverse','purr','psych','lme4','lmerTest','broom','doBy','reshape','emmeans','effects','mlr','randomForest','glmnet','foreign'), repos='https://cran.rstudio.com/')" \
&& rm -rf /tmp/downloaded_packages/ /tmp/*.rds
RUN mkdir /home/rstudio/smartmeter
VOLUME /home/rstudio/smartmeter
EXPOSE 8787

77
rstudio/Energy.Rmd Executable file
View File

@@ -0,0 +1,77 @@
library(dplyr)
library(dbplyr)
library(ggplot2)
library(lubridate)
library(tidyr)
# Access to our smartmeter DB
con <- DBI::dbConnect(RMariaDB::MariaDB(), host="db", dbname="smartmeter", user="root", password="rootme")
DBI::dbListTables(con)
# get some records
res <- DBI::dbSendQuery(con, "SELECT * FROM readings LIMIT 10")
DBI::dbFetch(res)
# dplyr style of reading data from mysql
# con %>% tbl("readings") %>% show_query()
# get all records from 2022
energy <- con %>% tbl("readings") %>%
select(total_m3_gas_consumed, total_kwh_consumed_high, total_kwh_consumed_low,
total_kwh_produced_high, total_kwh_produced_low, high_tarif, created_at) %>%
filter(created_at > "2021-09-01", created_at < "2021-12-31") %>%
mutate(date = as.Date(created_at)) %>%
collect()
# add hour column
energy <- energy %>% mutate(hour = format(strptime(created_at,"%Y-%m-%d %H:%M:%S"),'%H'))
# group by hour
energy_per_hour <- energy %>%
mutate(total_usage_kwh = (total_kwh_consumed_high+total_kwh_consumed_low)) %>%
mutate(total_return_kwh = (total_kwh_produced_high+total_kwh_produced_low)) %>%
group_by(date,hour) %>%
summarize(max_m3_gas_consumed = max(total_m3_gas_consumed),
min_m3_gas_consumed = min(total_m3_gas_consumed),
max_usage_kwh = max(total_usage_kwh),
min_usage_kwh = min(total_usage_kwh),
max_return_kwh = max(total_return_kwh),
min_return_kwh = min(total_return_kwh)) %>%
mutate(usage_m3 = max_m3_gas_consumed-min_m3_gas_consumed) %>%
mutate(usage_kwh = max_usage_kwh-min_usage_kwh) %>%
mutate(return_kwh = max_return_kwh-min_return_kwh) %>%
select(-max_m3_gas_consumed, -min_m3_gas_consumed, -max_usage_kwh, -min_usage_kwh, -max_return_kwh, -min_return_kwh )
# mutate(usage_kwh = round(usage_kwh,1), return_kwh = round(return_kwh,1)) %>%
# and again, group by day
energy_per_day <- energy_per_hour %>%
group_by(date) %>%
summarize(usage_m3=round(sum(usage_m3),2),
usage_kwh=round(sum(usage_kwh),2),
return_kwh=round(sum(return_kwh),2))
# some plots
energy_per_hour %>%
mutate(usage_kwh = round(usage_kwh,1), return_kwh = round(return_kwh,1)) %>%
ggplot( aes(x=date, fill=hour, y=usage_kwh, text=as.character(date))) +
geom_bar(stat="identity") +
theme_bw() +
labs(x="Date", y="kwh")
# daily usage/return of electricity
energy_per_day %>% pivot_longer(cols = usage_kwh:return_kwh) %>%
ggplot( aes(x=date, y=value, fill=name, text=as.character(date))) +
geom_bar(position="dodge", stat="identity") +
geom_text(aes(label=value), vjust=-0.3, hjust=1.2, size=2.5) +
theme_bw() +
labs(x="Date", y="kwh")
# daily usage/return of gas
energy_per_day %>% pivot_longer(cols = usage_m3) %>%
ggplot( aes(x=date, y=value, fill=name, text=as.character(date))) +
geom_bar(position="dodge", stat="identity") +
geom_text(aes(label=value), vjust=-0.3, hjust=1.2, size=2.5) +
theme_bw() +
labs(x="Date", y="m3")