R markdown html(tabular())输出转义字符&破桌子

时间:2017-06-09 14:27:17

标签: html r html-table markdown html-escape-characters

以下是一个代码示例,它将在 R Markdown中生成我想要的表格:

---
title: "Table"
author: "Nick"
date: "9 June 2017"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tables)
Age <- sample(0:19, 500, replace = TRUE)
Unborn <- sample(0:1, 500, replace = TRUE)
GenderBand <- sample(1:3, 500, replace = TRUE)
EthnicityGroup <- sample(1:5, 500, replace = TRUE)
InitialCategory <- sample(1:5, 500, replace = TRUE)

data <- data.frame(Age, Unborn, GenderBand, EthnicityGroup, InitialCategory)
Age <- 6
data$Age[data$ChildAge31March == 0] <- 1
data$Age[data$ChildAge31March >= 1 & data$ChildAge31March <= 4] <- 2
data$Age[data$ChildAge31March >= 5 & data$ChildAge31March <= 9] <- 3
data$Age[data$ChildAge31March >= 10 & data$ChildAge31March <= 15] <- 4
data$Age[data$ChildAge31March >= 16 & data$ChildAge31March <= 50] <- 5
data$Age <- factor(data$Age,
                  levels = c(1,2,3,4,5,6),
                  labels = c("Under 1",
                             "1 to 4 Years Old",
                             "5 to 9 Years Old",
                             "10 to 15 Years Old",
                             "16 to 50 Years Old",
                             "Other"))
data$Unborn <- factor(data$Unborn, levels = c(0,1), labels = c("Born","Unborn"))
data$GenderBand <- factor(data$GenderBand, levels = c(1,2,3), labels = c("Male","Female","Unknown"))
data$EthnicityGroup <- factor(data$EthnicityGroup, 
                              levels = c(1,2,3,4,5,6), 
                              labels = c("White","Mixed","Asian","Black","Other","Refused"))
data$InitialCategory <- factor(data$InitialCategory,
                               levels = c(1,2,3,4,5),
                               labels = c("Emotional",
                                          "Multiple",
                                          "Neglect",
                                          "Phyical",
                                          "Sexual"))
Table <- tabular(GenderBand + (Unborn * Age) + EthnicityGroup ~ InitialCategory, data=data)
```

```{r output, echo=FALSE, results="asis"}
html(Table)
```

这非常完美,我想要它。给我这个: Working Table 但是,当我使用我的真实数据做到这一点时,我得到了这个: Broken Rubbish Table 我已经在HTML中发现了这个问题,看来由于某种原因,在某些单元格(破碎的单元格)上,html(tablular())输出了这个:

Incorrectly Interpreted Mark Up

我完全迷失了为什么它似乎在加扰HTML输出,因为数字是由 R (它们的因素计数)产生的。

理论上我可能会将HTML输出存储在变量和gsub()违规字符串中,但对于那些不应该真正需要的东西来说,这似乎是一个混乱的工作。有没有人对此有任何见解?

2 个答案:

答案 0 :(得分:0)

抱歉无耻的autopromotion,但您可以尝试我的包expss

---
title: "Table"
author: "Nick"
date: "9 June 2017"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(expss)
ChildAge31March = sample(0:19, 500, replace = TRUE)
Unborn = sample(0:1, 500, replace = TRUE)
GenderBand = sample(1:3, 500, replace = TRUE)
EthnicityGroup = sample(1:5, 500, replace = TRUE)
InitialCategory = sample(1:5, 500, replace = TRUE)

data = data.frame(ChildAge31March, Unborn, GenderBand, EthnicityGroup, InitialCategory)

data = compute(data,  {
    Age = recode(ChildAge31March, 
                 0 ~ 1, 
                 1 %thru% 4 ~ 2,
                 5 %thru% 9 ~ 3,
                 10 %thru% 15 ~ 4,
                 16 %thru% 50 ~ 5,
                 other ~ 6
    )   
    val_lab(Age) = autonum(
        "Under 1
         1 to 4 Years Old
         5 to 9 Years Old
         10 to 15 Years Old
         16 to 50 Years Old
         Other")

    val_lab(Unborn) = num_lab(
         "0 Born
          1 Unborn")
    val_lab(GenderBand) = autonum(
         "Male
         Female
         Unknown")
    val_lab(EthnicityGroup) = autonum(
        "White
         Mixed
         Asian
         Black
         Other
         Refused")
    val_lab(InitialCategory) = autonum(
        "Emotional
         Multiple
         Neglect
         Phyical
         Sexual" )
})

Table = data %>% 
    tab_cols(InitialCategory) %>% 
    tab_cells(GenderBand, Unborn %nest% Age, EthnicityGroup) %>% 
    tab_stat_cases(total_row_position = "none") %>% 
    tab_pivot()

```

```{r output, echo=FALSE, results="asis"}
Table
```

答案 1 :(得分:0)

有点晚了,但我遇到了同样的问题,最近发现了发生的事情。当R输出我的表时,它证明了并且在摘要数据的每个单元格周围添加了引号:

Stratified by Group
                                  1                 2                 3           
n                                 "   676"          "  1378"          " 27245"         
DON_AGE (mean (sd))               " 41.24 (12.76)"  " 36.92 (11.03)"  " 39.89 (17.70)" 
DON_LF_LU_BRONCHO (%)             "   "             "  "              " "            " 
     Abnormal                     "     8 ( 1.2) "  "    15 ( 1.1) "  "  2258 ( 8.3) "  
     Missing                      "   631 (93.3) "  "  1333 (96.7) "  " 19343 (71.0) "  
     Normal                       "    37 ( 5.5) "  "    30 ( 2.2) "  "  5644 (20.7) "

当我尝试使用HTML通过R Markdown运行它时,有额外空格的单元格被读作原始HTML代码(例如,上面第1组中的异常单元格),这就是为什么我在我的代码中获取代码的原因表

我使用了CreateTableOne函数,为了解决这个问题,我在noSpaces=T中使用了print(CreateTableOne())选项。然后,我使用htmlTable打印table对象,这解决了我的问题。