0

I have read up on this in the article "expss - Tables with labels in R". Using the example supplied I can export 1 table to Excel. But I want to export many tables, all run at the same time. Can someone give me some pointers on how to export many tables at the same time. Regards

MarkWebb
  • 59
  • 4

1 Answers1

0

The simplest way is to put all your tables in the list. Then you can export this list to Excel. Example:

library(expss)
library(openxlsx)
data(mtcars)
mtcars = apply_labels(mtcars,
                      mpg = "Miles/(US) gallon",
                      cyl = "Number of cylinders",
                      disp = "Displacement (cu.in.)",
                      hp = "Gross horsepower",
                      drat = "Rear axle ratio",
                      wt = "Weight (lb/1000)",
                      qsec = "1/4 mile time",
                      vs = "Engine",
                      vs = c("V-engine" = 0,
                             "Straight engine" = 1),
                      am = "Transmission",
                      am = c("Automatic" = 0,
                             "Manual"=1),
                      gear = "Number of forward gears",
                      carb = "Number of carburetors"
)

banner = calc(mtcars, list(total(), am, vs))

library(comprehenr) # for 'to_list' function

list_of_tables = to_list(
    for(variable in mtcars){
        if(length(unique(variable))<7){
            cro_cpct(variable, banner) %>% significance_cpct()
        } else {
            # if number of unique values greater than seven we calculate mean
            cro_mean_sd_n(variable, banner) %>% significance_means()

        }    
    }
)

wb = createWorkbook()
sh = addWorksheet(wb, "Tables")

xl_write(list_of_tables, wb, sh)

saveWorkbook(wb, "report.xlsx", overwrite = TRUE)

If your tables are very different and you can't calculate them in the loop then you can put them in the list one by one, e. g.:

list_of_tables[[1]] = tab1

...

list_of_tables[[10]] = tab10

UPDATE. Function which additionally copies printed table to list with the name 'output'.

new_output = function(){
    output <<- list()
    print.etable <<- function(x, ...){
        output[[length(output) + 1]] <<- x
        expss:::print.etable(x, ...)
    }
    print.with_caption <<- function(x, ...){
        output[[length(output) + 1]] <<- x
        expss:::print.with_caption(x, ...)
    }
}

stop_output = function(){
    rm(print.etable, envir = .GlobalEnv)
    rm(print.with_caption, envir = .GlobalEnv)
}


new_output() # create empty list for output
banner = calc(mtcars, list(total(), am, vs))

# printed tables also will be added to 'output'
cro_cpct(mtcars$gear, banner)
cro_cpct(mtcars$carb, banner)

stop_output() # stop adding to list

wb = createWorkbook()
sh = addWorksheet(wb, "Tables")

xl_write(output, wb, sh)

saveWorkbook(wb, "report.xlsx", overwrite = TRUE)
Gregory Demin
  • 4,596
  • 2
  • 20
  • 20
  • My tables are just lines of code in RStudio RScript window. When run they are printed either in the console or the viewer window. They don't have names like tab1. How do I name tabs so that I can reference them for the list? Sorry - I am new to this utilisation of R. Keep well. – MarkWebb Jun 06 '19 at 09:35
  • @MarkWebb It is quite simple: just replace `tab1`, ... `tab...` with your lines of code. Another way (see update) is modification of the print method. With this modification tables when printed will be added to list. – Gregory Demin Jun 06 '19 at 12:07
  • Many thanks Gregory. Works well and will be very useful for what I do. Thanks again for your great package & assistance. Mark – MarkWebb Jun 06 '19 at 13:35
  • @MarkWebb New update fixes output of tables with caption. In the previous version captioned tables were skipped - https://stackoverflow.com/questions/56489768/expss-r-set-caption-not-outputting-to-excel. – Gregory Demin Jun 07 '19 at 08:09
  • Thanks again - perfect! – MarkWebb Jun 07 '19 at 08:59