Skip to content

Wrong indentation involving ~? #970

Open

Description

@gavinsimpson

Is this the expected behaviour (with styler::tidyverse_style(scope = "indention"))?

txt <- '
m2 <- gam(list(max_temp ~ s(month, bs = "cc", k = 12) + s(year) +
                            ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
                        ~ s(month, bs = "cc", k = 12),
                        ~ s(month, bs = "cc", k = 12) + s(year)),
          method = "REML",
          data = month_maxima,
          family = gevlss(link = list("identity", "identity", "logit")),
          knots = knots,
          control = ctrl,
          optimizer = "efs")'
style_text(txt, scope = "indention", indent_by = 2)

output:


m2 <- gam(list(max_temp ~ s(month, bs = "cc", k = 12) + s(year) +
  ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
~ s(month, bs = "cc", k = 12),
~ s(month, bs = "cc", k = 12) + s(year)),
method = "REML",
data = month_maxima,
family = gevlss(link = list("identity", "identity", "logit")),
knots = knots,
control = ctrl,
optimizer = "efs")

I would have expected something like

m2 <- gam(list(max_temp ~ s(month, bs = "cc", k = 12) + s(year) +
    ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
    ~ s(month, bs = "cc", k = 12),
    ~ s(month, bs = "cc", k = 12) + s(year)),
  method = "REML",
  data = month_maxima,
  family = gevlss(link = list("identity", "identity", "logit")),
  knots = knots,
  control = ctrl,
  optimizer = "efs")

or (perhaps preferably - ideally?)

m2 <- gam(list(max_temp ~ s(month, bs = "cc", k = 12) + s(year) +
                          ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
                        ~ s(month, bs = "cc", k = 12),
                        ~ s(month, bs = "cc", k = 12) + s(year)),
  method = "REML",
  data = month_maxima,
  family = gevlss(link = list("identity", "identity", "logit")),
  knots = knots,
  control = ctrl,
  optimizer = "efs")

where the tildes are aligned with one another

If that's not the expected indentation, is there a way to stop the reindention associated with the ~? Assuming that's what is causing the failure to indent subsequent arguments 2 spaces as per the tidyverse style guide?

session info
─ Session info ────────────────────────────────────────────
 setting  value
 version  R version 4.2.1 (2022-06-23)
 os       Ubuntu 20.04.4 LTS
 system   x86_64, linux-gnu
 ui       X11
 language en_GB:en
 collate  en_GB.UTF-8
 ctype    en_GB.UTF-8
 tz       Europe/Copenhagen
 date     2022-07-29
 pandoc   2.18 @ /opt/quarto/bin/pandoc

─ Packages ────────────────────────────────────────────────
package * version date (UTC) lib source
cli 3.3.0 2022-04-25 [1] RSPM (R 4.2.0)
jsonlite 1.8.0 2022-02-22 [1] RSPM (R 4.2.0)
rlang 1.0.4 2022-07-12 [1] RSPM (R 4.2.1)
sessioninfo 1.2.2 2021-12-06 [1] RSPM (R 4.2.0)

[1] /home/au690221/R/x86_64-pc-linux-gnu-library/4.2
[2] /usr/local/lib/R/site-library
[3] /usr/lib/R/site-library
[4] /usr/lib/R/library

───────────────────────────────────────────────────────────

Activity

  1. gavinsimpson commented on Jul 29, 2022

    @gavinsimpson
    Author

    Just to focus in on the ~ indentation. This illustrates the intended (?) behaviour with simpler code

    txt <- '
    f <- list(max_temp ~ s(month, bs = "cc", k = 12) + s(year) + ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
                       ~ s(month, bs = "cc", k = 12),
                       ~ s(month, bs = "cc", k = 12) + s(year))
    '
    style_text(txt, scope = "indention")

    producing

    f <- list(max_temp ~ s(month, bs = "cc", k = 12) + s(year) + ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
      ~ s(month, bs = "cc", k = 12),
      ~ s(month, bs = "cc", k = 12) + s(year))
    
    

    but as soon as we break up the long first formula with a line break, the indentation breaks

    txt <- '
    f <- list(max_temp ~ s(month, bs = "cc", k = 12) + s(year) +
      ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
                       ~ s(month, bs = "cc", k = 12),
                       ~ s(month, bs = "cc", k = 12) + s(year))
    '
    style_text(txt, scope = "indention")

    yielding

    f <- list(max_temp ~ s(month, bs = "cc", k = 12) + s(year) +
      ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
    ~ s(month, bs = "cc", k = 12),
    ~ s(month, bs = "cc", k = 12) + s(year))
    
  2. lorenzwalthert commented on Jul 29, 2022

    @lorenzwalthert
    Collaborator

    Ok. Probably not intended but since most people don’t use a non-default scope value, it has not been detected…

  3. gavinsimpson commented on Aug 1, 2022

    @gavinsimpson
    Author

    @lorenzwalthert Fair enough, but the first example I showed doesn't work as expected with the defaults either:

    txt <- '
    m2 <- gam(list(max_temp ~ s(month, bs = "cc", k = 12) + s(year) +
                                ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
                            ~ s(month, bs = "cc", k = 12),
                            ~ s(month, bs = "cc", k = 12) + s(year)),
              method = "REML",
              data = month_maxima,
              family = gevlss(link = list("identity", "identity", "logit")),
              knots = knots,
              control = ctrl,
              optimizer = "efs")'
    style_text(txt, scope = "tokens")

    yielding

    m2 <- gam(list(
      max_temp ~ s(month, bs = "cc", k = 12) + s(year) +
        ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
      ~ s(month, bs = "cc", k = 12),
      ~ s(month, bs = "cc", k = 12) + s(year)
    ),
    method = "REML",
    data = month_maxima,
    family = gevlss(link = list("identity", "identity", "logit")),
    knots = knots,
    control = ctrl,
    optimizer = "efs"
    )
    
  4. lorenzwalthert commented on Aug 1, 2022

    @lorenzwalthert
    Collaborator

    True, then let's fix that as well 😉

  5. lorenzwalthert commented on Aug 14, 2022

    @lorenzwalthert
    Collaborator

    Actually not related to ~, seems like only happens to unnamed arguments:

    txt <- '
    test_that(key(
                            s),
              x = 1)'
    styler::style_text(txt, scope = "tokens")
    #> 
    #> test_that(key(
    #>   s
    #> ),
    #> x = 1
    #> )
    txt <- 'test_that(key(
     1), 
    1)'
    styler::style_text(txt)
    #> test_that(
    #>   key(
    #>     1
    #>   ),
    #>   1
    #> )

    Created on 2022-08-14 by the reprex package (v2.0.1)

  6. gavinsimpson commented on Aug 25, 2022

    @gavinsimpson
    Author

    That's great; thanks!

  7. gavinsimpson commented on Feb 7, 2024

    @gavinsimpson
    Author

    As you mentioned that the reported behaviour in the original issue was "probably not intended", I note that the reported behaviour remains in

    > packageVersion("styler")                                                    
    [1] ‘1.10.2.9000’
    

    With

    txt <- '
    m2 <- gam(list(max_temp ~ s(month, bs = "cc", k = 12) + s(year) +
                                ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
                            ~ s(month, bs = "cc", k = 12),
                            ~ s(month, bs = "cc", k = 12) + s(year)),
              method = "REML",
              data = month_maxima,
              family = gevlss(link = list("identity", "identity", "logit")),
              knots = knots,
              control = ctrl,
              optimizer = "efs")'
    style_text(txt, scope = "indention", indent_by = 2)

    I'm still seeing

    m2 <- gam(list(max_temp ~ s(month, bs = "cc", k = 12) + s(year) +
      ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
    ~ s(month, bs = "cc", k = 12),
    ~ s(month, bs = "cc", k = 12) + s(year)),
    method = "REML",
    data = month_maxima,
    family = gevlss(link = list("identity", "identity", "logit")),
    knots = knots,
    control = ctrl,
    optimizer = "efs")
    

    whereas with scope = "tokens" what I think is the desirable formatting is achieved:

    m2 <- gam(
      list(
        max_temp ~ s(month, bs = "cc", k = 12) + s(year) +
          ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
        ~ s(month, bs = "cc", k = 12),
        ~ s(month, bs = "cc", k = 12) + s(year)
      ),
      method = "REML",
      data = month_maxima,
      family = gevlss(link = list("identity", "identity", "logit")),
      knots = knots,
      control = ctrl,
      optimizer = "efs"
    )
    

    The simpler version I mentioned also doesn't indent nicely with scope = "indentation":

    txt <- '
    f <- list(max_temp ~ s(month, bs = "cc", k = 12) + s(year) +
      ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
                       ~ s(month, bs = "cc", k = 12),
                       ~ s(month, bs = "cc", k = 12) + s(year))
    '
    style_text(txt, scope = "indention")

    yields

    f <- list(max_temp ~ s(month, bs = "cc", k = 12) + s(year) +
      ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
    ~ s(month, bs = "cc", k = 12),
    ~ s(month, bs = "cc", k = 12) + s(year))
    
    session info:

    Output from sessioninfo::session_info()

    ─ Session info ──────────────────────────────────────────────────────────────────────────────────────
    setting  value
    version  R version 4.3.2 (2023-10-31)
    os       Ubuntu 20.04.6 LTS
    system   x86_64, linux-gnu
    ui       X11
    language en_GB:en
    collate  en_GB.UTF-8
    ctype    en_GB.UTF-8
    tz       Europe/Copenhagen
    date     2024-02-07
    pandoc   2.5 @ /usr/bin/ (via rmarkdown)
    
    ─ Packages ──────────────────────────────────────────────────────────────────────────────────────────
    !  package      * version     date (UTC) lib source
      abind          1.4-5       2016-07-21 [1] RSPM (R 4.3.0)
      ADGofTest      0.3         2011-12-28 [1] RSPM (R 4.3.0)
      boot           1.3-28.1    2022-11-22 [1] RSPM (R 4.3.0)
      brio           1.1.4       2023-12-10 [1] RSPM (R 4.3.2)
      bslib          0.6.1       2023-11-28 [1] RSPM (R 4.3.2)
      cachem         1.0.8       2023-05-01 [1] RSPM (R 4.3.0)
      callr          3.7.3       2022-11-02 [1] RSPM (R 4.3.0)
      class          7.3-22      2023-05-03 [1] RSPM (R 4.3.0)
      classInt       0.4-10      2023-09-05 [1] RSPM (R 4.3.1)
      cli            3.6.2       2023-12-11 [1] RSPM (R 4.3.2)
      clipr          0.8.0       2022-02-22 [1] RSPM (R 4.3.0)
      colorspace     2.1-0       2023-01-23 [1] RSPM (R 4.3.0)
      copula         1.1-3       2023-12-07 [1] RSPM (R 4.3.2)
      crayon         1.5.2       2022-09-29 [1] RSPM (R 4.3.0)
      curl           5.2.0       2023-12-08 [1] RSPM (R 4.3.2)
      DBI            1.2.1       2024-01-12 [1] RSPM (R 4.3.2)
      desc           1.4.3       2023-12-10 [1] RSPM (R 4.3.2)
      devtools     * 2.4.5       2022-10-11 [1] RSPM (R 4.3.1)
      diffviewer     0.1.1       2021-09-30 [1] RSPM (R 4.3.0)
      digest         0.6.34      2024-01-11 [1] RSPM (R 4.3.2)
      dplyr        * 1.1.4       2023-11-17 [1] CRAN (R 4.3.2)
      e1071          1.7-14      2023-12-06 [1] RSPM (R 4.3.2)
      ellipsis       0.3.2       2021-04-29 [1] RSPM (R 4.3.0)
      evaluate       0.23        2023-11-01 [1] RSPM (R 4.3.2)
      evd            2.3-6.1     2022-07-04 [1] RSPM (R 4.3.0)
      fansi          1.0.6       2023-12-08 [1] RSPM (R 4.3.2)
      farver         2.1.1       2022-07-06 [1] RSPM (R 4.3.0)
      fastmap        1.1.1       2023-02-24 [1] RSPM (R 4.3.0)
      forcats        1.0.0       2023-01-29 [1] RSPM (R 4.3.0)
      fs             1.6.3       2023-07-20 [1] RSPM (R 4.3.1)
      gamlss.dist    6.1-1       2023-08-23 [1] RSPM (R 4.3.1)
      gamm4        * 0.2-6       2020-04-03 [1] RSPM (R 4.3.0)
      generics       0.1.3       2022-07-05 [1] RSPM (R 4.3.0)
      ggokabeito     0.1.0       2021-10-18 [1] RSPM (R 4.3.0)
      ggplot2      * 3.5.0       2024-02-07 [1] Github (tidyverse/ggplot2@fa6d68d)
      GJRM         * 0.2-6.5     2024-01-25 [1] RSPM (R 4.3.2)
      glue           1.7.0       2024-01-09 [1] RSPM (R 4.3.2)
      gmp            0.7-4       2024-01-15 [1] RSPM (R 4.3.2)
    VP gratia       * 0.8.9.4     2024-02-03 [?] load_all() (on disk 0.8.9.3)
      gsl            2.1-8       2023-01-24 [1] RSPM (R 4.3.0)
      gtable         0.3.4       2023-08-21 [1] RSPM (R 4.3.1)
      hms            1.1.3       2023-03-21 [1] RSPM (R 4.3.0)
      htmltools      0.5.7       2023-11-03 [1] RSPM (R 4.3.2)
      htmlwidgets    1.6.4       2023-12-06 [1] RSPM (R 4.3.2)
      httpgd         1.3.1       2023-01-30 [1] RSPM (R 4.3.0)
      httpuv         1.6.14      2024-01-26 [1] RSPM (R 4.3.2)
      ismev          1.42        2018-05-10 [1] RSPM (R 4.3.0)
      isoband        0.2.7       2022-12-20 [1] RSPM (R 4.3.0)
      jquerylib      0.1.4       2021-04-26 [1] RSPM (R 4.3.0)
      jsonlite       1.8.8       2023-12-04 [1] RSPM (R 4.3.2)
      KernSmooth     2.23-22     2023-07-10 [1] RSPM (R 4.3.1)
      knitr          1.45        2023-10-30 [1] RSPM (R 4.3.2)
      labeling       0.4.3       2023-08-29 [1] RSPM (R 4.3.1)
      later          1.3.2       2023-12-06 [1] RSPM (R 4.3.2)
      lattice        0.22-5      2023-10-24 [1] RSPM (R 4.3.2)
      lifecycle      1.0.4       2023-11-07 [1] RSPM (R 4.3.2)
      lme4         * 1.1-35.1    2023-11-05 [1] RSPM (R 4.3.2)
      magic          1.6-1       2022-11-16 [1] RSPM (R 4.3.0)
      magrittr       2.0.3       2022-03-30 [1] RSPM (R 4.3.0)
      mapproj        1.2.11      2023-01-12 [1] RSPM (R 4.3.0)
      maps           3.4.2       2023-12-15 [1] RSPM (R 4.3.2)
      MASS           7.3-60.0.1  2024-01-13 [1] RSPM (R 4.3.2)
      Matrix       * 1.6-5       2024-01-11 [1] RSPM (R 4.3.2)
      matrixStats    1.2.0       2023-12-11 [1] RSPM (R 4.3.2)
      memoise        2.0.1       2021-11-26 [1] RSPM (R 4.3.0)
      mgcv         * 1.9-1       2023-12-21 [1] RSPM (R 4.3.2)
      mime           0.12        2021-09-28 [1] RSPM (R 4.3.0)
      miniUI         0.1.1.1     2018-05-18 [1] RSPM (R 4.3.0)
      minqa          1.2.6       2023-09-11 [1] RSPM (R 4.3.1)
      mitools        2.4         2019-04-26 [1] RSPM (R 4.3.0)
      mnormt         2.1.1       2022-09-26 [1] RSPM (R 4.3.0)
      munsell        0.5.0       2018-06-12 [1] RSPM (R 4.3.0)
      mvnfast        0.2.8       2023-02-23 [1] RSPM (R 4.3.0)
      mvtnorm        1.2-4       2023-11-27 [1] RSPM (R 4.3.2)
      nlme         * 3.1-164     2023-11-27 [1] RSPM (R 4.3.2)
      nloptr         2.0.3       2022-05-26 [1] RSPM (R 4.3.0)
      numDeriv       2016.8-1.1  2019-06-06 [1] RSPM (R 4.3.0)
      patchwork      1.2.0       2024-01-08 [1] RSPM (R 4.3.2)
      pcaPP          2.0-4       2023-12-07 [1] RSPM (R 4.3.2)
      pillar         1.9.0       2023-03-22 [1] RSPM (R 4.3.0)
      pkgbuild       1.4.3       2023-12-10 [1] RSPM (R 4.3.2)
      pkgconfig      2.0.3       2019-09-22 [1] RSPM (R 4.3.0)
      pkgload        1.3.4       2024-01-16 [1] RSPM (R 4.3.2)
      processx       3.8.3       2023-12-10 [1] RSPM (R 4.3.2)
      profvis        0.3.8       2023-05-02 [1] RSPM (R 4.3.0)
      promises       1.2.1       2023-08-10 [1] RSPM (R 4.3.1)
      proxy          0.4-27      2022-06-09 [1] RSPM (R 4.3.0)
      ps             1.7.6       2024-01-18 [1] RSPM (R 4.3.2)
      pspline        1.0-19      2022-02-20 [1] RSPM (R 4.3.0)
      psych          2.4.1       2024-01-18 [1] RSPM (R 4.3.2)
      purrr          1.0.2       2023-08-10 [1] RSPM (R 4.3.1)
      R.cache        0.16.0      2022-07-21 [1] RSPM (R 4.3.0)
      R.methodsS3    1.8.2       2022-06-13 [1] RSPM (R 4.3.0)
      R.oo           1.26.0      2024-01-24 [1] RSPM (R 4.3.2)
      R.utils        2.12.3      2023-11-18 [1] CRAN (R 4.3.2)
      R6             2.5.1       2021-08-19 [1] RSPM (R 4.3.0)
      RColorBrewer   1.1-3       2022-04-03 [1] RSPM (R 4.3.0)
      Rcpp           1.0.12      2024-01-09 [1] RSPM (R 4.3.2)
      readr          2.1.5       2024-01-10 [1] RSPM (R 4.3.2)
      remotes        2.4.2.1     2023-07-18 [1] RSPM (R 4.3.1)
      reprex         2.1.0       2024-01-11 [1] RSPM (R 4.3.2)
      rlang          1.1.3       2024-01-10 [1] RSPM (R 4.3.2)
      rmarkdown      2.25        2023-09-18 [1] RSPM (R 4.3.1)
      Rmpfr          0.9-5       2024-01-21 [1] RSPM (R 4.3.2)
      rprojroot      2.0.4       2023-11-05 [1] RSPM (R 4.3.2)
      rstudioapi     0.15.0      2023-07-07 [1] RSPM (R 4.3.1)
      sass           0.4.8       2023-12-06 [1] RSPM (R 4.3.2)
      scales         1.3.0       2023-11-28 [1] RSPM (R 4.3.2)
      scam         * 1.2-15      2024-01-25 [1] RSPM (R 4.3.2)
      sessioninfo    1.2.2       2021-12-06 [1] RSPM (R 4.3.0)
      sf             1.0-15      2023-12-18 [1] RSPM (R 4.3.2)
      shiny        * 1.8.0       2023-11-17 [1] CRAN (R 4.3.2)
      stabledist     0.7-1       2016-09-12 [1] RSPM (R 4.3.0)
      stringi        1.8.3       2023-12-11 [1] RSPM (R 4.3.2)
      stringr        1.5.1       2023-11-14 [1] RSPM (R 4.3.2)
      styler         1.10.2.9000 2024-02-07 [1] Github (r-lib/styler@775c399)
      survey         4.2-1       2023-05-03 [1] RSPM (R 4.3.0)
      survival       3.5-7       2023-08-14 [1] RSPM (R 4.3.1)
      systemfonts    1.0.5       2023-10-09 [1] RSPM (R 4.3.2)
      testthat     * 3.2.1       2023-12-02 [1] RSPM (R 4.3.2)
      tibble       * 3.2.1       2023-03-20 [1] CRAN (R 4.3.0)
      tidyr          1.3.1       2024-01-24 [1] RSPM (R 4.3.2)
      tidyselect     1.2.0       2022-10-10 [1] RSPM (R 4.3.0)
      trust          0.1-8       2020-01-10 [1] RSPM (R 4.3.0)
      tzdb           0.4.0       2023-05-12 [1] RSPM (R 4.3.0)
      units          0.8-5       2023-11-28 [1] RSPM (R 4.3.2)
      urlchecker     1.0.1       2021-11-30 [1] RSPM (R 4.3.0)
      usethis      * 2.2.2       2023-07-06 [1] RSPM (R 4.3.1)
      utf8           1.2.4       2023-10-22 [1] RSPM (R 4.3.1)
      vctrs          0.6.5       2023-12-01 [1] RSPM (R 4.3.2)
      vdiffr         1.0.7       2023-09-22 [1] CRAN (R 4.3.1)
      VGAM           1.1-9       2023-09-19 [1] RSPM (R 4.3.1)
      VineCopula     2.5.0       2023-07-10 [1] RSPM (R 4.3.1)
      viridisLite    0.4.2       2023-05-02 [1] RSPM (R 4.3.0)
      waldo          0.5.2       2023-11-02 [1] RSPM (R 4.3.2)
      withr          3.0.0       2024-01-16 [1] RSPM (R 4.3.2)
      xfun           0.41        2023-11-01 [1] RSPM (R 4.3.2)
      xtable         1.8-4       2019-04-21 [1] RSPM (R 4.3.0)
      yaml           2.3.8       2023-12-11 [1] RSPM (R 4.3.2)
    
    [1] /home/au690221/R/x86_64-pc-linux-gnu-library/4.3
    [2] /usr/local/lib/R/site-library
    [3] /usr/lib/R/site-library
    [4] /usr/lib/R/library
    
    V ── Loaded and on-disk version mismatch.
    P ── Loaded and on-disk path mismatch.
    
    ─────────────────────────────────────────────────────────────────────────────────────────────────────
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions