nutterb / pixiedust

Tables So Beautifully Fine-Tuned You Will Believe It's Magic.
178 stars 18 forks source link

Table not outputting to rmarkdown looped output #85

Closed kgottstine closed 7 years ago

kgottstine commented 7 years ago

I used pixiedust to make a table to be used on a html file. The output of the table is perfect for what we are looking for and worked fine in test runs with one large dataset. The goal was to use a loop to create several html files from the same dataset but smaller subsets. All graphs that were created in the report print fine, but the pixiedust table outputs to the viewer pane and not to the html file itself. Have tried using print() around the table, saving the table and printing that, but neither have worked.

nutterb commented 7 years ago

This is one that could probably be documented better. Printing inside a for loop can be somewhat tricky. I find it works better to retrieve the HTML text of the table and run it through cat. To do this, your chunk option will need to include results = 'asis'. Here's an example:

```{r, results = 'asis'}
X <- split(mtcars, 
               mtcars$gear)

for (i in seq_along(X))
{
  dust(X[[i]]) %>%
    medley_bw() %>%
    sprinkle(bg = "lightblue") %>%
    print(asis = FALSE) %>%
    cat()
}

Another option, if your tables are mutually exclusive, is to use a list instead of a loop.  `dust` and `sprinkle` will apply any changes you make over the full list of dust objects and print them simultaneously.  (note, if you are doing captions, need text between tables, or group specific customizations, this can get a little harder to do)
library(pixiedust)

split(mtcars,
      mtcars$gear) %>%
  dust() %>%
  medley_bw() # medley_bw may not be properly vectorized


I hope one of those solutions works for you.
kgottstine commented 7 years ago

I guess my explanation might have been confusing. I have one rmarkdown file and one of the last chunks involves the following pixiedust table:

```{r SD and Mean Table, echo = FALSE, results = 'asis', warning = FALSE}
line.table[line.cols] %>%
  dust(justify = "left") %>%
  medley_all_borders(part = "table") %>%
  sprinkle_table(halign = "left", pad = 2.5) %>%
  sprinkle(rows = which(line.table$`Mean Pass/Fail` == "Fail"), col = "Mean Pass/Fail", 
           bg = "rgba(255, 00, 00, .7)") %>%
  sprinkle(rows = which(line.table$`Mean Pass/Fail` == "Pass"), col = "Mean Pass/Fail", 
           bg = "rgba(63, 255, 00, .7)")

This markdown file is saved and then called upon using the following code which creates subsets of a data set and runs it through the file to create the report:

for (jnum in unique(rawData$`J Number`)){
  data = subset(rawData, `J Number` == jnum)
  rmarkdown::render("jnum.loop.Rmd",
                    output_file =  paste("Report_", jnum, "_", Sys.Date(), ".html", sep="") 
  )
}

As this loop runs, the tables output to the viewer pane and not to the html file.

nutterb commented 7 years ago

Oh, I see. I think I've encountered something similar when calling rmarkdown::render from a Shiny Application. It seems like it should be in a new environment, but render actually runs in the interactive session, which causes it to print to the viewer. (Unless the for loop is being run non-interactively, in which case, I'm surprised at this behavior). I guess what I really need is an escape argument such as interactive = FALSE that will allow bypassing the viewer.

For now, the %>% print(asis = FALSE) %>% cat() addition to your code will produce your tables.

kgottstine commented 7 years ago

This worked, thank you very much!!