rr-markdownknitrhtmltoolssummarytools

Rmarkdown - printing a list of objects without showing those pesky [[indices]]


This will be a little wordy, as without proper context I cannot see how I can ask my question.

Without going too much into details, in my summarytools package, I have dealt with by() objects by using a wrapper function for print.summarytools, namely view(). This view() function can identify objects created through by or lapply(), and dispatch its components to the package's print(), with appropriate arguments relating to headings and footnotes, and so on (the first element will not receive the same arguments as the second, nor the last).

Link to package dev page:

The package offers two ways of rendering content: rmarkdown through pander, and HTML with RStudio's htmltools. As far as rmarkdown/pander is concerned, I have achieved satisfying results. The same cannot be said about the HTML rendering inside rmarkdown documents.

For example:

library(devtools)
install_github("dcomtois/summarytools", ref = "dev-current")
library(summarytools)

# call the descr() function through by() to get stats by gender
groupstats <- by(data = exams, INDICES = exams$gender, FUN = descr)

# Use the view() function to neatly display results
view(groupstats, method = 'render')

This example is available as a Gist on github.

Results after knitting:

rendered html content

I have tried several things, none satisfactory. Since the objects returned by summarytool's print method are of class shinytag, the (maybe) ideal solution would be to combine all of those into one list of class shinytag, but I'm not aware of any way to do that with htmltools. And handpicking the list elements appears to me as a recipe for trouble, since the is a whole lot of list-nesting in there.

I tried lapply(groupstats, print, method = 'render'), but then instead of having [[n]]'s, I have $names showing up.

So my question is: How can I possibly get rid of the [[n]]'s in the output?

Package source code For the relavant chuck of the package source code, see in summarytool's dev-current branch the R/view.R file, line ~ 78 -116.


Solution

  • If you have a list() of items that will print as HTML, you can put them together using htmltools::tagList() and the indices of the list won't show. For example,

    library(htmltools)
    thelist <- list(a, b)
    

    might display with the [1] and [2] as in your example, but

    tagList(thelist)
    

    or

    tagList(a, b)
    

    will just display the two items.