Skip to content

Commit 318d461

Browse files
authored
Merge pull request #436 from lucdw/master
allow interactions in LHS
2 parents 5197d5d + 5f21af1 commit 318d461

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

R/lav_syntax_parser.R

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -814,9 +814,14 @@ ldw_parse_model_string <- function(model.syntax = "", as.data.frame. = FALSE) {
814814
}
815815
nelem <- length(formul1$elem.type)
816816
# where is the operator
817-
opi <- match(types$lavaanoperator, formul1$elem.type)
818-
# opi <- which(formul1$elem.type == types$lavaanoperator)
819-
# if (length(opi) > 1L) opi <- opi[1L]
817+
opi <- which(formul1$elem.type == types$lavaanoperator)
818+
if (length(opi) > 1L) { # if more then 1 operator skip operators ':'
819+
opii <- 1L
820+
while (formul1$elem.text[opi[opii]] == ":" && opii < length(opi)) {
821+
opii <- opii + 1L
822+
}
823+
opi <- opi[opii]
824+
}
820825
op <- formul1$elem.text[opi]
821826
if (any(op == constraint_operators)) { # ----- constraints -------
822827
lhs <- paste(formul1$elem.text[seq.int(1L, opi - 1L)], collapse = "")
@@ -953,7 +958,8 @@ ldw_parse_model_string <- function(model.syntax = "", as.data.frame. = FALSE) {
953958
colons <- which(formul1$elem.text[seq.int(1L, nelem - 1L)] == ":" &
954959
formul1$elem.type[seq.int(2L, nelem)] == types$identifier)
955960
# check at most 1 colon
956-
if (length(colons) > 1) {
961+
if (length(colons) > 2L ||
962+
(length(colons) == 2L && (colons[1L] > opi || colons[2L] < opi))) {
957963
tl <- ldw_txtloc(modelsrc, formul1$elem.pos[colons[2]])
958964
lav_msg_stop(
959965
gettext(
@@ -965,13 +971,28 @@ ldw_parse_model_string <- function(model.syntax = "", as.data.frame. = FALSE) {
965971
), tl[1L], footer = tl[2L]
966972
)
967973
}
968-
if (length(colons) == 1) {
974+
if (length(colons) > 0L) {
969975
# collapse items around colon "a" ":" "b" => "a:b"
970-
formul1$elem.text[colons - 1L] <-
971-
paste(formul1$elem.text[seq.int(colons - 1L, colons + 1L)],
976+
formul1$elem.text[colons[1L] - 1L] <-
977+
paste(formul1$elem.text[seq.int(colons[1L] - 1L, colons[1L] + 1L)],
972978
collapse = ""
973979
)
974-
formul1 <- ldw_parse_sublist(formul1, seq.int(1L, colons - 1L))
980+
formul1 <- ldw_parse_sublist(formul1,
981+
setdiff(seq.int(1L, nelem), seq.int(colons[1L], colons[1L] + 1L)))
982+
nelem <- length(formul1$elem.type)
983+
if (colons[1L] < opi) {
984+
opi <- opi - 2L # is in LHS
985+
if (length(colons) == 2L) colons[2L] <- colons[2L] - 2L
986+
}
987+
}
988+
if (length(colons) == 2L) {
989+
# collapse items around colon "a" ":" "b" => "a:b"
990+
formul1$elem.text[colons[2L] - 1L] <-
991+
paste(formul1$elem.text[seq.int(colons[2L] - 1L, colons[2L] + 1L)],
992+
collapse = ""
993+
)
994+
formul1 <- ldw_parse_sublist(formul1,
995+
setdiff(seq.int(1L, nelem), seq.int(colons[2L], colons[2L] + 1L)))
975996
nelem <- length(formul1$elem.type)
976997
}
977998
# modifiers

0 commit comments

Comments
 (0)