-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplot-task.R
134 lines (126 loc) · 5.15 KB
/
plot-task.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
#' @title PlotTask
#' @description R6 class for PlotTask settings
#' @field title section title in the report corresponding to the task
#' @field reference id of anchor tag referencing title
#' @field fileName name of report appendix file associated to task
#' @field getTaskResults function called by task that computes and format figure results
#' @field nameTaskResults name of the function that returns task results,
#' @export
#' @family workflow tasks
PlotTask <- R6::R6Class(
"PlotTask",
inherit = Task,
public = list(
title = NULL,
reference = NULL,
fileName = NULL,
getTaskResults = NULL,
nameTaskResults = "none",
#' @description
#' Create a `PlotTask` object
#' @param reportTitle title to be printed in the report
#' @param reportReference id of anchor tag referencing title
#' @param fileName name of report appendix file associated to task
#' @param getTaskResults function called by task that computes and format figure results
#' @param nameTaskResults name of the function that returns task results,
#' @param ... input parameters inherited from `Task` R6 class
#' @return A new `PlotTask` object
initialize = function(reportTitle = NULL,
reportReference = NULL,
fileName = NULL,
getTaskResults = NULL,
nameTaskResults = "none",
...) {
super$initialize(...)
self$title <- reportTitle
self$reference <- reportReference
self$fileName <- file.path(self$workflowFolder, fileName)
self$getTaskResults <- getTaskResults
self$nameTaskResults <- nameTaskResults
},
#' @description
#' Save the task results related to a `structureSet`.
#' @param structureSet A `SimulationStructure` object defining the properties of a simulation set
#' @param taskResults list of `TaskResults` objects
saveResults = function(structureSet, taskResults) {
simulationSetName <- structureSet$simulationSet$simulationSetName
# Issue #1084: new reference title get anchor at the end of title
addTextChunk(
fileName = self$fileName,
text = paste(
"## ", self$title, " for ", simulationSetName,
anchor(paste0(self$reference, "-", removeForbiddenLetters(simulationSetName))),
sep = ""
)
)
for (result in taskResults) {
# Get both absolute and relative paths for figures and tables
# Absolute path is required to always find the figure/table
# Relative path is required by the final md report
plotFileName <- getDefaultFileName(
removeForbiddenLetters(simulationSetName),
suffix = result$id,
extension = reEnv$defaultPlotFormat$format
)
figureFilePath <- self$getAbsolutePath(plotFileName)
figureFileRelativePath <- self$getRelativePath(plotFileName)
tableFileName <- getDefaultFileName(
removeForbiddenLetters(simulationSetName),
suffix = result$id,
extension = "csv"
)
tableFilePath <- self$getAbsolutePath(tableFileName)
tableFileRelativePath <- self$getRelativePath(tableFileName)
# Provide error message indicating which file could not be saved
tryCatch(
{
result$saveFigure(fileName = figureFilePath)
},
error = function(e) {
stop(messages$ggsaveError(figureFilePath, simulationSetName, e))
}
)
result$addFigureToReport(
reportFile = self$fileName,
fileRelativePath = figureFileRelativePath,
fileRootDirectory = self$workflowFolder
)
result$saveTable(fileName = tableFilePath)
result$addTableToReport(
reportFile = self$fileName,
fileRelativePath = tableFileRelativePath,
fileRootDirectory = self$workflowFolder,
digits = self$settings$digits,
scientific = self$settings$scientific
)
result$addTextChunkToReport(reportFile = self$fileName)
}
return(invisible())
},
#' @description
#' Run task and save its output results
#' @param structureSets list of `SimulationStructure` objects
runTask = function(structureSets) {
actionToken <- re.tStartAction(actionType = "TLFGeneration", actionNameExtension = self$nameTaskResults)
logInfo(messages$runStarting(self$message))
t0 <- tic()
resetReport(self$fileName)
addTextChunk(
fileName = self$fileName,
text = paste("# ", self$title, anchor(self$reference), sep = "")
)
if (!is.null(self$outputFolder)) {
dir.create(file.path(self$workflowFolder, self$outputFolder), showWarnings = FALSE)
}
for (set in structureSets) {
logInfo(messages$runStarting(self$message, set$simulationSet$simulationSetName))
if (self$validateStructureSetInput(set)) {
taskResults <- self$getTaskResults(set, self$settings)
self$saveResults(set, taskResults)
}
}
re.tEndAction(actionToken = actionToken)
logInfo(messages$runCompleted(getElapsedTime(t0), self$message))
}
)
)