pheymanss / chronicle

R package to create R Markdown reports with a sequential syntax inspired by ggplot
29 stars 4 forks source link

Chaining/piping code in add_code #43

Open systemnova opened 2 years ago

systemnova commented 2 years ago

Firstly, thankyou for making chronicle it has had a massive impact on how I work in R. Chronicle is so much more convenient, easy to understand and elegant in comparison to any other report generation approach I've tried.

There's probably an obvious solution, but I was wondering if it's possible to chain or pipe multiple separate functions into add_code.

eg. rp1 %<>% add_code( code = quo( c(reactable(table1), reactable(table2))))

pheymanss commented 2 years ago

Thank you very much for the encouraging feedback!

I am not sure if I understand the question. If you mean having several expressions built into a single chunk through a single add_code, call, it is quite straightforward since the code parameter is designed to receive a single character string.

So, for example, this

chronicle::add_code(report = '', 
                     code = 'reactable(table1)
reactable(table2)')

returns this:

```{r, eval=TRUE, warning=FALSE, message=FALSE, echo=TRUE, fig.width=params$figure_width, 
fig.height=params$figure_height}
reactable(table1)
reactable(table2)
```   

If you wanted to do it programmatically though, there is a big flaw in the design of add_code in which it currently cannot handle a character vector, in the sense that passing it a character vector will return a vector, and then you have N separate report templates, one with each entry of the vector you wanted to add (so in your example, you'd get two reports, one having table1 and another having table2)

This is a very large flaw that can be easily fixed, I will try to squeeze that into an update through the weekend. In the meantime, sadly, you'll have to manually collapse the vector into a single string, like so

chronicle::add_code(code = paste(
  c(deparse(substitute(reactable(table1))),
    deparse(substitute(reactable(table2)))),
   collapse = '\n')) 
systemnova commented 2 years ago

Awesome, thanks. Yes this was exactly the challenge I was facing, sorry for not expressing it well. I think I might have got this working with the qc() function from wrapr, as an alternative work around. I'm also experimenting with i2dash that seems quite similar to chronicle, but hitting different little bugs here and there. Might try later to see if I can use both packages to operate on the same .rmd. Would be awesome if chronicle could insert the separators for flexdashboard & supported bootstrap 5 themes at export. Thanks again for an awesomely useful package, really streamlines the reporting process.