#### File showing off things that go wrong or *went* wrong in the past1#### -- with R-mode (mostly coded in ../lisp/ess-mode.el )23### NOTE: this file is indented with RRR style !!!!!4### but do not change indentations anymore of anything in here:5### expressions are written as we *want* them, not as ESS currently puts them67options(keep.source = FALSE) # so we see R's deparse() + print() indentation8910### -------- 1 --------- extraneous comment chars : This seems fixed1112## From: Robert Gentleman <[email protected]>13## To: Martin Maechler <[email protected]>14## Subject: ESS buglet15## Date: Sun, 01 Jul 2007 21:41:24 -07001617## Hi Martin,18## It seems that the following buglet exists (at least in what ever19## version I am using)2021##a silly comment22##and a second one23foo <- function(x=a, abc = list("def", a=1,3,3), more.args, and, bla,24blu, bl,25another, plus, yet.another, and_mbasd,26lots = NULL,27more = NULL,28args = NULL) {29x30}3132##- when the line before a function def is a comment, and adding args,33##- then new lines, when generated have a comment char at the beginning of34##- the line. It is slightly annoying as I have to remove the comment char.35##-36##- If I add a blank line after the comment line, then the problem does not37##- occur.38## and another ''anonymous'' function:39function(x=a, abc = list("def", a=c(1,3,3)), more.args, and, bla, blu,40blo, Abc,41def,42another, and_another, and_this) {43...; ...44}4546## This is a "TRUE" example (from Matrix/tests/ ):47NA.or.True <- function(x) is.na(x) | x4849abc <- function(x, y, ...) this.is.just.a.one.liner(x,y, z=TRUE, ...)5051## A more-liner function with no "{...}" -- this one even works (but not all!)52mindiff <- function(df) df[which.min(df$diff),53which.max(df$daff)]5455## Two functions in one line - can I "send" just one of them? {no, not "simply"}56f1 <- function(x) be.friendly(x, force=TRUE); f2 <- function(x,y) x*sin(pi*x)5758### --- 2 ----------------------------------------------------------------59### --- Suggestion (Jenny Brian): --> Create a (defun ess-eval-multiline .)60## Here is useful valid R "test code":6162## From 'example(plot.default)' :6364Speed <- cars$speed65Distance <- cars$dist66plot(Speed, Distance, panel.first = grid(8,8),67pch = 0, cex = 1.2, col = "blue")68pp <- plot(Speed, Distance, panel.first = grid(8,8),69pch = 0, cex = 1.2, col = "blue")70plot(Speed, Distance,71panel.first = lines(lowess(Speed, Distance), lty = "dashed"),72pch = 0, cex = 1.2, col = "blue")7374## Note: We now at least C-c C-c {ess-eval-function-or-paragraph-and-step}7576### --- 3 ----------------------------------------------------------------77###--- This one (from the Matrix package) is for testing ess-roxy...,78## i.e., C-c C-o7980## not exported but used more than once for "dimnames<-" method :81## -- or do only once for all "Matrix" classes ??82dimnamesGets <- function (x, value) {83d <- dim(x)84if (!is.list(value) || length(value) != 2 ||85!(is.null(v1 <- value[[1]]) || length(v1) == d[1]) ||86!(is.null(v2 <- value[[2]]) || length(v2) == d[2]))87stop(gettextf("invalid dimnames given for '%s' object", class(x)))88x@Dimnames <- list(if(!is.null(v1)) as.character(v1),89if(!is.null(v2)) as.character(v2))90x91}9293### --- 4 ----------------------------------------------------------------94### continued statements95a <- function(ch) {96if(ch == Inf) {97E.cond <- numeric(nb)98}99else {100indic <- ifelse(jinf+1 <= 1 & jsup >= 1,1,0)101E.cond <- ch*(-pbinom(jinf,ni,prb) + 1-pbinom(js.n,ni,prb)) +102ifelse(ni == 1, prb*indic,103mu*(pbinom(js.n-1,pmax(ni-1,1),prb) -104pbinom(jinf-1,pmax(ni-1,1),prb))) / sV -105### ^-- here106mu/sV*(pbinom(js.n,ni,prb) - pbinom(jinf,ni,prb))107### ^-- here108indic2 <- ifelse(jinf+1 <= 1 & jsup >= 1 & ni == 2,1,0)109}110}111112113### --- 5 ----------------------------------------------------------------114### The beginning of function is not found correctly, and hence115### all "ess-*-function" (C-M-a, C-M-e, ...) fail:116117setMeneric <-118## It is clearly allowed to have comments here.119## S version 4, and John Chambers in particular like it.120##121## BUG: M-C-e or M-C-a fails from ``here'' --122## --- effectively because of ess-beginning-of-function fails123## and that really relies on finding ess-function-pattern;124## i.e., ess-R-function-pattern in ~/emacs/ess/lisp/ess-cust.el125##126function(name, def = NULL, group = list(), valueClass = character(),127where = topenv(parent.frame()), genericFunction = NULL)128{129## comments in here are at least kept via "source" attribute130if(exists(name, "package:base") &&131typeof(get(name, "package:base")) != "closure") {132FALSE133}134"ABC"135}136137### --- 6 ----------------------------------------------------------------138## In one-liners without "{ ... }" body, the end-of-function is also139## not correctly found:140## Use C-M-e to see: In these two, the "end-of-function" is after141## 'class' :142## ---- these all work now (ESS version 5.3.8) :143## no it doesn't VS[10-03-2012|ESS 12.03]:144onelinerFails <- function(x, ...) class(x)145146onelinerFailsToo <-147function(x, ...)148class(x)149150onelinerWorks <- function(x, ...) { class(x) }151152onelinerWorksToo <-153function(x, ...) {154class(x)155}156157### --- 7 ----------------------------------------------------------------158## idem:159## this has one line more before 'function' than "typically:"160setMethod("[", signature(x = "dgTMatrix", i = "numeric", j = "missing",161drop = "logical"),162function (x, i, j, ..., drop) { ## select rows163storage.mode(i) <- "integer"164xi <- x@i + 1:1 # 1-indexing165## ...................166if (drop && any(nd == 1)) drop(as(x,"matrix")) else x167})168169### --- 8 ----------------------------------------------------------------170## idem:171## all bellow are ok VS[10-03-2012|ESS 12.03]:172"dimnames<-.data.frame" <- function(x, value) {173d <- dim(x)174if(!is.list(value) || length(value) != 2175|| d[[1]] != length(value[[1]])176|| d[[2]] != length(value[[2]]))177stop("invalid 'dimnames' given for data frame")178row.names(x) <- as.character(value[[1]]) # checks validity179names(x) <- as.character(value[[2]])180x181}182183'[.foo' <- function(x, i, value)184{185###186y <- x187y[i] <- value188y189}190191'[[.bar' <- function(x, i, value)192{193## bla bla bla194y <- as.foo(x) ; y[[i]] <- value195y196}197198"[<-.foobar" <- function(x,i,j,value) {199## just something200x201}202203"names<-.foobar" <- function(x, value) {204## just something else205x206}207208`[<-.data.frame` <- function(x, i, j, value)209{210nA <- nargs() # value is never missing, so 3 or 4.211212###..........213214class(x) <- cl215x216}217218"[[<-.data.frame"<- function(x, i, j, value)219{220cl <- oldClass(x)221## delete class: Version 3 idiom222## to avoid any special methods for [[<-223class(x) <- NULL224225###...........226227class(x) <- cl228x229}230231232"$<-.data.frame" <- function(x, i, value)233{234cl <- oldClass(x)235## delete class: Version 3 idiom236## to avoid any special methods for [[<-237238###...........239240class(x) <- cl241return(x)242}243244## swanky functions:245`swank:quit-inspector` <- function(slimeConnection, sldbState) {246resetInspector(slimeConnection)247FALSE248}249250'swank:quit-inspector' <- function(slimeConnection, sldbState) {251resetInspector(slimeConnection)252FALSE253}254255256### --- 9 ----------------------------------------------------------------257## VS[03-2012|12.03]:FIXED:258259## From: "Sebastian P. Luque" <[email protected]>260## To: [email protected]261## Subject: [ESS-bugs] ess-mode 5.12; `ess-indent-line' error262## Date: Tue, 17 Aug 2010 13:08:25 -0500263264## With the following input, and point on the line with "Table 8.3":265## it was the parenthetical expression at the beg of line266267if (require(lme4)) {268## Model in p. 213269(fm1 <- lmer(logFEV1 ~ age + log(height) + age0 + log(height0) + (age | id),270data=fev1, subset=logFEV1 > -0.5))271## Table 8.3272VarCorr(fm1)$id * 100273274## Model in p. 216275(fm2 <- update(fm1, . ~ . - (age | id) + (log(height) | id)))276}277278### -----279## hitting TAB (`ess-indent-command'), which calls `ess-indent-line' I get280## the following trace:281282## ....: (scan-error "Containing expression ends prematurely" 20 20)283## scan-sexps(177 -2)284## forward-sexp(-2)285## ...286## ess-continued-statement-p()287## ......288289## Interestingly, if the lines 2-4 are absent, then the problem is gone.290## The problem is also there in ESS 5.11.291292## I'll try to find out what is going on in `ess-continued-statement-p' but293## given that I'm not very familiar with the stuff in ess-mode.el, I'm294## submitting the report in case somebody can detect the issue sooner.295296## another example: hitting Tab at }else line297.essDev_differs <- function(f1, f2){298if (is.function(f1) && is.function(f2)){299!(identical(body(f1), body(f2)) && identical(args(f1), args(f2)))300}else301!identical(f1, f2)302}303304305306### --- 10 ---------------------------------------------------------------307## indent at 0 after }else:308## VS:[03-2012|12.03]:FIXED:309if (is.function(f1) && is.function(f2)){310!(identical(body(f1), body(f2)) && identical(args(f1), args(f2)))311}else312!identical(f1, f2)313314315### --- 11 ---------------------------------------------------------------316## --------------- C-c C-c was finding the wrong "beginning of function"317## [:FIXED:, 2011-05-28]318foobar <- function(...) {}319rm(list=ls())320321##--------> consequence of the above experiments:322## the 2nd form is numerically "uniformly better" than the first323##--------> 2011-05-27: Change Frank's psiInv() to324## psiInv = function(t,theta)325## -log1p(exp(-theta)*expm1((1-t)*theta)/expm1(-theta))326327### --- 12 ---------------------------------------------------------------328##--- In the following block, in the first line, C-c C-c does *NOT* behave329## VS[10-03-2012|ESS 12.03]: works fine for me:330th <- 48 # now do ls() and see what happened ... the horror !!!331d <- 3332cpF <- list("Frank", list(th, 1:d))333cop <- acF <- cpF$copula334335### --- 13 ---------------------------------------------------------------336## VS[05-05-2012|ESS 12.04]: looks like :FIXED:337338## From: Aleksandar Blagotic <[email protected]>339## To: <[email protected]>340## Subject: [ESS] R-mode: forward-sexp: Scan error: "Unbalanced parentheses"341## Date: Tue, 6 Dec 2011 01:24:11 +0100342#343## Let's presuppose that I have a function like this:344#345fn <- function(x, ...){346re <- "^#{1,6} [[:print:]]+$"347grepl(re, x, ...)348}349## As soon as I put my cursor at the end of the line with regexp, and350## press RET, I get this error:351352## forward-sexp: Scan error: "Unbalanced parentheses"353##354##-------355## Rodney S: I can reproduce it ...356## Martin M: I can NOT reproduce it, neither with 'emacs -Q';357## tried both ESS 5.14 and ESS from svn358## VS[03-2012|12.03]: Cannot reproduce it either, solved?359360361### --- 14 ---------------------------------------------------------------362## check the behavior of ess-arg-function-offset-new-line363364a <- some.function(365arg1,366arg2)367## ^--- RRR has ess-arg-function-offset-new-line (4) ==> should indent here368369a <- some.function(arg1,370arg2)371## ^--- here372373374### --- 15 --------------------------------------------------------------375## VS[05-05-2012|ESS 12.04]:FIXED:376## indentation of the 3rd line is wrong377for(s in seq(10, 50, len = 5))378for(a in seq(.5, 1, len = 5))379pt_dif_plot(s, a)380## ^-- here381382### --- 16 ----383## VS[05-05-2012|ESS 12.04]:FIXED:384## MM[2014-04-28]: added '}' before else (=> "{" after if(.))385## so parse(<file>) works at all!386## Gives error unbalanced para at else lines and indentation is wrong387## error: Point is not in a function according to 'ess-function-pattern'.388getOrCreateForm <- function(bindName, whereEnv)389if(exists(bindName, envir = get(".forms", envir = whereEnv))) {390get(bindName, envir = whereEnv)391### ^-- here392} else393new("protoForm")394### ^-- here395396397398parentContainer <-399if(is.null(.getPrototype(.Object@host))) { emptyenv()400} else sdf401### ^-- } is here (broken!)402403parentContainer <-404if(is.null(.getPrototype(.Object@host))) emptyenv()405else sdf406### ^-- here407408### --- 17 ---409## Indentation ----- "expression" is special410expremmion <- c(1, 3,4119876)# was always ok412## Had wrong indentation here:413expression <- c(2343,41423874, 239487)415416## or here:417foo <- function(x) {418expression <- c(2343,41923874, 239487)42010 + expression421}422423## Where as here, we *do* want the indentation to424## *NOT* go all the way to the right:425426{427my.long.Expression <- expression(428x[a[j]] == exp(theta[1] + theta[2]^2),429x[b[i]] == sin(theta[3] ~~ theta[4])430)431ausdruck <- expression432my.long.Expr...... <- ausdruck(433x[a[j]] == exp(theta[1] + theta[2]^2),434)435}436437## VS[18-08-2012]: redundant feature. This is a feature for long subexpressions438## imidiately folowing new line. Documented in ess-arg-function-offset-new-line439440### --- 18 ---441## M-C-a (beginning of function)442## ----- anywhere inside the following function, M-C-a must go to beginning443Ops.x.x <- function(e1, e2)444{445d <- dimCheck(e1,e2)446if((dens1 <- extends(c1 <- class(e1), "denseMatrix")))447gen1 <- extends(c1, "generalMatrix")448if((dens2 <- extends(c2 <- class(e2), "denseMatrix")))449gen2 <- extends(c2, "generalMatrix")450if(dens1 && dens2) { ## both inherit from ddense*451geM <- TRUE452if(!gen1) {453if(!gen2) { ## consider preserving "triangular" / "symmetric"454geM <- FALSE455le <- prod(d)456isPacked <- function(x) length(x@x) < le457}458}459## now, in all cases @x should be matching & correct {only "uplo" part is used}460r <- callGeneric(e1@x, e2@x)461if(geM)462new(paste0(.M.kind(r), "geMatrix"), x = r, Dim = d, Dimnames = dimnames(e1))463else464new(paste0(.M.kind(r), Mclass), x = r, Dim = d, .....)465}466else {467r <- ....468469## criterion "2 * nnz(.) < ." as in sparseDefault() in Matrix() [./Matrix.R] :470if(2 * nnzero(r, na.counted = TRUE) < prod(d))471as(r, "sparseMatrix") else r472}473}474475476### --- 19 ---477## indentation with regexp (bug in ess-backward-to-noncomment)478parse_roc <- function(lines, match = "^\\s*+\' ?") {479lines <- lines[str_detect(lines, match)]480if (length(lines) == 0) return(NULL)481### ^-- here (broken because of the # in the regexp)482}483484485### --- 20 ---486## continuation indentation must be consisten in/out {}:487488{489a <- ggplot(data = overtime.by.month,490aes(x="", y=Percent, fill = Overtime)) +491geom_bar(width = 1) +492xlab('') +493ylab(sub.txt) +494labs(title = title.txt) +495facet_wrap(~Year.Month)496}497498a <- ggplot(data = overtime.by.month,499aes(x="", y=Percent, fill = Overtime)) +500geom_bar(width = 1) +501xlab('') +502ylab(sub.txt) +503labs(title = title.txt) +504facet_wrap(~Year.Month)505### ^-- face_wrap must be here506507508### --- 21 ---509510## From: Marius Hofert <[email protected]>511## Date: Fri, 15 Mar 2013 21:00:45 +0100 (32 minutes, 1 second ago)512## Hi,513## The following bug happens in ESS 12.09-2 [rev. 5395 (2013-01-10)]. Put the514## cursor in the line before the function head and hit C-c C-c.515516foo <- function(x)517x # bar518x <- 1:10519520## I'll see521## > + > [1] 1 2 3 4 5 6 7 8 9 10522523foo <- function(x) x*x524bar <- function(y) y525## via C-c C-c leads to "Error: object 'bar' not found".526527528### --- 22 ----529## wrong indentation because of #, same reason as 19.530if (!grepl("#", x))531return(res)532533### --- 23 ----534### three ways to indent closing parent depending on context:535foo <-536function_call(537a,538b,539c540)541### ^- here542543foo <- function_call(544a,545b,546c547) ## bol548549foo <- function_call(a,550b,551c552)553### ^-) is here554555### --- 24 ---556### shift comma in function calls557558foo <- function_call(a559, b560, c561### ^- c is here562)563564### --- 25 ---565## if/else in function calls and nested566567function_call(abc =568if (test)569do_something570else571do_something_else)572573function_call(574abc =575if (test)576do_something577else578do_something_else)579580581function_call(abc = if (test)582do_something583else584do_something_else)585586## real example is smooth.spline() source code:587ss <- function (x, all.knots, nknots, ...)588{589if (all.knots) {590if (!missing(nknots) && !is.null(nknots))591warning("'all.knots' is TRUE; 'nknots' specification is disregarded")592nknots <- nx593} else if (is.null(nknots)) # <- for back compatibility594nknots <- .nknots.smspl(nx)595else {596if (is.function(nknots))597nknots <- nknots(nx)598else if (!is.numeric(nknots))599stop("'nknots' must be numeric (in {1,..,n})")600if (nknots < 1)601stop("'nknots' must be at least 1")602else if (nknots > nx)603stop("cannot use more inner knots than unique 'x' values")604}605}606607## "if" conditional is an exception of the continuation rules:608## Here, we do not want subsequently further indentation of the c1 || c2 || c3609## part:610t2 <- function(x) {611if(long.expression.of.some.size(x, pi) ||612another.longish.expression(sin(x)*exp(x)) ||613a.third.condition.under.which.A.is.chosen)614### ^-- here615A616else617B618}619620621r <-622(some.function (x, 2342) +623another.f (x^3) + sdfsdf - sdfsdf +624and(x) + the(x) - last(x)*part(3))625626627### --- 26 ----628## This is formally correct R, though help(parse) mentions the line-length limit of629## 4095 __when reading from the console__630## ESS gives syntax errors when evaluating this631## because line length >= 4096 :632##633x <- c(1, 3.075819, 1.515999, 2.156169, 1.480742, 1.765485, 1.460206, 1.603707, 1.427429, 1.504712, 1.334528, 1.48297, 1.355308, 1.383867, 1.319241, 1.36065, 1.307467, 1.365596, 1.255259, 1.352741, 1.239381, 3.15342, 1.799889, 2.258497, 1.688312, 1.906779, 1.548203, 1.724785, 1.500873, 1.573442, 1.417137, 1.540805, 1.395945, 1.472596, 1.394247, 1.377487, 1.337394, 1.369354, 1.333378, 1.3181, 1.313813, 1.315528, 2.12777, 2.718898, 1.993509, 2.220433, 1.820585, 1.97782, 1.672455, 1.770151, 1.587478, 1.685352, 1.539295, 1.584536, 1.499487, 1.50702, 1.41952, 1.449058, 1.393042, 1.432999, 1.369964, 1.400997, 1.333824, 2.950549, 2.145387, 2.382224, 1.927077, 2.032489, 1.8371, 1.877833, 1.710891, 1.756053, 1.620778, 1.657761, 1.558978, 1.56257, 1.508633, 1.534406, 1.46709, 1.468734, 1.432529, 1.455283, 1.386975, 1.417532, 2.229573, 2.494447, 2.016117, 2.190061, 1.877996, 1.978964, 1.767284, 1.836948, 1.677372, 1.743316, 1.616383, 1.655964, 1.55484, 1.594831, 1.502185, 1.543723, 1.467005, 1.491123, 1.44402, 1.446915, 1.401578, 2.580264, 2.109121, 2.240741, 1.944719, 2.043397, 1.821808, 1.89725, 1.748788, 1.786988, 1.659333, 1.697012, 1.610622, 1.616503, 1.538529, 1.562024, 1.499964, 1.529344, 1.474519, 1.483264, 1.441552, 1.434448, 2.165233, 2.320281, 2.007836, 2.086471, 1.884052, 1.950563, 1.76926, 1.843328, 1.708941, 1.741039, 1.627206, 1.644755, 1.580563, 1.593402, 1.527312, 1.568418, 1.501462, 1.502542, 1.464583, 1.467921, 1.431141, 2.340443, 2.048262, 2.161097, 1.926082, 1.995422, 1.81446, 1.853165, 1.738533, 1.784456, 1.679444, 1.696463, 1.612931, 1.629483, 1.548186, 1.580026, 1.52198, 1.531111, 1.482914, 1.484824, 1.442726, 1.447838, 2.093386, 2.185793, 1.948989, 2.02804, 1.867137, 1.907732, 1.771923, 1.800413, 1.691612, 1.720603, 1.642705, 1.649769, 1.589028, 1.598955, 1.539759, 1.55096, 1.503965, 1.50703, 1.471349, 1.469791, 1.436959, 2.218315, 1.997369, 2.041128, 1.887059, 1.928524, 1.79626, 1.827538, 1.716748, 1.735696, 1.658329, 1.664211, 1.599286, 1.611511, 1.553925, 1.562637, 1.516805, 1.529894, 1.476064, 1.482474, 1.453253, 1.458467, 2.0247, 2.07899, 1.921976, 1.949376, 1.824629, 1.851671, 1.744713, 1.765647, 1.683525, 1.685592, 1.625113, 1.624961, 1.571921, 1.581223, 1.535257, 1.537464, 1.497165, 1.504879, 1.468682, 1.469319, 1.448344, 2.092315, 1.941412, 1.969843, 1.844093, 1.866133, 1.766145, 1.783829, 1.703613, 1.709714, 1.646078, 1.654264, 1.594523, 1.598488, 1.545105, 1.555356, 1.514627, 1.521353, 1.483958, 1.487677, 1.449191, 1.459721, 1.958987, 1.985144, 1.87739, 1.879643, 1.786823, 1.799642, 1.720015, 1.724688, 1.663539, 1.662997, 1.609267, 1.615124, 1.56746, 1.562026, 1.520586, 1.52503, 1.493008, 1.502496, 1.471983, 1.468546, 1.435064, 1.994706, 1.880348, 1.894254, 1.805827, 1.815965, 1.744296, 1.743389, 1.665481, 1.681644, 1.624466, 1.626109, 1.584028, 1.5818, 1.54376, 1.547237, 1.504878, 1.515087, 1.479032, 1.47936, 1.450758, 1.45073, 1.892685, 1.91087, 1.825301, 1.827176, 1.745363, 1.746115, 1.693373, 1.701692, 1.648247, 1.637112, 1.594648, 1.592013, 1.554849, 1.55013, 1.522186, 1.520901, 1.492606, 1.493072, 1.460868, 1.46733, 1.440956, 1.92771, 1.835696, 1.841979, 1.775991, 1.766092, 1.703807, 1.708791, 1.654985, 1.655917, 1.602388, 1.611867, 1.570765, 1.573368, 1.53419, 1.529033, 1.506767, 1.503596, 1.481126, 1.471806, 1.444917, 1.451682, 1.850262, 1.855034, 1.778997, 1.789995, 1.718871, 1.717326, 1.667357, 1.666291, 1.619743, 1.631475, 1.582624, 1.58766, 1.546302, 1.545063, 1.512222, 1.517888, 1.489127, 1.487271, 1.466722, 1.463618, 1.444137, 1.8709, 1.794033, 1.80121, 1.736376, 1.740201, 1.673776, 1.682541, 1.638153, 1.642294, 1.604417, 1.597721, 1.559534, 1.559108, 1.533942, 1.529348, 1.499517, 1.501586, 1.473147, 1.473031, 1.457615, 1.452348, 1.805753, 1.812952, 1.746549, 1.747222, 1.696924, 1.694957, 1.652157, 1.650568, 1.607807, 1.613666, 1.577295, 1.570712, 1.543704, 1.538272, 1.515369, 1.517113, 1.487451, 1.491593, 1.464514, 1.464658, 1.439359, 1.823222, 1.758781, 1.767358, 1.70872, 1.712926, 1.666956, 1.667838, 1.62077, 1.621445, 1.592891, 1.58549, 1.55603, 1.559042, 1.521501, 1.523342, 2, 3, 4)634635636637