Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Headerless tables not formatted correctly #1893

Closed
3 tasks done
fkohrt opened this issue Sep 8, 2020 · 10 comments · Fixed by #1895
Closed
3 tasks done

Headerless tables not formatted correctly #1893

fkohrt opened this issue Sep 8, 2020 · 10 comments · Fixed by #1895
Assignees

Comments

@fkohrt
Copy link

fkohrt commented Sep 8, 2020

Pandoc's simple tables may or may not contain a header1. Headerless tables are, however, not formatted correctly by rmarkdown. The following document...

---
title: "Beheaded tables"
output: html_document
---

  Right     Left     Center     Default
-------     ------ ----------   -------
     12     12        12             12
    123     123       123           123
      1     1          1              1

Table: Simple table with header


-------     ------ ----------   -------
     12     12        12             12
    123     123       123           123
      1     1          1              1
-------     ------ ----------   -------

Table: Simple table without header

...renders to:

beheaded tables

I ran into this while attempting to write a table with a header column – for which a syntax is underway, but not available right now – and hence without a header row. My attempt to overcome rmarkdown's limitation is as follows (based on this solution by Jan):

---
title: "Patched up tables"
output: html_document
---

```{r, echo=FALSE}
table_header <- c("Title", "Author", "Version", "Last edit", "License", "Address", "Contact")
table_body <- c("Headerless tables not formatted correctly",
                "fkohrt", "1.0.0", "2020-09-08", "CC0 1.0",
                "https://github.com/rstudio/rmarkdown/issues/1893",
                "https://github.com/fkohrt")

table_frame <- data.frame(table_body)
row.names(table_frame) <- table_header
table <- kableExtra::kable_styling(kableExtra::column_spec(knitr::kable(table_frame, format="html", caption="Not so simple table without header row"), 1, bold=TRUE), bootstrap_options = c("condensed"))
gsub("<thead>.*</thead>", "", table)
```

...which looks like this:

patched up tables

But I believe there's no reason to limit R Markdown to ... capitated tables.

xfun::session_info('rmarkdown')
R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.4 LTS, RStudio 1.2.5001

Locale:
  LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
  LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
  LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

Package version:
  base64enc_0.1.3 digest_0.6.25   evaluate_0.14   glue_1.4.2      graphics_3.6.3  grDevices_3.6.3 highr_0.8      
  htmltools_0.5.0 jsonlite_1.7.0  knitr_1.29      magrittr_1.5    markdown_1.1    methods_3.6.3   mime_0.9       
  rlang_0.4.7     rmarkdown_2.3.3 stats_3.6.3     stringi_1.4.6   stringr_1.4.0   tinytex_0.25    tools_3.6.3    
  utils_3.6.3     xfun_0.16       yaml_2.2.1     

Pandoc version: 2.10.1

1 as can multiline tables and grid tables, but not pipe tables


By filing an issue to this repo, I promise that

  • I have fully read the issue guide at https://yihui.org/issue/.
  • I have provided the necessary information about my issue.
    • If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    • If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included xfun::session_info('rmarkdown'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: remotes::install_github('rstudio/rmarkdown').
    • If I have posted the same issue elsewhere, I have also mentioned it in this issue.
  • I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.

@cderv
Copy link
Collaborator

cderv commented Sep 8, 2020

Thanks for the feedback ! I need some precision because I am not sure of what is the expected output. I tested with pandoc only and I got the same as with Rmarkdown

I put this in test.md

-------     ------ ----------   -------
     12     12        12             12
    123     123       123           123
      1     1          1              1
-------     ------ ----------   -------

Table: Simple table without header

And call

pandoc -f markdown -t html4 -o test.html test.md

I got this in test.html
image

Converting from rmarkdown

---
output: html_document
---

-------     ------ ----------   -------
     12     12        12             12
    123     123       123           123
      1     1          1              1
-------     ------ ----------   -------

Table: Simple table without header

gives me the same (with some styling minor difference)
image

Are you expecting something else ?

I am trying to see where Rmarkdown could interfere. The md content is untouched for pandoc to convert, and html_document used markdown as from format for pandoc which as simple_tables extension activated by default.

Thanks!

@fkohrt
Copy link
Author

fkohrt commented Sep 8, 2020

Yeah, my bad, I had not looked enough at the resulting HTML, which, in principle is fine indeed.

What I am expecting when converting the following document...

---
output: html_document
---

-------     ------ ----------   -------
     12     12        12             12
    123     123       123           123
      1     1          1              1
-------     ------ ----------   -------

Table: Simple table without header

...is this...

expected

...instead of this...

actual

The difference between them is the class table table-condensed that is being applied to the <table> in the first screenshot. It needs to be applied for headerless tables as well.

@cderv
Copy link
Collaborator

cderv commented Sep 8, 2020

Thank you for the precision! I agree it looks better !

From my test and example with the command line, it seems pandoc itselfs does not output your expected table.
Do you agree on that ? Or did I miss something ? Do you get the correct formatting using pandoc (without rmarkdown) ?

@atusy
Copy link
Collaborator

atusy commented Sep 8, 2020

table-condensed class is added by JavaScript below, which only targets at tables with headers.

$$('tr.header').parent('thead').parent('table').addClass('table table-condensed');

@fkohrt
Copy link
Author

fkohrt commented Sep 8, 2020

Yeah, I believe it's not an issue with Pandoc, but with rmarkdown's HTML template.

From my test and example with the command line, it seems pandoc itselfs does not output your expected table.
Do you agree on that ?

So yeah, Pandoc does not output my expected table, but Pandoc does neither for tables with headers. The styling is added by rmarkdown, but only for tables with headers.

Do you get the correct formatting using pandoc (without rmarkdown) ?

Pandoc doesn't provide formatting (as in: CSS) at all by default.

@cderv
Copy link
Collaborator

cderv commented Sep 8, 2020

I know understand the issue ! Sorry for all the question, it wasn't clear enough to me to know where to look.

I think @atusy pointed to the correct part - we will see how to support header less tables.

Thanks for the feedback @fkohrt !

@cderv cderv self-assigned this Sep 8, 2020
@yihui
Copy link
Member

yihui commented Sep 8, 2020

@fkohrt You could add the classes by yourself via a js code chunk:

---
title: "Beheaded tables"
output: html_document
---

  Right     Left     Center     Default
-------     ------ ----------   -------
     12     12        12             12
    123     123       123           123
      1     1          1              1

Table: Simple table with header


-------     ------ ----------   -------
     12     12        12             12
    123     123       123           123
      1     1          1              1
-------     ------ ----------   -------

Table: Simple table without header

```{js, echo=FALSE}
$('table').addClass('table table-condensed'); 
```

We may be able to do this in the default HTML template, but I'm not sure if it could break anything, given that the template has existed for quite a few years.

@cderv
Copy link
Collaborator

cderv commented Sep 8, 2020

I just opened a PR with a quick fix to discuss if it worth taking the risk that something break or not.
The current selector has been around this 2016 and rmarkdown 0.6 (7d29206) - so yeah quite some time !

@fkohrt
Copy link
Author

fkohrt commented Sep 8, 2020

I saw people struggle with this before, so I'm not sure of no action is the best bet in the long run. But you could wait until the dust settles with the AST change to tables, i.e. the readers and writers get implemented.

The immediate problem I had is remedied by the two workarounds discussed above.

@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants