Rapporter / pander

An R Pandoc Writer: Convert arbitrary R objects into markdown
http://rapporter.github.io/pander/
Open Software License 3.0
294 stars 66 forks source link

Possible bug: panderOptions('round', 2) being lost when dplyr::group_by used #287

Closed brfry closed 7 years ago

brfry commented 7 years ago

Not sure if it is something I am overlooking, a problem with dplyr, or something with pander, but I am having this issue:

Load libraries and set options:

library(pander)
panderOptions('round', 2)
panderOptions('keep.trailing.zeros', TRUE)
panderOptions('table.split.table', Inf)

library(tidyverse)

Create some data:

set.seed(10)
df <- data.frame(x = rnorm(10), y = rnorm(10), class = c("a", "b"))

print(df)
x y class
0.01874617 1.10177950 a
-0.18425254 0.75578151 b
-1.37133055 -0.23823356 a
-0.59916772 0.98744470 b
0.29454513 0.74139013 a
0.38979430 0.08934727 b
-1.20807618 -0.95494386 a
-0.36367602 -0.19515038 b
-1.62667268 0.92552126 a
-0.25647839 0.48297852 b

Manipulate df and use pander without any group_by() operation:

# make a table and output data
df_nogroup <- df %>%
  mutate(xy = x * y) %>%
  summarise(mean = mean(xy, na.rm = TRUE),
            sd = sd(xy, na.rm = TRUE),
            se = sd(xy, na.rm = TRUE)/sqrt(n()),
            CI95_upr = mean + (qnorm(0.975) * se),
            CI95_lwr = mean - (qnorm(0.975) * se),
            n = n())

pander(df_nogroup, "No grouping step. Round working")

mean sd se CI95_upr CI95_lwr n
-0.05 0.68 0.21 0.37 -0.47 10

Table: No grouping step. Round working

Now with group_by():

df_group <- df %>%
  mutate(xy = x * y) %>%
  group_by(class) %>%
  summarise(mean = mean(xy, na.rm = TRUE),
            sd = sd(xy, na.rm = TRUE),
            se = sd(xy, na.rm = TRUE)/sqrt(n()),
            CI95_upr = mean + (qnorm(0.975) * se), 
            CI95_lwr = mean - (qnorm(0.975) * se),
            n = n()) 

pander(df_group, "Grouping appears to be the culprit")

class mean sd se CI95_upr CI95_lwr n
a 0.04277 0.9674 0.4326 0.89069 -0.8051 5
b -0.14979 0.2640 0.1181 0.08163 -0.3812 5

Table: Grouping appears to be the culprit

I tried adding a ungroup() to the end of the piping with no change.

daroczig commented 7 years ago

Cannot reproduce the bug here -- can you please share your devtools::session_info()?

brfry commented 7 years ago

Session info ------------------------------------------------------------------------------------------------------------- setting value
version R version 3.3.2 (2016-10-31) system x86_64, mingw32
ui RStudio (1.0.136)
language (EN)
collate English_United States.1252
tz America/New_York
date 2017-01-26

Packages ----------------------------------------------------------------------------------------------------------------- package version date source
assertthat 0.1 2013-12-06 CRAN (R 3.3.2) backports 1.0.5 2017-01-18 CRAN (R 3.3.2) base64enc 0.1-3 2015-07-28 CRAN (R 3.3.2) broom 0.4.1 2016-06-24 CRAN (R 3.3.1) coda 0.19-1 2016-12-08 CRAN (R 3.3.2) codetools 0.2-15 2016-10-05 CRAN (R 3.3.2) colorspace 1.3-2 2016-12-14 CRAN (R 3.3.2) DBI 0.5-1 2016-09-10 CRAN (R 3.3.2) devtools
1.12.0 2016-06-24 CRAN (R 3.3.2) digest 0.6.11 2017-01-03 CRAN (R 3.3.2) dplyr 0.5.0 2016-06-24 CRAN (R 3.3.2) estimability 1.2 2016-11-19 CRAN (R 3.3.2) evaluate 0.10 2016-10-11 CRAN (R 3.3.2) foreign 0.8-67 2016-09-13 CRAN (R 3.3.2) ggplot2 2.2.1 2016-12-30 CRAN (R 3.3.2) gtable 0.2.0 2016-02-26 CRAN (R 3.3.2) haven 1.0.0 2016-09-23 CRAN (R 3.3.2) hms 0.3 2016-11-22 CRAN (R 3.3.2) htmltools 0.3.5 2016-03-21 CRAN (R 3.3.2) httr 1.2.1 2016-07-03 CRAN (R 3.3.2) jsonlite 1.2 2016-12-31 CRAN (R 3.3.2) knitr 1.15.1 2016-11-22 CRAN (R 3.3.2) labeling 0.3 2014-08-23 CRAN (R 3.3.2) lattice 0.20-34 2016-09-06 CRAN (R 3.3.2) lazyeval 0.2.0 2016-06-12 CRAN (R 3.3.2) lme4 1.1-12 2016-04-16 CRAN (R 3.3.2) lsmeans 2.25 2016-11-19 CRAN (R 3.3.2) lubridate 1.6.0 2016-09-13 CRAN (R 3.3.2) magrittr 1.5 2014-11-22 CRAN (R 3.3.2) MASS 7.3-45 2016-04-21 CRAN (R 3.3.2) Matrix 1.2-7.1 2016-09-01 CRAN (R 3.3.2) memoise 1.0.0 2016-01-29 CRAN (R 3.3.2) minqa 1.2.4 2014-10-09 CRAN (R 3.3.2) mnormt 1.5-5 2016-10-15 CRAN (R 3.3.2) modelr 0.1.0 2016-08-31 CRAN (R 3.3.2) multcomp 1.4-6 2016-07-14 CRAN (R 3.3.2) munsell 0.4.3 2016-02-13 CRAN (R 3.3.2) mvtnorm 1.0-5 2016-02-02 CRAN (R 3.3.2) nlme 3.1-128 2016-05-10 CRAN (R 3.3.2) nloptr 1.0.4 2014-08-04 CRAN (R 3.3.2) packrat 0.4.8-1 2016-09-07 CRAN (R 3.3.2) pander 0.6.0 2015-11-23 CRAN (R 3.3.2) plyr 1.8.4 2016-06-08 CRAN (R 3.3.2) psych 1.6.12 2017-01-08 CRAN (R 3.3.2) purrr 0.2.2 2016-06-18 CRAN (R 3.3.2) R6 2.2.0 2016-10-05 CRAN (R 3.3.2) Rcpp 0.12.9 2017-01-14 CRAN (R 3.3.2) readr 1.0.0 2016-08-03 CRAN (R 3.3.2) readxl 0.1.1 2016-03-28 CRAN (R 3.3.2) reshape2 1.4.2 2016-10-22 CRAN (R 3.3.2) rmarkdown 1.3 2016-12-21 CRAN (R 3.3.2) rprojroot 1.2 2017-01-16 CRAN (R 3.3.2) rvest 0.3.2 2016-06-17 CRAN (R 3.3.2) sandwich 2.3-4 2015-09-24 CRAN (R 3.3.2) scales 0.4.1 2016-11-09 CRAN (R 3.3.2) stringi 1.1.2 2016-10-01 CRAN (R 3.3.2) stringr 1.1.0 2016-08-19 CRAN (R 3.3.2) survival 2.39-5 2016-06-26 CRAN (R 3.3.2) TH.data 1.0-8 2017-01-23 CRAN (R 3.3.2) tibble 1.2 2016-08-26 CRAN (R 3.3.2) tidyr 0.6.1 2017-01-10 CRAN (R 3.3.2) tidyverse 1.1.0 2017-01-20 CRAN (R 3.3.2) withr 1.0.2 2016-06-20 CRAN (R 3.3.2) xml2 1.1.1 2017-01-24 CRAN (R 3.3.2) xtable 1.8-2 2016-02-05 CRAN (R 3.3.2) yaml 2.1.14 2016-11-12 CRAN (R 3.3.2) zoo 1.7-14 2016-12-16 CRAN (R 3.3.2)

daroczig commented 7 years ago

Can you try the dev version of pander? It hasn't been updated on CRAN for a while, and I think this was fixed since then at #234. Based on this, I'm closing this now, but please feel free to reopen if that doesn't solve the issue.

brfry commented 7 years ago

That has fixed it. Works with both panderOptions global and inline. Thanks.