Closed dbsamson closed 8 years ago
This is actually an R thing dealing with implicit vs explicit printing in for
loops, but gets more complicated with the asis
environment and viewer panes, etc. To be honest, I don't entirely understand why R behaves the way it does here, but I am familiar with how to work around it.
The code below builds an HTML document with Rmarkdown that will hopefully illustrate the behavior and help you get the output you want.
---
title: "Untitled"
author: "Benjamin Nutter"
date: "January 18, 2016"
output: html_document
---
## Original Sample Code
```{r}
testTable <- function() {
library(pixiedust)
dust(data.frame(matrix(rnorm(20,4),ncol = 4))) %>%
sprinkle(rows = 2, bg = "purple" ) %>%
sprinkle(border="all",halign="left",valign="top") %>%
medley_bw() %>%sprinkle_print_method("html")
}
for (i in 1:3 ) {
testTable()
}
## knitr::kable
It turns out that `knitr::kable` illustrates the same behavior
```{r}
for (i in 1:3){
knitr::kable(data.frame(matrix(rnorm(20,4),ncol = 4)))
}
## Printing to the console
Even an implicit print to the console in a `for` loop hides the output.
```{r}
testTable <- function() {
library(pixiedust)
dust(data.frame(matrix(rnorm(20,4),ncol = 4))) %>%
sprinkle(rows = 2, bg = "purple" ) %>%
sprinkle(border="all",halign="left",valign="top") %>%
medley_bw() %>%
sprinkle_print_method("console")
}
for (i in 1:3 ) {
testTable()
}
## Using Explicit Printing to the console
You can force the output to show up in the console with an explicit print.
```{r}
testTable <- function() {
library(pixiedust)
dust(data.frame(matrix(rnorm(20,4),ncol = 4))) %>%
sprinkle(rows = 2, bg = "purple" ) %>%
sprinkle(border="all",halign="left",valign="top") %>%
medley_bw() %>%
sprinkle_print_method("console")
}
for (i in 1:3 ) {
testTable() %>%
print()
}
## Getting your output to print in Rmarkdown
Because `pixiedust` calls `knitr::asis_output`, the explicit print is a little less straightforward. If you're wanting to produce the output in a document, you'll need to feed the actual character string generated by `pixiedust` through `cat`. This applies to markdown, html, and latex formats.
1. Make sure you set the chunk option `results = 'asis'`
2. Use an explicit print with `asis = FALSE`
3. Send your results through `cat`
```{r, results = 'asis'}
library(pixiedust)
testTable <- function() {
library(pixiedust)
dust(data.frame(matrix(rnorm(20,4),ncol = 4))) %>%
sprinkle(rows = 2, bg = "purple" ) %>%
sprinkle(border="all",halign="left",valign="top") %>%
medley_bw() %>%sprinkle_print_method("html")
}
for (i in 1:3 ) {
testTable() %>%
print(asis = FALSE) %>%
cat()
}
It seems odd, but the code below works well, with the repeated calls to "testTable". If I use the for loop instead, nothing prints. This was isolated from a larger report, which saw the same scenario.
Pixiedust 0.6.0 Windows 8.1 Pro RStudio 0.99.489 R 3.2.3