yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.39k stars 878 forks source link

kable produces malformed reference links after HTML calls in blogdown #1669

Closed oganm closed 5 years ago

oganm commented 5 years ago

I have asked this question in stackoverflow too but did not get an answer yet.

The gist of it is, within a freshly created blogdown page, creating a table using kable causes the caption to be edited to include a reference link and a table count (eg. "Table 1: Intended Caption"). This is undesirable for me but I assume it is intended behaviour.

However this behaviour changes when kable is called from within pretty much anything that uses a {. The reference link is added by appending a (\#tab:codeblocklabel) in the beginning of each table which html just shows as plain text.

Here is an an example post that highlights the problem. Here test1's caption is processed correctly but all the others use the (\#tab:codeblocklabel) format.

---
title: test
author: ''
date: '2019-02-05'
slug: test
categories: []
tags: []
---

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

```{r test1}
library(knitr)
library(magrittr)
library(kableExtra)
library(shiny)

data.frame(a= c(1,2,3)) %>% kable(caption = 'test',format = 'html')

```

```{r test2}

lapply(1:2,function(x){
    data.frame(a= c(1,2,3)) %>% kable(caption = 'test2',format = 'html') %>% HTML()
}) -> tables

tables[[1]]

tables[[2]]
```

```{r test3}
for(i in 1){
  table = data.frame(a= c(1,2,3)) %>% kable(caption = 'test3',format = 'html') %>% HTML()
}
table
```

```{r test4}
{data.frame(a= c(1,2,3)) %>% kable(caption = 'test4',format = 'html') %>% HTML()}
```

As stated on the stackoverflow I was able to identify the code that causes this behaviour in the create_label function but could not understand how "bookdown.internal.label" is set or can be modified.

Ultimately, I don't want my captions to be edited by kintr at all. But the second behaviour is clearly wrong and unintentional while the first is probably a sensible default (though I would appreciate some hints in getting rid of that too).

tcgriffith commented 5 years ago

i tested your example post and all of the tables appear the same. Can you add a screenshot? thanks!

oganm commented 5 years ago

image

tcgriffith commented 5 years ago

Unable to reproduce the issue in my blogdown site. maybe it's related to the specific theme. which theme are you using? Please show us your repo.

oganm commented 5 years ago

This is the default theme ("yihui/hugo-lithium"). I simply ran

blogdown::new_site()
blogdown::new_post("test", ext = ".rmd")

And edited the content/post/2019-02-05-test.Rmd file to include the code posted above. Everything else is untouched. Then

blogdown::serve_site()

You can find the output here freshly re-created including the rendered rmd file and the resulting site in public

The issue was replicated in two independent ubuntu machines. I am yet to try this on a windows

oganm commented 5 years ago

It seems like it is related to the HTML call I appended after kables. The issue occurs at the point of coercing into a character and has nothing to do with {s. If I replace

data.frame(a= c(1,2,3)) %>% kable(caption = 'test',format = 'html')

with

data.frame(a= c(1,2,3)) %>% kable(caption = 'test',format = 'html') %>% as.character()

I get

## [1] "<table>\n<caption>(\\#tab:test1)test</caption>\n <thead>\n  <tr>\n   <th style=\"text-align:right;\"> a </th>\n  </tr>\n </thead>\n<tbody>\n  <tr>\n   <td style=\"text-align:right;\"> 1 </td>\n  </tr>\n  <tr>\n   <td style=\"text-align:right;\"> 2 </td>\n  </tr>\n  <tr>\n   <td style=\"text-align:right;\"> 3 </td>\n  </tr>\n</tbody>\n</table>"

as the output which is what appears in my previous malformed tables. If I replace that as.character with HTML, I get the same malformed table caption. This suggests the issue has nothing to do with {s. I suspect in blogdown and bookdown, knitr outputs the files with the malformed captions which are later processed by something else to add the table numbers. I don't know why using the HTML function bypasses this post processing. But in general getting rid of this processing step will probably solve both my problems

Also in my use case I need the HTML calls as I use shiny's tags to get the tables together

yihui commented 5 years ago

Duplicate of https://github.com/rstudio/blogdown/issues/367

github-actions[bot] commented 3 years ago

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.