Search code examples
rpipeflextable

flextable package assigns changes without <-


I'm not sure if this is the correct forum to post this, but I have noticed some strange behavior with the flextable package in R, and was wondering if anyone can shed any light.

In the documentation for flextable it shows objects being modified when they are re-assigned to themselves, eg:

ft <- regulartable(head(iris)) 
ft <- color(ft, color = "orange", part = "body" )

However, my code is modifying the actual table even without re-assigning it, just using piping %>%:

myft <- regulartable(head(iris))
myft %>% align(j = 1, align = "left")
myft    # changed

I don't think piping is the issue as it doesn't have the same effect with other packages, eg:

library(plyr)
df <- head(iris)
df %>% mutate(Sum=Sepal.Width*2)
df    # unchanged

Is this a bug in flextable? Or is this by design?


Solution

  • It's true that you can mutate formats without assigning the object. But that's not a behavior you can rely on. This is an unwanted design ;) and should be corrected in the next versions so it is safer to assign the result if you want your code to work with future versions.