Skip to content

Commit

Permalink
fix major bug introduced by last commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lrberge committed Feb 10, 2024
1 parent cff3315 commit 4b22eda
Showing 1 changed file with 46 additions and 41 deletions.
87 changes: 46 additions & 41 deletions R/fixest_env.R
Original file line number Diff line number Diff line change
Expand Up @@ -470,58 +470,63 @@ fixest_env = function(fml, data, family = c("poisson", "negbin", "logit", "gauss

# Special beavior .[y] for multiple LHSs
lhs_fml = fml[[2]]
is_iv_fml = lhs_fml[[1]] == "~"
if(is_iv_fml){
lhs_fml = lhs_fml[[2]]
}

if(length(lhs_fml) == 1){
# nothing
} else if(lhs_fml[[1]] == "[" && length(lhs_fml) == 3 && grepl("\\.$", lhs_fml[[2]])){
# works for both .[y] and y.[1:3]

new_lhs_fml = (~ c(x))[[2]]
my_dsb = (~ .[, x])[[2]]
my_dsb[[2]] = lhs_fml[[2]]
my_dsb[[4]] = lhs_fml[[3]]

new_lhs_fml[[2]] = my_dsb

} else {
is_iv_fml = lhs_fml[[1]] == "~"
if(is_iv_fml){
fml[[2]][[2]] = new_lhs_fml
} else {
fml[[2]] = new_lhs_fml
}
lhs_fml = lhs_fml[[2]]
}

if(length(lhs_fml) == 1){
# nothing
} else if(lhs_fml[[1]] == "[" && length(lhs_fml) == 3 && grepl("\\.$", lhs_fml[[2]])){
# works for both .[y] and y.[1:3]

new_lhs_fml = (~ c(x))[[2]]
my_dsb = (~ .[, x])[[2]]
my_dsb[[2]] = lhs_fml[[2]]
my_dsb[[4]] = lhs_fml[[3]]

new_lhs_fml[[2]] = my_dsb

if(is_iv_fml){
fml[[2]][[2]] = new_lhs_fml
} else {
fml[[2]] = new_lhs_fml
}

} else if(as.character(lhs_fml[[1]])[1] %in% c("..", "regex") &&
length(lhs_fml) == 2 && is.character(lhs_fml[[2]])){
} else if(as.character(lhs_fml[[1]])[1] %in% c("..", "regex") &&
length(lhs_fml) == 2 && is.character(lhs_fml[[2]])){

# works for ..("x") ~ y
re = lhs_fml[[2]]
if(grepl(".[", re, fixed = TRUE)){
re = dot_square_bracket(re, call_env, regex = TRUE)
}
# works for ..("x") ~ y
re = lhs_fml[[2]]
if(grepl(".[", re, fixed = TRUE)){
re = dot_square_bracket(re, call_env, regex = TRUE)
}

vars = grep(re, dataNames, value = TRUE, perl = TRUE)
if(length(vars) == 0){
vars = grep(re, dataNames, value = TRUE, perl = TRUE)
if(length(vars) == 0){

msg = re
if(!grepl(".[", lhs_fml[[2]], fixed = TRUE)){
msg = paste0(lhs_fml[[2]],"', evaluated as '", re)
msg = re
if(!grepl(".[", lhs_fml[[2]], fixed = TRUE)){
msg = paste0(lhs_fml[[2]],"', evaluated as '", re)
}

stop("In the LHS of the formula, the regular expression: '", msg, "' leads to no variable being selected. Estimation cannot be done.")
}

stop("In the LHS of the formula, the regular expression: '", msg, "' leads to no variable being selected. Estimation cannot be done.")
new_lhs_txt = paste0("c(", paste0(vars, collapse = ", "), ")")
new_lhs_fml = error_sender(str2lang(new_lhs_txt),
"In the LHS, the expansion of the regex leads to the following invalid expression: '", new_lhs_txt, "'.")

if(is_iv_fml){
fml[[2]][[2]] = new_lhs_fml
} else {
fml[[2]] = new_lhs_fml
}
}

new_lhs_txt = paste0("c(", paste0(vars, collapse = ", "), ")")
new_lhs_fml = error_sender(str2lang(new_lhs_txt),
"In the LHS, the expansion of the regex leads to the following invalid expression: '", new_lhs_txt, "'.")

if(is_iv_fml){
fml[[2]][[2]] = new_lhs_fml
} else {
fml[[2]] = new_lhs_fml
}
}

# Expansion
Expand Down

0 comments on commit 4b22eda

Please sign in to comment.