STAT545-UBC / Discussion

Public discussion
38 stars 20 forks source link

Getting pander to render markdown #344

Open ksedivyhaley opened 7 years ago

ksedivyhaley commented 7 years ago

I'm getting pander output that looks like this:

continent gdp_pc_min gdp_pc_mean gdp_pc_max
Africa 241.1659 2193.755 21951.21
Americas 1201.6372 7136.110 42951.65
Asia 331.0000 7902.150 113523.13
Europe 973.5332 14469.476 49357.19
Oceania 10039.5956 18621.609 34435.37

This source says that pander's output is supposed to interpret markdown as long as knitr is running in the background. Should I be doing something other than simply calling library(knitr) or hitting the Knit button in rstudio to make sure that knitr is running?

jennybc commented 7 years ago

From skimming that document I wonder if you have your pander and/or chunk options set correctly? That seems to be important. Maybe post an actual example? It's hard to tell what's wrong from the above.

ksedivyhaley commented 7 years ago

Setup line created by RStudio:

knitr::opts_chunk$set(echo = TRUE)

Load packages, settings:

suppressPackageStartupMessages(library(tidyverse))
library(gapminder)
library(knitr)
library(pander)
panderOptions('round', 2)
panderOptions('keep.trailing.zeros', TRUE)
panderOptions('knitr.auto.asis', TRUE) #this is what the page described

Create table:

gapminder %>%
  group_by(continent) %>%
  summarise(gdp_pc_min = min(gdpPercap),
            gdp_pc_mean = mean(gdpPercap),
            gdp_pc_max = max(gdpPercap)) %>%
  pandoc.table(style = "rmarkdown")

I also tried looking at the actual md document and noticed that the table is being produced inside a code chunk (without an {r} tag, commented out). which explains why markdown doesn't think it should be formatted, but not why it's showing up inside a chunk and the kable() tables aren't.

samhinshaw commented 7 years ago

I tried this out and I got pandoc.table() to output the table, but treated as a console output. However, with pander() it rendered properly in the markdown output.

gapminder %>%
  group_by(continent) %>%
  summarise(gdp_pc_min = min(gdpPercap),
            gdp_pc_mean = mean(gdpPercap),
            gdp_pc_max = max(gdpPercap)) %>%
  pander(style = "rmarkdown")

output:

continent gdp_pc_min gdp_pc_mean gdp_pc_max
Africa 241.2 2194 21951
Americas 1201.6 7136 42952
Asia 331.0 7902 113523
Europe 973.5 14469 49357
Oceania 10039.6 18622 34435

Is this what you were looking for, or did I misinterpret your question?


In summary: tables

ksedivyhaley commented 7 years ago

That's exactly it, thanks!

I started with this documentation which uses pandoc.table() and displayed tables as console output but missed the fact that the second one with the nice output switched to pander().