haozhu233 / kableExtra

Construct Complex Table with knitr::kable() + pipe.
https://haozhu233.github.io/kableExtra/
Other
689 stars 147 forks source link

`save_kable` for markdown "pipe" tables #545

Closed vincentarelbundock closed 3 years ago

vincentarelbundock commented 4 years ago

Currently, using save_kable for markdown table produces an unusable file:

library(kableExtra)
dat = data.frame(a = 1:2, b = 2:3)
tab = kbl(dat, format="markdown") 
save_kable(tab, "test.md")

Which produces:

\<!DOCTYPE html\> `<html>`{=html} `<head>`{=html}
`<meta charset="utf-8"/>`{=html}
`<style>`{=html}body{background-color:white;}`</style>`{=html}
`<script src="lib1012e5ef3ed58/jquery-1.11.3/jquery.min.js">`{=html}`</script>`{=html}
`<meta name="viewport" content="width=device-width, initial-scale=1" />`{=html}
`<link href="lib1012e5ef3ed58/bootstrap-3.3.5/css/simplex.min.css" rel="stylesheet" />`{=html}
`<script src="lib1012e5ef3ed58/bootstrap-3.3.5/js/bootstrap.min.js">`{=html}`</script>`{=html}
`<script src="lib1012e5ef3ed58/bootstrap-3.3.5/shim/html5shiv.min.js">`{=html}`</script>`{=html}
`<script src="lib1012e5ef3ed58/bootstrap-3.3.5/shim/respond.min.js">`{=html}`</script>`{=html}
`<link href="lib1012e5ef3ed58/lightable-0.0.1/lightable.css" rel="stylesheet" />`{=html}
`<script src="lib1012e5ef3ed58/kePrint-0.0.1/kePrint.js">`{=html}`</script>`{=html}

```{=html}
</head>
<body>
|  a|  b| |--:|--:| |  1|  2| |  2|  3|
</body>
</html>


One solution would be to:

1. Add a conditional in `save_kable` to dispatch tables of type Pandoc Pipe, based on the `attr`
2. Implement a new function `save_kable_pipe` function to save tables of type Pandoc Pipe. This function could work in different ways. One possibility would be a simple `writeLines` call.

If you are interested in this functionality, I can prepare a PR.
haozhu233 commented 4 years ago

@vincentarelbundock Thanks a lot for this suggestion! It seems that you want to add markdown support to both the input and the output? Here are a few things that are not very clear to me.

I hope save_kable could have more functionalities as you suggested in option 1 but at the same time, I'm also looking for a unified interface. Have you looked into pandoc and see if the two questions above are possible?

vincentarelbundock commented 4 years ago

I had a much less general (or perhaps a stopgap) measure in mind: If the user supplies an object with attr("format")=="pipe" and a file name with a ".txt" or ".md" or ".Rmd" extension, then writeLines to file.

Honestly, I think that if users want an image, it would be much better for them to go through LaTeX or HTML. I just don't see a realistic and compelling use-case for going through markdown if you want a JPG in the end. Plus, this would add a lot of complications for little payoff.

My main worry is to be able to save markdown files in a useable text-only format. As far as I can tell, there is currently no "clean" way to do that, beyond calling writeLines (with some kind of paste(collapse='\n'). It seems natural to expect save_kable(tab, "file.md") to "Just Work", and it would be quite easy to get that minimal functionality in, without diving deep into options that will probably have very few actual use cases (in my expectation).

haozhu233 commented 4 years ago

@vincentarelbundock I see. Sure, a PR on save_kable will be really appreciated! It's probably a good idea to send out some warning or stop if users want to do these md -> image thing. I sort of learned that users will try every possible route. ;)