Rmarkdown Aggregate error

Asked

Viewed 446 times

1

Hello, I’m trying to create reports by Rmarkdown, with the method read_chunk() it is possible to plot in doc using script externalization.R. Doing this integration, with only 1 script worked, when I went to put the second, there was an error in the function aggragate, he claims he no longer has rows to record. I will detail the steps I take until the error:

1°: Scripts in . R

Each of the two scripts contains a Plot, they are geom_bar of library ggplot2. To make this graph, X is usually placed as a factor and pull the other information in a row direction as an array. To do this, I used the Aggregate function in the two scripts indicated in an old question I did here in the stack.

Doubts ggplot in bars

Piece of draft script N. R

dado <- data.frame("hora" = tab.genova$V1, "Mud" = tab.genova$V2, "Conf" = 
tab.genova$V3, "Alt" = tab.genova$V4, "DEM" = tab.genova$V4, "Cy" = tab.genova$V5)
agg <- aggregate(cbind(DEM, Alt, Conf, Mud, Cy) ~ hora, dado, sum)
molten <-melt(agg, id = "hora")

Piece of draft script. R

data2 <- data.frame("dia" = tab.Roma$Dia,"ResolvidosFSR" = 
tab.Roma$Resolvidos_no_FSR, "Abertos" = tab.Roma$Abertos)
agg2 <- aggregate(cbind(tab.Roma$Resolvidos_no_FSR, tab.Roma$Abertos) ~ dia, data, sum)
molten2 <-melt(agg2, id = "dia2")

2°: Code of . Rmd

```{r LeituraDeDados, echo=FALSE, results='hide', warning=FALSE, 
message=FALSE}
library(lubridate)
library(ggplot2)
library(gtable)
library(grid)
library(extrafont)
library(data.table)
library(scales)
library(gridExtra)
library(tidyr)
library(reshape2)
library(knitr)
loadfonts(device="win")
# dê o set na pasta que está seus arquivos script.R, no meu caso tb os csv que eu usava.
setwd("C:/Users/admin/Desktop/testeMark")

# Esses read.csv eu uso aqui no meu pq tiro os dados no arquivo, coloquei aqui para ilustrar 
#que estou usando por este metódo e aqui no stack farei por dput (deve ser a mesma coisa).
#tab.genova <- read.csv(file="tab.genova.csv", sep=";")
#tab.Roma <- read.csv(file="tab.Roma.csv", sep=";")

```

## Plot 1

```{r echo=FALSE}
read_chunk('rascunhoN.R')
#Esse chunk está lendo e importando o script 'rascunhoN.R' com o primeiro plot
```

```{r rascunhoNplot.R , code=readLines("rascunhoN.R"), echo=FALSE, 
fig.align='right', fig.height=7.7522, fig.width= 11.816666666666666}
#Esse chunk está lendo o script importado e plotando.
```

## plot2

```{r echo=FALSE}
read_chunk('rascunho.R')
#Esse chunk está lendo e importando o script 'rascunho.R' com o segundo plot
```

```{r rascunho.R , code=readLines("rascunho.R"), echo=FALSE, fig.align= 
'center', fig.height=7.7522, fig.width= 11.816666666666666}
 #Esse chunk está lendo o script importado e plotando.
```

3°: ERROR

After giving Ctrl+shift+k

has the file build in output . doc, which generates this error:

 Quitting from lines 88-89 (teste.Rmd) 
 Error in aggregate.data.frame(lhs, mf[-1L], FUN = FUN, ...) : no rows to aggregate
 Calls: <Anonymous> ... aggregate -> aggregate.formula -> aggregate.data.frame

I will leave the data and codes of the scripts:

draft Plot. r

## @knitr rascunhoN.R


dados <- #...
agg <- aggregate(cbind(DEM, Alt, Conf, Mud, Cy) ~ hora, dado, sum)
molten <-melt(agg, id = "hora")

## @knitr rascunhoNplot.R
q1 <- ggplot(molten, aes(x = hora, y = value, fill = variable))+
  geom_bar(position = "stack", stat = "identity")+
  labs(x= NULL, y= NULL)+
  theme(axis.text.x = element_text( hjust = 1, vjust = 0.3))+
  scale_y_continuous(expand = c(0, 0),  breaks = pretty_breaks(8), sec.axis = sec_axis(~.*1, breaks = pretty_breaks(8)))+
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.3))+
  scale_fill_manual( values = c("#8AC5FF","#73C5FF", "#73B0EE", "#5EA9C9", "#5BC5AC"))+
  theme(
     axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.3, size = 14),
     panel.background = element_blank(), 
     panel.grid.minor = element_blank(), 
     panel.grid.major = element_line(color = "gray50", size = 0.5), 
     panel.grid.major.x = element_blank(),
     text = element_text(family="Simplon BP Light"),
     axis.text.y = element_text(size = 18),
     axis.title = element_text(color = "gray50", size = 14, family = "Simplon BP Light"),
     axis.ticks = element_line(colour = 'gray50'),
     axis.ticks.length = unit(.25, "cm"),
     axis.ticks.x = element_line(colour = "gray50"),
     legend.position="bottom",
     legend.title = element_blank(),
     axis.ticks.y = element_blank(),
     legend.text = element_text(colour="gray50", size=22,  family = "Simplon BP Light" ),
     legend.box = "horizontal",
     plot.margin = margin(15, 15, 15, 15),
     plot.title = element_text(hjust = 0.5, color = "gray50", size = 30, face = "bold", family = "Simplon BP Light"))

  q1

   #Dados:
   # ...
    dados <- structure(list(hora = structure(1:24, .Label = c("00:00", "01:00", 
   "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", 
   "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", 
   "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", 
   "23:00"), class = "factor"), Mud = c(4L, 3L, 0L, 0L, 2L, 0L, 
   1L, 1L, 2L, 2L, 1L, 2L, 0L, 1L, 1L, 1L, 6L, 5L, 0L, 0L, 2L, 2L, 
   0L, 3L), Conf = c(1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 5L, 4L, 
   3L, 1L, 4L, 4L, 5L, 3L, 2L, 1L, 1L, 5L, 0L, 3L, 0L), AltSev = c(0L, 
   0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 1L, 3L, 0L, 0L, 0L, 0L, 1L, 
   0L, 0L, 1L, 0L, 0L, 0L, 2L), DEM = c(0L, 1L, 0L, 0L, 0L, 
   0L, 1L, 0L, 4L, 2L, 1L, 2L, 1L, 2L, 4L, 3L, 2L, 2L, 1L, 0L, 1L, 
   0L, 1L, 0L), Cyber = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
   2L, 1L, 0L, 0L, 0L, 0L, 1L, 5L, 1L, 0L, 0L, 1L, 0L, 1L)), .Names = 
   c("hora", "Mud", "Conf", "Alt", "DEM", "Cy"), row.names = c(NA, 
   -24L), class = "data.frame")

draft. R

   ## @knitr rascunho.R


   data2 <- #...
   agg2 <- aggregate(cbind(tab.Roma$Resolvidos_no_X, tab.Roma$Abertos) ~ 
   dia, data, sum)
   molten2 <-melt(agg2, id = "dia")


   q1 <- ggplot(molten2, aes(x = dia, y = value, fill = variable))+
   geom_bar(position = "dodge", stat = "identity")+
   labs(x= NULL, y= NULL)+
   theme(axis.text.x = element_text( hjust = 1, vjust = 0.3))+
   scale_y_continuous(expand = c(0, 0), limits = c(-0, 3000), breaks = 
   pretty_breaks(8))+
   scale_x_date(breaks=seq(min(tab.Roma$Dia), max(tab.Roma$Dia), by="1 day"),  date_labels="%d/%b", minor_breaks=seq(min(tab.Roma$Dia), max(tab.Roma$Dia), by="1 month"))+
   theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.3))+
   scale_fill_manual(labels = c("Resolvidos no X  ","Abertos  "), values = c("#00B0CC","#CC5200"))+
   theme(
       axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.3),
       panel.background = element_blank(), 
       panel.grid.minor = element_blank(), 
       panel.grid.major = element_line(color = "gray50", size = 0.5), 
       panel.grid.major.x = element_blank(),
text = element_text(family="Simplon BP Light"),
       axis.text.y = element_text(size = 14),
       axis.title = element_text(color = "gray50", size = 14, family = "Simplon BP Light"),
       axis.ticks = element_line(colour = 'gray50'),
       axis.ticks.length = unit(.25, "cm"),
       axis.ticks.x = element_line(colour = "gray50"),
       legend.position="bottom",
       legend.title = element_blank(),
       axis.ticks.y = element_blank(),
       legend.text = element_text(colour="gray50", size=10,  family = "Simplon BP Light" ),
       legend.box = "horizontal",
       plot.margin = margin(15, 15, 15, 15),
       plot.title = element_text(hjust = 0.5, color = "gray50", size = 14, face = "bold", family = "Simplon BP Light"))

   q2 <- ggplot(tab.Roma, aes(x = Dia))+
   geom_line(aes(y = Meta.X, colour = "Meta.X"), linetype = 4, size = 1.5)+
   geom_line(aes(y = X._Resolvidos, colour = "X._Resolvidos"), linetype= 1, size = 1.5)+
   geom_line(aes(y = X._Resolvidos_no_X, colour = "X._Resolvidos_no_X"), linetype = 1, size = 1.5)+
   labs(x= NULL, y= NULL)+
   theme(axis.text.x = element_text( hjust = 1, vjust = 0.3))+
   scale_y_continuous(expand = c(0, 0), limits = c(-0, 1), breaks = pretty_breaks(8), labels = scales::percent)+
   scale_x_date(breaks=seq(min(tab.Roma$Dia), max(tab.Roma$Dia), by="1 day"),  date_labels="%d/%b", minor_breaks=seq(min(tab.Roma$Dia), max(tab.Roma$Dia), by="1 month"))+
   theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.3))+
   scale_colour_manual(name = "", values = c("X._Resolvidos" = "#FFBB00", "X._Resolvidos_no_X" = "#484848", "Meta.X" = "#009600"), breaks=c("X._Resolvidos", "X._Resolvidos_no_X", "Meta.X"), labels=c("Abertos  \n    ( % ) ", "Resolvidos no X  \n            ( % )", "Meta X\n   ( 80% )"))+
   theme(
        panel.border = element_blank(),
        panel.background = element_rect(fill = "transparent"),
        panel.grid.minor = element_blank(), 
        panel.grid.major = element_blank(),
        axis.title = element_text(color = "gray50", size = 14, family = "Simplon BP Light"),
        text = element_text(family="Simplon BP Light"),
        axis.text.y = element_text(size=14),
        axis.text.x = element_text(size = 14),
        axis.ticks = element_line(colour = 'gray50'),
        axis.ticks.length = unit(.25, "cm"),
        axis.ticks.x = element_line(colour = "gray50"),
        legend.text = element_text(colour="gray50", size=10,  family = "Simplon BP Light" ),
        legend.position="bottom",
        legend.title = element_blank(),
        legend.box = "horizontal",
        axis.ticks.y = element_blank())

      g1 <- ggplot_gtable(ggplot_build(q1)) 
      g2 <- ggplot_gtable(ggplot_build(q2)) 
      pp <- c(subset(g1$layout, name == "panel", se = t:r)) 
      g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, pp$l, pp$b, pp$l) 
      ia <- which(g2$layout$name == "axis-l") 
      ga <- g2$grobs[[ia]]
      ax <- ga$children[[2]]
      ax$widths <- rev(ax$widths) 
      ax$grobs <- rev(ax$grobs)
      g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1) 
      g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)



      leg1 <- g1$grobs[[which(g1$layout$name == "guide-box")]]
      leg2 <- g2$grobs[[which(g2$layout$name == "guide-box")]]


      g$grobs[[which(g$layout$name == "guide-box")]] <-
      gtable:::cbind_gtable(leg1, leg2, "first")


      plot(g)
  #dados:
  #...
  data2  <- structure(list(Dia = structure(c(17563, 17564, 17565, 17566, 17567, 17568, 17569, 17570, 17571, 17572, 17573, 17574, 17575, 17576, 17577, 17578, 17579, 17580, 17581, 17582, 17583, 17584, 17585, 17586, 17587, 17588, 17589, 17590), class = "Date"), Meta.X = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "0,8", class = "factor"), 
X._Resolvidos = structure(c(5L, 9L, 1L, 3L, 10L, 11L, 9L, 
8L, 12L, 9L, 1L, 8L, 6L, 10L, 10L, 9L, 3L, 4L, 10L, 12L, 
11L, 11L, 10L, 7L, 12L, 13L, 13L, 2L), .Label = c("0,7", 
"0,76", "0,78", "0,79", "0,8", "0,81", "0,82", "0,83", "0,85", 
"0,86", "0,87", "0,88", "0,9"), class = "factor"), X._Resolvidos_no_X = structure(c(5L, 
3L, 1L, 5L, 9L, 6L, 6L, 5L, 4L, 10L, 7L, 4L, 8L, 5L, 8L, 
7L, 9L, 2L, 7L, 8L, 4L, 5L, 6L, 3L, 9L, 6L, 7L, 7L), .Label = c("0,87", 
"0,88", "0,89", "0,9", "0,91", "0,92", "0,93", "0,94", "0,95", 
"0,96"), class = "factor")), .Names = c("Dia", "Meta.X",  "X._Resolvidos", "X._Resolvidos_no_X"), row.names = c(NA, -28L), class = "data.frame") 
  • This is just the error message, can you edit the question with the minimum code that produced it? And the data, are the same as the other question of the link? (If not, use dput.)

  • @Noisy I’ll edit here and update. But it’s pretty much the same, I’ll say it one more time, this error occurs because it has two functions using Agg.

  • Today was the fourth time I’ve entered this question and I still can’t understand what she wants. Everything is very confusing for me and I believe for other people who access the site as well. I don’t know, for example, what should be plotted, because in some moments the data set is called tab.genova and in others of q1. I also don’t know how the data should be grouped. Is it by hour? I suggest to the OP to rewrite its doubt, removing what is unnecessary from it and providing a CMR with only the essential so that the question is understandable to those who wish to help.

  • I’ll rephrase, and do a step by step. It’s really quite confusing to explain around here, until I get it right.

  • 1

    I know it’s hard and everything, but I still can’t reproduce your code. For example, when I run the first part of it, given by dado <- data.frame("hora" = tab.genova$V1, "Mud" = tab.genova$V2, "Conf" = tab.genova$V3, "Alt" = tab.genova$V4, "DEM" = tab.genova$V4, "Cy" = tab.genova$V5), I get an error message, just because I don’t have this object tab.genova on my PC. Before submitting the question edit, try running the code in a new R instance, with no objects in memory..

  • I tried to open another instance, it was an error. I will correct this from obj tab.genova. It was a detail that I completely forgot, but the data related to tab.algumacoisa is in the dputs at the end of each code. Thank you for the force.

Show 1 more comment

1 answer

0


I broke my head too hard to find the problem, the problem was the aggregate same. Somehow Rmarkdown does not work with multiples aggregate in one instance, debugging the code verified that this function was not needed for the script’s function and that it only needed the function melt(). So I cut this function out of my code, I used the package magrittr to use %>% as forward-pipe.

 melted1 <- dados %>% melt(id.vars = "dia") 

A hint, the aggregate is very slow, when using a large mass of data will weigh heavily in the processing of R (which is no longer as efficient). I recommend that you use the packets dplyr and tidyr to circumvent, I saw test in a user stack post proved that the dplyris 20 times faster.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.