-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from PetterHopp/main
OKplan v0.4.0 2021-12-09
- Loading branch information
Showing
15 changed files
with
427 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#' @title Style row with "Sum" in Excel sheet | ||
#' @description Style the font with bold for the line with the text "Sum" in on cell. | ||
#' It is possible to use other text decoration, see \code{openxlxs::createStyle}. | ||
#' A line with the text "Sum" or another text as given by text will be styled. | ||
#' | ||
#' @details The whole line will be styled. | ||
#' | ||
#' @param workbook The workbook object. | ||
#' @param sheet The Excel sheet name. | ||
#' @param data The data frame that have been exported to the Excel sheet. Used to | ||
#' find column number and row number for the pretext for which the row should be styled. | ||
#' @param text The text in the cell for which the row should be styled. | ||
#' Defaults to "Sum". | ||
#' @param text_decoration The text decoration style that should be used, see \code{openxlsx::createStyle}. | ||
#' Defaults to "bold". | ||
#' @param \dots Other arguments to be passed. | ||
#' | ||
#' | ||
#' @return None. One row in the workbook object is styled. | ||
#' | ||
#' @author Petter Hopp Petter.Hopp@@vetinst.no | ||
#' @export | ||
|
||
|
||
style_sum_line <- function(workbook = workbook, | ||
sheet = sheet, | ||
data, | ||
text = "Sum", | ||
text_decoration = "bold", | ||
...) { | ||
|
||
# ARGUMENT CHECKING ---- | ||
# Object to store check-results | ||
checks <- checkmate::makeAssertCollection() | ||
|
||
# Perform checks | ||
checkmate::assert_class(workbook, classes = "Workbook", add = checks) | ||
checkmate::assert_character(sheet, len = 1, min.chars = 1, add = checks) | ||
checkmate::assert_data_frame(data, add = checks) | ||
checkmate::assert_character(text, len = 1, add = checks) | ||
checkmate::assert_character(text_decoration, len = 1, add = checks) | ||
|
||
# Report check-results | ||
checkmate::reportAssertions(checks) | ||
|
||
# STYLING ---- | ||
# Style a row in the Excel sheet with the given text in a cell | ||
openxlsx::addStyle(wb = workbook, | ||
sheet = sheet, | ||
style = openxlsx::createStyle(textDecoration = text_decoration), | ||
cols = 1:dim(data)[2], | ||
rows = which(data == text, arr.ind = TRUE)[1] + 1) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#' @title Writes an Excel file with the selection list. | ||
#' | ||
#' @description The selection list based on selected data from okplan file and | ||
#' uses standardize_columns to select, format and style columns. | ||
#' | ||
#' @param data The data with units that should be tested. | ||
#' @param filename The name of the Excel file that should be written. | ||
#' @param filepath The path to the Excel file that should be written. | ||
#' @param sheet The name of the Excel sheet with the list. | ||
#' @param calculate_sum \[logical\] Should a line with the sum be appended. Defaults to TRUE. | ||
#' @param dbsource The name of the dbtable in OK_column_standards that should | ||
#' be used for standardizing the columns. | ||
#' @export | ||
|
||
|
||
write_ok_selection_list <- function(data, | ||
sheet, | ||
filename, | ||
filepath, | ||
calculate_sum = TRUE, | ||
dbsource) { | ||
# ARGUMENT CHECKING ---- | ||
# Object to store check-results | ||
checks <- checkmate::makeAssertCollection() | ||
|
||
# Perform checks | ||
# for (i in 1:length(data)) { | ||
checkmate::assert_data_frame(data, max.rows = (1048576 - 1), max.cols = 16384, add = checks) | ||
# } | ||
checkmate::assert_character(sheet, min.chars = 1, min.len = 1, max.len = length(data), unique = TRUE, add = checks) | ||
checkmate::assert_character(filename, min.chars = 1, len = 1, add = checks) | ||
checkmate::assert_directory_exists(filepath, add = checks) | ||
checkmate::assert_logical(calculate_sum, any.missing = FALSE, min.len = 1, add = checks) | ||
checkmate::assert_character(dbsource, min.len = 1, add = checks) | ||
checkmate::assert_choice(dbsource, | ||
choices = unique(OKplan::OK_column_standards[, "table_db"]), | ||
add = checks) | ||
|
||
# Report check-results | ||
checkmate::reportAssertions(checks) | ||
|
||
# GENERATE EXCEL WORKBOOK ---- | ||
okwb <- openxlsx::createWorkbook() | ||
|
||
# for (i in 1:length(data)) { | ||
# i <- 1 | ||
# STANDARDIZE COLUMNS ---- | ||
# column names | ||
okdata <- NVIdb::standardize_columns(data, | ||
standards = OKplan::OK_column_standards, | ||
dbsource = dbsource, | ||
property = "colnames") | ||
|
||
# order columns and keep only designated columns | ||
okdata <- NVIdb::standardize_columns(data = okdata, | ||
standards = OKplan::OK_column_standards, | ||
dbsource = dbsource, | ||
property = "colorder", exclude = TRUE) | ||
|
||
# INCLUDE EXTRA INFORMATION ---- | ||
# Append sum | ||
if(isTRUE(calculate_sum)) { | ||
okdata <- append_sum_line(data = okdata, column = c("ant_prover"), position = "left") | ||
} | ||
|
||
# Append date generated | ||
okdata <- append_date_generated_line(okdata) | ||
|
||
|
||
# STYLE EXCEL SHEET ---- | ||
NVIpretty::add_formatted_worksheet(data = okdata, | ||
workbook = okwb, | ||
sheet = sheet, | ||
wrapHeadlineText = TRUE, | ||
collabels = TRUE, | ||
colwidths = TRUE, | ||
standards = OKplan::OK_column_standards, | ||
dbsource = dbsource) | ||
|
||
|
||
if(isTRUE(calculate_sum)) { | ||
style_sum_line(workbook = okwb, sheet = sheet, data = okdata) | ||
} | ||
# } | ||
# SAVE EXCEL WORKBOOK ---- | ||
openxlsx::saveWorkbook(wb = okwb, file = paste0(filepath, filename), overwrite = TRUE) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.