Skip to content

Commit 7deff88

Browse files
boutinbvandenman
andauthored
Use jaspSyntax (#167)
* Use jaspSyntax * Update R/common.R Co-authored-by: Don van den Bergh <donvdbergh@hotmail.com> --------- Co-authored-by: Don van den Bergh <donvdbergh@hotmail.com>
1 parent 31bc23b commit 7deff88

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export(rowVariance)
135135
export(rowVarianceNaRm)
136136
export(runJaspResults)
137137
export(runWrappedAnalysis)
138+
export(storeDataSet)
138139
export(startProgressbar)
139140
export(tDist)
140141
export(unifDist)

R/common.R

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,29 +1145,34 @@ editImage <- function(name, optionsJson) {
11451145
return(toJSON(response))
11461146
}
11471147

1148-
registerData <- function(data) {
1149-
#TODO
1150-
}
1151-
1152-
checkAnalysisOptions <- function(analysisName, options, version) {
1153-
# TODO when QMLComponents can be linked to jaspBase
1154-
return(options)
1148+
#' @export
1149+
storeDataSet <- function(dataset) {
1150+
jaspSyntax::loadDataSet(dataset)
11551151
}
11561152

11571153
#' @export
1158-
runWrappedAnalysis <- function(analysisName, data, options, version) {
1154+
runWrappedAnalysis <- function(moduleName, analysisName, qmlFileName, options, version, preloadData) {
11591155
if (jaspResultsCalledFromJasp()) {
1160-
1161-
result <- list("options" = options, "analysis" = analysisName, "version" = version)
1162-
result <- jsonlite::toJSON(result, auto_unbox = TRUE, digits = NA, null="null", force = TRUE)
1163-
return(as.character(result))
1156+
# In this case, it is JASP Desktop that called the wrapper. This was done to parse the R code, and to get the arguments
1157+
# in a structured way. In this way the Desktop can then set the options to the QML controls of the form, and this will run the analysis.
1158+
# So here, just give back the parsed options.
1159+
return(toJSON(list("options" = options, "module" = moduleName, "analysis" = analysisName, "version" = version)))
11641160

11651161
} else {
1162+
# The wrapper is called inside an R environment (R Studio probably).
1163+
# The options must be parsed and checked by the QML form, and then the real analysis can be called.
1164+
qmlFile <- file.path(find.package(moduleName), "qml", qmlFileName)
1165+
# Load the qml form, and set the right options (formula should be parsed and all logics set in QML should be checked), and run the analysis
1166+
options <- jaspSyntax::loadQmlAndParseOptions(moduleName, analysisName, qmlFile, as.character(toJSON(options)), version, preloadData)
11661167

1167-
options <- checkAnalysisOptions(analysisName, options, version)
1168-
# fool renv so it does not try to install jaspTools
1169-
jaspToolsRunAnalysis <- utils::getFromNamespace("runAnalysis", asNamespace("jaspTools"))
1170-
return(jaspToolsRunAnalysis(analysisName, data, options))
1168+
if (options == "")
1169+
stop("Error when parsing the options")
1170+
1171+
internalAnalysisName <- paste0(moduleName, "::", analysisName, "Internal")
11711172

1173+
return(runJaspResults(name=internalAnalysisName, title=analysisName, dataKey="{}", options=options, stateKey="{}", functionCall=internalAnalysisName, preloadData=preloadData))
11721174
}
11731175
}
1176+
1177+
1178+

R/formula.R

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@
4545
#' @export
4646
jaspFormula <- function(formula, data) {
4747
formulaEncoded <- formulaEncode(formula)
48-
data <- formulaCheckOrReadData(data)
49-
formulaCheckRequirements(formulaEncoded, data)
48+
49+
colNames <- formulaCheckOrReadData(data)
50+
formulaCheckRequirements(formulaEncoded, colNames)
5051

5152
# TODO: We should use the encoded formula hier, but if a column has a type ('col.scale'), then it is decoded by 'col'
5253
result <- list(
@@ -63,7 +64,6 @@ jaspFormula <- function(formula, data) {
6364
#' @rdname jaspFormula
6465
#' @export
6566
makeJaspFormula <- function(..., response=NULL, data) {
66-
data <- formulaCheckOrReadData(data)
6767

6868
if(!is.null(response) && !is.character(response)) {
6969
stop("`response` must be a character.", domain = NA)
@@ -78,7 +78,7 @@ makeJaspFormula <- function(..., response=NULL, data) {
7878
}
7979

8080

81-
rhs <- vapply(dots, makeJaspFormulaRhs, character(1), data = data)
81+
rhs <- vapply(dots, makeJaspFormulaRhs, character(1))
8282

8383
formula <- stats::reformulate(rhs, response)
8484
return(jaspFormula(formula, data))
@@ -94,8 +94,7 @@ jaspFormulaRhs <- function(terms = NULL, group = NULL, intercept = TRUE, correla
9494
return(result)
9595
}
9696

97-
makeJaspFormulaRhs <- function(rhs, data) {
98-
allVarNames <- colnames(data)
97+
makeJaspFormulaRhs <- function(rhs) {
9998

10099
result <- paste(rhs[["terms"]], collapse = "+")
101100
if (is.null(rhs[["terms"]])) {
@@ -133,27 +132,26 @@ formulaEncode <- function(formula) {
133132

134133
formulaCheckOrReadData <- function(data) {
135134
# If we are in JASP and no data are supplied explicitly, we simply read the dataset from JASP.
136-
if(jaspBase::jaspResultsCalledFromJasp()) # && (missing(data) || is.null(data)))
135+
if(jaspBase::jaspResultsCalledFromJasp()) {
137136
data <- jaspBase::readDataSetToEnd(all.columns = TRUE)
137+
colNames = decodeColNames(colnames(data))
138+
} else {
139+
colNames = jaspSyntax::getVariableNames()
140+
}
138141

139-
if(missing(data) || is.null(data) || !is.data.frame(data))
140-
stop("`data` must be a data frame.", domain = NA)
141-
142-
return(data)
142+
return(colNames)
143143
}
144144

145-
formulaCheckRequirements <- function(formula, data) {
145+
formulaCheckRequirements <- function(formula, columnNames) {
146146
if (!inherits(formula, "formula")) {
147147
stop("`formula` argument must be object of class `formula`.", domain = NA)
148148
}
149-
150149
attr <- attributes(stats::terms(formula))
151150

152151
if (!is.null(attr[["offset"]])) {
153152
stop("JASP formulas do not understand `offset` terms. Analyses that allow the `offset` terms have a special `offset` argument.", domain = NA)
154153
}
155154

156-
columnNames <- decodeColNames(colnames(data))
157155
lhs <- decodeColNames(all.names(formulaExtractLhs(formula)))
158156
anyLhsTransformed <- !all(lhs %in% c(columnNames, "cbind", "(", ")"))
159157

0 commit comments

Comments
 (0)