-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtask-results.R
170 lines (163 loc) · 7.69 KB
/
task-results.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#' @title TaskResults
#' @description R6 class for TaskResults settings
#' @field id unique identifier of results, helps in deriving final plot name
#' @field sectionId unique identifier of section results, used only by qualification workflows
#' @field plot `ggplot` object corresponding to the figure to be saved
#' @field plotCaption text included into the report explaining the figure
#' @field includePlot logical indicating if the plot should be included in final report
#' @field table data.frame corresponding to the table to be saved
#' @field tableCaption text included into the report explaining the table
#' @field includeTable logical indicating if the table should be included in final report
#' @field textChunk text included into the report explaining the table
#' @field includeTextChunk logical indicating if the text chunk should be included in final report
#' @import ospsuite.utils
#' @keywords internal
TaskResults <- R6::R6Class(
"TaskResults",
cloneable = FALSE,
public = list(
id = NULL,
sectionId = NULL,
plot = NULL,
plotCaption = NULL,
includePlot = TRUE,
table = NULL,
tableCaption = NULL,
includeTable = FALSE,
textChunk = NULL,
includeTextChunk = FALSE,
#' @description
#' Save ggplot figure, available as plot task result, into a file
#' @param fileName path of file corresponding to the figure to save
saveFigure = function(fileName) {
if (isEmpty(self$plot)) {
return()
}
self$plot <- updatePlotDimensions(self$plot)
# Use same dpi as RE environment settings to display correct point size
if (requireNamespace("showtext", quietly = TRUE)) {
currentDPI <- showtext::showtext_opts()$dpi
showtext::showtext_opts(dpi = reEnv$defaultPlotFormat$dpi)
}
ggplot2::ggsave(
filename = fileName,
plot = self$plot,
width = self$plot$plotConfiguration$export$width %||% reEnv$defaultPlotFormat$width,
height = self$plot$plotConfiguration$export$height %||% reEnv$defaultPlotFormat$height,
dpi = reEnv$defaultPlotFormat$dpi,
units = self$plot$plotConfiguration$export$units %||% reEnv$defaultPlotFormat$units
)
# Revert showtext settings
if (requireNamespace("showtext", quietly = TRUE)) {
showtext::showtext_opts(dpi = currentDPI)
}
logDebug(paste0("Figure '", fileName, "' was successfully saved."))
re.tStoreFileMetadata(access = "write", filePath = fileName)
},
#' @description
#' Save data.frame, available as table task result, into a csv file
#' @param fileName path of csv file corresponding to the table to save
saveTable = function(fileName) {
if (isEmpty(self$table)) {
return()
}
write.csv(self$table, file = fileName, row.names = FALSE, fileEncoding = "UTF-8")
logDebug(paste0("Table '", fileName, "' was successfully saved."))
re.tStoreFileMetadata(access = "write", filePath = fileName)
},
#' @description
#' Write markdown content that adds a figure, available as plot task result, into a markdown report
#' To be displayed, figure path must be relative to report location
#' @param reportFile markdown file in which the figure and its caption should be added
#' @param fileRelativePath figure path relative to `reportFile` location
#' @param fileRootDirectory root/working directory needed by `tracelib` package
addFigureToReport = function(reportFile, fileRelativePath, fileRootDirectory) {
if (isEmpty(self$plot)) {
return()
}
# includePlot select if the plot and its caption should be included in final report
# However, tables in task results are saved no matter this field
if (isFALSE(self$includePlot)) {
return()
}
addFigureChunk(
fileName = reportFile,
figureFileRelativePath = fileRelativePath,
figureFileRootDirectory = fileRootDirectory
)
# Issue #1053, figure caption is below
if (!isEmpty(self$plotCaption)) {
# Remove leading and/or trailing whitespace from character strings
addTextChunk(reportFile, paste0("**Figure: ", trimws(self$plotCaption), "**"))
}
# Enforce 2 blank lines using html notation to improve report clarity
addTextChunk(reportFile, rep("<br>", reEnv$blankLinesBetweenArtifacts))
},
#' @description
#' Write markdown content that adds a data.frame, available as table task result, into a markdown report
#' @param reportFile markdown file in which the table and its caption should be added
#' @param fileRelativePath table path relative to `reportFile` location
#' @param fileRootDirectory root/working directory needed by `tracelib` package
#' @param digits number of decimal digits in displayed numbers
#' @param scientific logical defining if displayed numbers use scientific writing
addTableToReport = function(reportFile, fileRelativePath, fileRootDirectory, digits = NULL, scientific = NULL) {
if (isEmpty(self$table)) {
return()
}
# includeTable select if the table and its caption should be included in the markdown report
# tables in task results are still saved but won't appear in the markdown report
if (isFALSE(self$includeTable)) {
return()
}
if (!isEmpty(self$tableCaption)) {
addTextChunk(reportFile, paste0("**Table: ", trimws(self$tableCaption), "**"))
}
addTableChunk(
fileName = reportFile,
tableFileRelativePath = fileRelativePath,
tableFileRootDirectory = fileRootDirectory,
digits = digits,
scientific = scientific
)
# Enforce blank lines using html notation to improve report clarity
addTextChunk(reportFile, rep("<br>", reEnv$blankLinesBetweenArtifacts))
},
#' @description
#' Write markdown content that adds text, available as textChunk task result, into a markdown report
#' @param reportFile markdown file in which the text should be added
addTextChunkToReport = function(reportFile) {
if (isEmpty(self$textChunk)) {
return()
}
if (isFALSE(self$includeTextChunk)) {
return()
}
addTextChunk(reportFile, self$textChunk)
}
)
)
#' @title saveTaskResults
#' @description Save task results within a `TaskResults` object
#' @param id unique identifier of results, helps in deriving final plot name
#' @param sectionId unique identifier of section results, used only by qualification workflows
#' @param plot `ggplot` object corresponding to the figure to be saved
#' @param plotCaption text included into the report explaining the figure
#' @param includePlot logical indicating if the plot should be included in final report
#' @param table data.frame corresponding to the table to be saved
#' @param tableCaption text included into the report explaining the table
#' @param includeTable logical indicating if the table should be included in final report
#' @param textChunk text included into the report explaining the table
#' @param includeTextChunk logical indicating if the text chunk should be included in final report
#' @param taskResults A `TaskResults` object
#' @return A `TaskResults` object
#' @import ospsuite.utils
#' @export
saveTaskResults <- function(id = NULL, sectionId = NULL, plot = NULL, plotCaption = NULL, includePlot = NULL, table = NULL, tableCaption = NULL, includeTable = NULL, textChunk = NULL, includeTextChunk = NULL, taskResults = NULL) {
taskResults <- taskResults %||% TaskResults$new()
eval(parseVariableToObject(
objectName = "taskResults",
variableName = c("id", "sectionId", "plot", "plotCaption", "includePlot", "table", "tableCaption", "includeTable", "textChunk", "includeTextChunk"),
keepIfNull = TRUE
))
return(taskResults)
}