Search code examples
rdata.tablecode-formatting

Break data.table chain into two lines of code for readability


I'm working on a Rmarkdown document, and was told to strictly limit to a maximum number of columns (margin column) of 100. In the document's code chunks I used many different packages, among which is data.table.

In order to comply with the limit I can split chains (and even long commands) like:

p <- ggplot(foo,aes(bar,foo2))+
       geom_line()+
       stat_smooth()
bar <- sum(long_variable_name_here,
         na.rm=TRUE)
foo <- bar %>% 
         group_by(var) %>%
         summarize(var2=sum(foo2))

but I can't split a data.table chain, as it produces an error. How can I achieve something like this?

bar <- foo[,.(long_name_here=sum(foo2)),by=var]
           [order(-long_name_here)]

Last line, of course, causes an error. Thanks!


Solution

  • You have to give a return between the [ and ] of each line. An example for how to divide your data.table code over several lines:

    bar <- foo[, .(long_name_here = sum(foo2)), by = var
               ][order(-long_name_here)]
    

    You can also give a return before / after each comma. An example with a return before the comma (my preference):

    bar <- foo[, .(long_name_here = sum(foo2))
               , by = var
               ][order(-long_name_here)
                 , long_name_2 := long_name_here * 10]
    

    See this answer for an extended example