Replace column names in kable/R markdown

前端 未结 2 2108
离开以前
离开以前 2021-02-07 12:52

My data frame has ugly column names, but when displaying the table in my report, I want to their \"real\" names including special characters \'(\', new lines, greek letters, rep

相关标签:
2条回答
  • 2021-02-07 13:50

    If you're targeting HTML, then Δ is an option too.

    I couldn't get the accepted answer to work on HTML, so used the above.

    0 讨论(0)
  • 2021-02-07 13:51

    I am not sure where you got the advice to replace rownames, but it seems excessively complex. It is much easier just to use the built-in col.names argument within kable. This solution works for both HTML and LaTeX outputs:

    ---
    output:
      pdf_document: default
      html_document: default
    ---
    ```{r functions,echo=T}
    require(knitr)
    
    df <- data.frame(A=c(1,2),B=c(4,2),C=c(3,4),D=c(8,7))
    knitr::kable(df, 
                 col.names = c("Space in name",
                               "(Special Characters)",
                               "$\\delta{m}_1$",
                               "Space in name"))
    
    ```
    

    PDF output:

    HTML output:

    0 讨论(0)
提交回复
热议问题