|
| 1 | +# |
| 2 | +# Copyright (C) 2018 University of Amsterdam |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation, either version 2 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +# |
| 17 | + |
| 18 | +# Main function ---- |
| 19 | +MockAnalysis <- function(jaspResults, dataset, options) { |
| 20 | + # Set title |
| 21 | + jaspResults$title <- "Mock Analysis" |
| 22 | + # Init options: add variables to options to be used in the remainder of the analysis |
| 23 | + options <- .mockInitOptions(jaspResults, options) |
| 24 | + # read dataset |
| 25 | + dataset <- .mockReadData(options) |
| 26 | + # error checking |
| 27 | + errors <- .mockErrorHandling(dataset, options) |
| 28 | + |
| 29 | + # Compute (a list of) results from which tables and plots can be created |
| 30 | + mockResults <- .mockComputeResults(jaspResults, dataset, options) |
| 31 | + |
| 32 | + # Output containers, tables, and plots based on the results. These functions should not return anything! |
| 33 | + .mockContainerMain( jaspResults, options, mockResults) |
| 34 | + .mockTableSomething(jaspResults, options, mockResults) |
| 35 | + .mockTableSthElse( jaspResults, options, mockResults) |
| 36 | + .mockPlotSomething( jaspResults, options, mockResults) |
| 37 | + |
| 38 | + return() |
| 39 | +} |
| 40 | + |
| 41 | +# Init functions ---- |
| 42 | +.mockInitOptions <- function(jaspResults, options) { |
| 43 | + # Determine if analysis can be run with user input |
| 44 | + # Calculate any options common to multiple parts of the analysis |
| 45 | + options |
| 46 | +} |
| 47 | + |
| 48 | +.mockReadData <- function(options) { |
| 49 | + # Read in the dataset using the built-in functions |
| 50 | + if (options$groupvar == "") { |
| 51 | + dataset <- .readDataSetToEnd(columns = variables) |
| 52 | + } else { |
| 53 | + dataset <- .readDataSetToEnd(columns = variables, columns.as.factor = options$groupvar) |
| 54 | + } |
| 55 | + dataset |
| 56 | +} |
| 57 | + |
| 58 | +.mockErrorHandling <- function(dataset, options) { |
| 59 | + # See error handling |
| 60 | + # Either it should be like this |
| 61 | + .hasErrors(dataset, "run", type = c('observations', 'variance', 'infinity'), |
| 62 | + all.target = options$variables, |
| 63 | + observations.amount = '< 2', |
| 64 | + exitAnalysisIfErrors = TRUE) |
| 65 | + |
| 66 | + # Or like this, if you want to do sth with the errors |
| 67 | + errors <- .hasErrors(dataset, "run", message = "short", |
| 68 | + type = c('observations', 'variance', 'infinity'), |
| 69 | + all.target = options$variables, |
| 70 | + observations.amount = '< 2') |
| 71 | + errors |
| 72 | +} |
| 73 | + |
| 74 | +# Results functions ---- |
| 75 | +.mockComputeResults <- function(jaspResults, dataset, options) { |
| 76 | + if (is.null(jaspResults[["stateMockResults"]])) { |
| 77 | + mockResults <- .mockResultsHelper(dataset) |
| 78 | + |
| 79 | + jaspResults[["stateMockResults"]] <- createJaspState(mockResults) |
| 80 | + jaspResults[["stateMockResults"]]$dependOnOptions("variables") |
| 81 | + |
| 82 | + } else { |
| 83 | + mockResults <- jaspResults[["stateMockResults"]]$object |
| 84 | + } |
| 85 | + mockResults |
| 86 | +} |
| 87 | + |
| 88 | +.mockResultsHelper <- function(dataset) { |
| 89 | + # do actual computations |
| 90 | +} |
| 91 | + |
| 92 | +# Output functions ---- |
| 93 | +.mockContainerMain <- function(jaspResults, options, mockResults) { |
| 94 | + if (!is.null(jaspResults[["mockMainContainer"]])) return() |
| 95 | + |
| 96 | + mainContainer <- createJaspContainer("Model fit tables") |
| 97 | + mainContainer$dependOnOptions(c("variables", "someotheroption")) |
| 98 | + |
| 99 | + jaspResults[["mockMainContainer"]] <- mainContainer |
| 100 | +} |
| 101 | + |
| 102 | +.mockTableSomething <- function(jaspResults, options, mockResults) { |
| 103 | + if (!is.null(jaspResults[["mockMainContainer"]][["mockTable"]])) return() |
| 104 | + |
| 105 | + # Below is one way of creating a table |
| 106 | + mockTable <- createJaspTable(title = "Mock Table") |
| 107 | + mockTable$dependOnOptions(c("variables", "someotheroption")) # not strictly necessary because container |
| 108 | + |
| 109 | + # Bind table to jaspResults |
| 110 | + jaspResults[["mockMainContainer"]][["mockTable"]] <- mockTable |
| 111 | + |
| 112 | + # Add column info |
| 113 | + mockTable$addColumnInfo(name = "chisq", title = "\u03a7\u00b2", type = "number", format = "sf:4") |
| 114 | + mockTable$addColumnInfo(name = "pvalue", title = "p", type = "number", format = "dp:3;p:.001") |
| 115 | + mockTable$addColumnInfo(name = "BF", title = "Bayes Factor", type = "number", format = "sf:4") |
| 116 | + mockTable$addColumnInfo(name = "sth", title = "Some Title", type = "string") |
| 117 | + |
| 118 | + # Add data per column |
| 119 | + mockTable[["chisq"]] <- mockResults$column1 |
| 120 | + mockTable[["pvalue"]] <- mockResults$column2 |
| 121 | + mockTable[["BF"]] <- mockResults$column3 |
| 122 | + mockTable[["sth"]] <- mockResults$sometext |
| 123 | +} |
| 124 | + |
| 125 | +.mockTableSthElse <- function(jaspResults, options, mockResults) { |
| 126 | + if (!is.null(jaspResults[["mockMainContainer"]][["mockTable2"]])) return() |
| 127 | + |
| 128 | + # Below is one way of creating a table |
| 129 | + mockTable2 <- createJaspTable(title = "Mock Table Something Else") |
| 130 | + mockTable2$dependOnOptions(c("variables", "someotheroption")) |
| 131 | + |
| 132 | + # Bind table to jaspResults |
| 133 | + jaspResults[["mockMainContainer"]][["mockTable2"]] <- mockTable2 |
| 134 | + |
| 135 | + # Add column info |
| 136 | + mockTable2$addColumnInfo(name = "hallo", title = "Hallo", type = "string") |
| 137 | + mockTable2$addColumnInfo(name = "doei", title = "Doei", type = "string") |
| 138 | + |
| 139 | + # Calculate some data from results |
| 140 | + mockSummary <- summary(mockResults$someObject) |
| 141 | + |
| 142 | + # Add data per column. Calculations are allowed here too! |
| 143 | + mockTable2[["hallo"]] <- ifelse(mockSummary$hallo > 1, "Hallo!", "Hello!") |
| 144 | + mockTable2[["doei"]] <- mockSummary$doei^2 |
| 145 | +} |
| 146 | + |
| 147 | +.mockPlotSomething <- function(jaspResults, options, mockResults) { |
| 148 | + if (!is.null(jaspResults[["mockPlot"]])) return() |
| 149 | + |
| 150 | + mockPlot <- createJaspPlot(title = "Mock Plot", height = 320, width = 480) |
| 151 | + mockPlot$dependOnOptions(c("variables", "someotheroption")) |
| 152 | + |
| 153 | + # Bind plot to jaspResults |
| 154 | + jaspResults[["mockPlot"]] <- mockPlot |
| 155 | + |
| 156 | + mockPlot$plotObject <- plot(mockResults$someObject) |
| 157 | +} |
0 commit comments