Closed ThomasKnecht closed 3 years ago
I would prefer to only use the sheetvar as sheetname. I have a case now, where the sheetname gets too long if the sheetvar column name as well as the value is used as sheetname. I
function (data, file, sheetvar, ...) { data <- as.data.frame(data) col_name <- rlang::enquo(sheetvar) wb <- openxlsx::createWorkbook(file) sheetvalues <- unique(data[, c(deparse(substitute(sheetvar)))]) for (sheetvalue in sheetvalues) { insert_worksheet(as.data.frame(data %>% dplyr::filter((!!col_name) == sheetvalue) %>% ungroup()), wb, sheetname = paste(deparse(substitute(sheetvar)), sheetvalue, sep = ","), ...) } openxlsx::worksheetOrder(wb) <- rev(openxlsx::worksheetOrder(wb)) openxlsx::saveWorkbook(wb, paste(file, ".xlsx", sep = ""), overwrite = TRUE) }
i would suggest the following solution:
function (data, file, sheetvar, ...) { data <- as.data.frame(data) col_name <- rlang::enquo(sheetvar) wb <- openxlsx::createWorkbook(file) sheetvalues <- unique(data[, c(deparse(substitute(sheetvar)))]) for (sheetvalue in sheetvalues) { insert_worksheet(as.data.frame(data %>% dplyr::filter((!!col_name) == sheetvalue) %>% ungroup()), wb, sheetname = sheetvalue, ...) } openxlsx::worksheetOrder(wb) <- rev(openxlsx::worksheetOrder(wb)) openxlsx::saveWorkbook(wb, paste(file, ".xlsx", sep = ""), overwrite = TRUE) }
Thanks @ThomasKnecht! Your suggestion is now implemented.
I would prefer to only use the sheetvar as sheetname. I have a case now, where the sheetname gets too long if the sheetvar column name as well as the value is used as sheetname. I
function (data, file, sheetvar, ...) { data <- as.data.frame(data) col_name <- rlang::enquo(sheetvar) wb <- openxlsx::createWorkbook(file) sheetvalues <- unique(data[, c(deparse(substitute(sheetvar)))]) for (sheetvalue in sheetvalues) { insert_worksheet(as.data.frame(data %>% dplyr::filter((!!col_name) == sheetvalue) %>% ungroup()), wb, sheetname = paste(deparse(substitute(sheetvar)), sheetvalue, sep = ","), ...) } openxlsx::worksheetOrder(wb) <- rev(openxlsx::worksheetOrder(wb)) openxlsx::saveWorkbook(wb, paste(file, ".xlsx", sep = ""), overwrite = TRUE) }
i would suggest the following solution:
function (data, file, sheetvar, ...) { data <- as.data.frame(data) col_name <- rlang::enquo(sheetvar) wb <- openxlsx::createWorkbook(file) sheetvalues <- unique(data[, c(deparse(substitute(sheetvar)))]) for (sheetvalue in sheetvalues) { insert_worksheet(as.data.frame(data %>% dplyr::filter((!!col_name) == sheetvalue) %>% ungroup()), wb, sheetname = sheetvalue, ...) } openxlsx::worksheetOrder(wb) <- rev(openxlsx::worksheetOrder(wb)) openxlsx::saveWorkbook(wb, paste(file, ".xlsx", sep = ""), overwrite = TRUE) }