I get hourly interval reports on equipment in buildings, a lot of buildings and a lot of equipment. Each parameter on the equipment is called a point and they already have a name, I don't get to choose the name of the point. Each point name is unique. What I'm trying to do is run a standard report on each building. Eventually, I'd like to move this to Shiny and look at my graphs and maybe print a report from there, but... baby steps.


Am I on the right track? Is there a more efficient way of doing this? Am I going to run into problems when I start to write Markdown reports or transfer this over to Shiny?

Sample Code


test_func <- function(pointa, pointb, mult) {
  out = (pointb - pointa) * mult

test_fail <- function(pointa, pointb) {
  out = ifelse(pointa > (pointb - 9), 1, 0)

tbl.data <- data.frame(
  date = c(rep("2/1/2018", 24),
           rep("2/2/2018", 24),
           rep("2/3/2018", 24),
           rep("2/4/2018", 24),
           rep("2/5/2018", 24),
           rep("2/6/2018", 24),
           rep("2/7/2018", 24)),
  hour = rep(0:23, 7),
  equipa.vala = runif(168, min = 50, max = 60),
  equipb.vala = runif(168, min = 50, max = 60)
  ) %>%
    equipa.valb = 10 + equipa.vala * runif(168, min = 0.75, max = 1.25),
    equipb.valb = 10 + equipb.vala * runif(168, min = 0.75, max = 1.25)

tbl.equip <- data.frame(
  equipment.id = c(1,2),
  equipment.name = c("equipa", "equipb"),
  equipment.mult = c(5, 7)

tbl.point <- data.frame(
  point = c("equipa.vala", "equipa.valb", "equipb.vala", "equipb.valb"),
  equipment = c("equipa", "equipa", "equipb", "equipb"),
  category = c("vala", "valb", "vala", "valb")

for (eq in tbl.equip[,2]) {

  vala <- as.character(
    tbl.point$point[tbl.point$equipment == eq &
                            tbl.point$category == "vala"]
  valb <- as.character(
    tbl.point$point[tbl.point$equipment == eq &
                            tbl.point$category == "valb"]

  equip.mult <- as.numeric(
    tbl.equip$equipment.mult[tbl.equip$equipment.name == eq]

  for.data <- tbl.data %>%
    select_(cola = vala,
            colb = valb) %>%
      result = test_func(cola, colb, equip.mult),
      fault = test_fail(cola, colb)

  score <- sum(for.data$fault)/length(for.data$fault)
  savings <- sum(for.data$result[for.data$result > 0])

  p1 <- ggplot(for.data, aes(x = colb, y = cola, color = as.factor(fault))) +
    geom_point() +
    annotate("text", label = paste("savings is:", savings), x = 50, y = 60) +
    annotate("text", label = paste("score is:", score), y = 51, x = 80) +
    ggtitle(paste("Equipment:", eq)) +


So in this sample, the tbl.data data frame would be the data I receive from each building. I'd have to manually create the tbl.equipment and tbl.point data frames, which I'd just house in *.csv files on my machine, or database (and be able to add/edit in Shiny). There's no standard for point names and there's not a guarantee that each piece of equipment has each point. Using select() helpers such as contains() or starts_with() is out of the question.

So I just created an Equipment table, which has parameters on the equipment, (in this case a multiple). Also, there's a Point table, which tells which piece of equipment and which category each point belongs to.

For this simple example, there's two sample functions I included. One calculates a value based on the the data, the other tests for a fault. My biggest problem in the past has been when a piece of equipment doesn't have a point, it stops the execution, so I have to manually go in and take it out or something else. I guess the workaround is to use exists() or something similar and test before running that piece of code.

Again, for this simple example, I just printed a plot, but the output could be a Markdown Document (which I think I've done before, but not like this) or Shiny (which I've created some simpler Apps).


The big question is "Is this the "right" way of doing it?" I'm sure this is pretty common and there has to be a really efficient method I'm not using. What's going to set me up for success when I start writing code to print out reports or taking this into a Shiny App?

Your Answer


Browse other questions tagged or ask your own question.