0

I have a list of data frames which looks like following:

$Profession1

Searchterm         Product            Quantity
internist          Der Internist      3
pneumo news        Pneumo News        1
der urologe        Der Urologe        5    


$Profession2

Searchterm         Product            Quantity
der nervenarzt     Der Internist      7
der kardiologe     Der Kardiologe     2
.piefel            Strahlentherapie   6
therapiedes zenker Pathophysiologie   1

.  
.  
.  
.  
.  
.  

$Profession(n)

Searchterm         Product            Quantity
tuberkolose        Tuberkolose        2
und notfallmedizin Notfall            3
rechtsmedizin      Rechtsmedizin      9

I want to generate a pdf file where each data frame gets printed on a new page and also the data frame's name should be included as header in it (eg of header with reference to my example is Profession1, Profession2 etc.). I am able to implement the first part using the following code, but don't know how to include the header.

require(gridExtra)
pdf(file = "myfile(f8).pdf", height = 12, width = 26)
total_rows_per_page = 38 
start_row = 1 

for (j in f8) {
  if(total_rows_per_page > nrow(j)){
      end_row = nrow(j)
  }else {
      end_row = total_rows_per_page 
  }

  for(i in 1:ceiling(nrow(j)/total_rows_per_page)){

    grid.newpage()   

    grid.table(j[start_row:end_row, ], rows = NULL)

    start_row = end_row + 1

    if((total_rows_per_page + end_row) < nrow(j)){

      end_row = total_rows_per_page + end_row

    }else {

      end_row = nrow(j)
    }    
  }
}

dev.off()

where f8 is my list.

Can someone please help me with a non Rmarkdown solution? Thanks.

Sanisa
  • 3
  • 4
  • Could you please share the way you create the f8 list? – Ika8 Apr 10 '18 at 06:31
  • I had a raw csv file having columns Profession, Searchterm, Product pages (pages clicked which are part of search results), Qunatity of clicks. Then I pre processed the data using grep and sub command in R itself. Then using the dplyr package, I grouped the data by Profession and splitted the Professions using the split command. That's when I get a list of data frames (f8) – Sanisa Apr 10 '18 at 07:07
  • Does this explain it? – Sanisa Apr 10 '18 at 07:07
  • Yes, it does! Check my answer – Ika8 Apr 10 '18 at 07:41

1 Answers1

0

You can use names() function:

Like this example:

> df <- iris
> head(df)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
> df_splited <- split(df, df$Species)
> names(df_splited)
[1] "setosa"     "versicolor" "virginica" 
> 

When you iterate over the f8 list you can get the name with names(f8)[j]

Ika8
  • 391
  • 1
  • 12