hughjonesd / huxtable

An R package to create styled tables in multiple output formats, with a friendly, modern interface.
http://hughjonesd.github.io/huxtable
Other
321 stars 28 forks source link

Output ignored when called in a for-loop #31

Closed stephenkirk closed 6 years ago

stephenkirk commented 7 years ago

Setting: I want to output a latex PDF document. Sometimes, I need to create an arbitrary number of tables – for example, to automatically split tables that become too long to fit on a page.

First things first:

hux(as_hux(data.frame(a = c("a","b","c"), b = c(1,2,3))))

Works as expected - the table appears. However, as soon as the hux() call is enclosed in some way, problems appear:

for (x in 1:5) 
{
    hux(as_hux(data.frame(a = c("a","b","c"), b = c(1,2,3))))
}

Doesn't work - none of the tables appear.

This seems similar to an issue that kable had (https://github.com/yihui/knitr/issues/886), but where the workaround was either an explicit print() or making sure that 'asis' is set.

Experimenting a bit, neither of the following work either:

for (x in 1:5)
{
    print(hux(as_hux(data.frame(a = c("a","b","c"), b = c(1,2,3)))))
}
# Generate five data frames in a list
dfs <- lapply(1:5, function(x) data.frame(a = c("a","b","c"), b = c(1,2,3)))

for (x in 1:length(dfs))
{
  hux(as_hux(dfs[[x]]))
}

But interestingly enough, this works:

# Generate five data frames
dfs <- lapply(1:5, function(x) data.frame(a = c("a","b","c"), b = c(1,2,3)))

hux(as_hux(dfs[[1]]))
hux(as_hux(dfs[[2]]))
hux(as_hux(dfs[[3]]))
hux(as_hux(dfs[[4]]))
hux(as_hux(dfs[[5]]))

I've also experimented with calling with map() in the purrr package without any success.

Problem occured both with the 0.3.1 version of huxtable, as well as 1.0.0.9. Attached is my sessioninfo:

> sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-apple-darwin17.0.0 (64-bit)
Running under: macOS High Sierra 10.13

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] huxtable_1.0.0.9000

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.13.1   assertthat_0.2.0 digest_0.6.12    dplyr_0.7.4     
 [5] rprojroot_1.2    R6_2.2.2         backports_1.1.0  magrittr_1.5    
 [9] evaluate_0.10.1  rlang_0.1.2      stringi_1.1.5    bindrcpp_0.2    
[13] rmarkdown_1.6    tools_3.4.2      stringr_1.2.0    glue_1.1.1      
[17] yaml_2.1.14      compiler_3.4.2   pkgconfig_2.0.1  htmltools_0.3.6 
[21] bindr_0.1        knitr_1.17       tibble_1.3.4  

I've tried to track this issue down a bit, but I'm not fully comfortable with the internals of huxtable and it'd take me a good while to figure it out. However, I figure that I should report it in case this is a known problem with a simple answer.

Beyond this, I'd like to thank you for building such a complete lifesaver of a package.

hughjonesd commented 7 years ago

hux(as_hux(...)) is redundant, just call as_hux.

Yes, it's an autoprinting issue. Cf. ggplot. I'm not sure why print() doesn't work though. Try doing print_latex in your for loop. Does that work?

hughjonesd commented 7 years ago

You will probably also need to set "results = 'asis'" in the knitr header

David

On 19 October 2017 at 12:28, David Hugh-Jones davidhughjones@gmail.com wrote:

hux(as_hux(...)) is redundant, just call as_hux.

Yes, it's an autoprinting issue. Cf. ggplot. I'm not sure why print() doesn't work though. Try doing print_latex in your for loop. Does that work?

danwwilson commented 6 years ago

I'm having a similar problem with a for loop. I've tried the print_latex() and the making the rmarkdown section results='asis' however it does create strange behaviours.

  1. It creates a sizeable indentation from the left
  2. No headers are included in the output.
  3. without a preliminary huxtable kitting the document throws latex errors for tabularx

The following is some reprex rmarkdown content to trial.

---
title: "Test"
output: 
  pdf_document:
    keep_tex: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

library(magrittr)
library(dplyr)
library(huxtable)

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

first huxtable


ht <- hux(
        Employee = c('John Smith', 'David Hugh-Jones'),
        Salary = c(50000, 50000),
        add_colnames = TRUE
      )

ht                                        %>%
      set_bold(1, everywhere, TRUE)       %>%
      set_bottom_border(1, everywhere, 1)

looping huxtable test


df1 <- data.frame("Col1" = c("A","A","B","B","C"),
                  "Description" = c("Lorem ipsum dolor sit", "blah", "more blah", "Ut enim ad minim", NA),
                  Some_Numbers = c(10, 1000, 1000000, 25000, 99999999),
                  more_numbers = c(4235, 345.345, 03575.34, 97534987534890, 78350))

for (i in LETTERS[1:3]) {
  tbl1 <- df1 %>% filter(Col1 == i)

  tbl2 <- tbl1 %>% summarise(Col1 = "Total",
                             Some_Numbers = sum(Some_Numbers),
                             more_numbers = sum(more_numbers))

  tbl3 <- bind_rows(tbl1, tbl2) %>% 
    select(Col1, Description, "Some Numbers" = Some_Numbers, more_numbers)

  cat("\n## Year", i, "\n Data for filtered records: ", i, "\n")
  print_latex(as_hux(tbl3))
  cat("\n")
}
hughjonesd commented 6 years ago

ah, yes. The automatic setting of headers only occurs when the huxtable is directly outputted via its print method. You might try just calling print on it within the loop.

David

On 5 December 2017 at 02:08, Dan notifications@github.com wrote:

I'm having a similar problem with a for loop. I've tried the print_latex() and the making the rmarkdown section results='asis' however it does create strange behaviours.

  1. It creates a sizeable indentation from the left
  2. No headers are included in the output.
  3. without a preliminary huxtable kitting the document throws latex errors for tabularx

The following is some reprex rmarkdown content to trial.

---title: "Test"output: pdf_document: keep_tex: yes---


library(magrittr)
library(dplyr)
library(huxtable)

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

first huxtable

ht <- hux(
        Employee = c('John Smith', 'David Hugh-Jones'),
        Salary = c(50000, 50000),
        add_colnames = TRUE
      )
ht                                        %>%
      set_bold(1, everywhere, TRUE)       %>%
      set_bottom_border(1, everywhere, 1)

looping huxtable test

df1 <- data.frame("Col1" = c("A","A","B","B","C"),
                  "Description" = c("Lorem ipsum dolor sit", "blah", "more blah", "Ut enim ad minim", NA),
                  Some_Numbers = c(10, 1000, 1000000, 25000, 99999999),
                  more_numbers = c(4235, 345.345, 03575.34, 97534987534890, 78350))
for (i in LETTERS[1:3]) {
  tbl1 <- df1 %>% filter(Col1 == i)

  tbl2 <- tbl1 %>% summarise(Col1 = "Total",
                             Some_Numbers = sum(Some_Numbers),
                             more_numbers = sum(more_numbers))

  tbl3 <- bind_rows(tbl1, tbl2) %>%
    select(Col1, Description, "Some Numbers" = Some_Numbers, more_numbers)

  cat("\n## Year", i, "\n Data for filtered records: ", i, "\n")
  print_latex(as_hux(tbl3))
  cat("\n")
}

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/hughjonesd/huxtable/issues/31#issuecomment-349171943, or mute the thread https://github.com/notifications/unsubscribe-auth/AFjJ98eqE05AGWxvzxdKQQLcwh6oSvHdks5s9KW4gaJpZM4P_EpF .