Why is R studio flagging an unexpected token error when combining pipe operator with rbind?

The following example code works fine, in counting the number of occurrences (of "Status_1" events subject to a "Status_2" constraint), creating a pivot table of those occurrences, and adding a totals row to the bottom of the pivot table:

library(dplyr)
library(tidyverse)

mydat <- 
  data.frame(
    Period = c(1, 2, 3, 1, 2, 3, 1, 2),
    ID = c(115,115,115,88,88,88,100,100),
    Status_1 = c('P01','P02','P03','P01','P02','P03','P01','P04'),
    Status_2 = c('Open','Open','Closed','Open','Open','Closed','Open','Open')
  )

counts <-
  mydat %>%
  group_by(Period,Status_1) %>%
  summarize(Status_1_cnt = n_distinct(ID[Status_2 == "Open"])) %>%
  ungroup()

mydatTable <- 
  pivot_wider(counts, 
              id_cols = Status_1, 
              names_from = Period, 
              values_from = Status_1_cnt, 
              values_fill = list(Status_1_cnt = 0)
              ) %>%
  bind_rows(summarise_all(., ~if(is.numeric(.)) sum(.) else "Total"))
mydatTable

> mydatTable
# A tibble: 5 x 4
  Status_1   `1`   `2`   `3`
  <chr>    <int> <int> <int>
1 P01          3     0     0
2 P02          0     2     0
3 P04          0     1     0
4 P03          0     0     0
5 Total        3     3     0

However, RStudio is flagging an "unexpected token" error in the rbind() row, as shown in the RStudio screenshot image at the bottom. I have researched this sort of error in both Google and Stack Overflow and there are many different flavors of this problem/solutions. If the solution is to update RStudio, all searches say "To update RStudio, just run RStudio, and go to the Help menu in the top menu bar (not the Help tab in the lower right quadrant). In the Help menu, select Check for Updates."...but my version of RStudio doesn't have "Check for Updates" in the Help menu.

Perhaps there's an issue with the rbind() line below and there's a better way to add a totals row?

Any suggestions for resolving this?

enter image description here


It looks as though RStudio is parsing the if incorrectly. Since if is just a function with special handling by the parser, you can write it in standard form and the warning goes away:

bind_rows(summarise_all(., ~ `if`(is.numeric(.), sum(.), "Total")))

Another fix is to put parens around the whole if call:

bind_rows(summarise_all(., ~(if(is.numeric(.)) sum(.) else "Total")))

And of course you could write your own function to handle it, and use that:

kluge <- function(x) 
  if (is.numeric(x)) sum(x) else "Total"

mydatTable <- 
  pivot_wider(counts, 
              id_cols = Status_1, 
              names_from = Period, 
              values_from = Status_1_cnt, 
              values_fill = list(Status_1_cnt = 0)
  ) %>%
  bind_rows(summarise_all(., ~kluge(.)))

I'd probably prefer to ignore the warning rather than use any of these fixes.