R markdown html(tabular()) Outputting escape characters & breaking tables

别来无恙 提交于 2019-12-11 07:34:03

问题


Here is a code sample that will generate the table that I want in 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)
```

This works pretty much perfectly how I want it. Giving me this: However when I did this using my real data, I got this: I've identified the issue in the HTML, and it appears that for some reason, on some cells (the broken ones), html(tablular()) has output this:

I'm completely lost as to why it seems to be scrambling the HTML output, as the numbers are generated by R (they're counts of factors).

In theory I could perhaps store the HTML output in a variable and gsub() the offending strings, but that seems like a messy work around for something that shouldn't really need one. Does anyone have any insight on this?


回答1:


Sorry for shameless autopromotion but you could try my package 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
```




回答2:


A little late to this, but I had the same issue and recently figured out what was going on. When R outputted my table, it justified and added quotes around each cell of summary data:

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) "

When I tried to run it through R Markdown using HTML, the cells where there were extra spaces were read as raw HTML code (for example, the Abnormal cell in group 1 above) and that's why I was getting the code in my table.

I used the CreateTableOne function, and to solve this problem I used the noSpaces=T option in print(CreateTableOne()). Then, I used htmlTable to print the table object, and this solved my problem.



来源:https://stackoverflow.com/questions/44460359/r-markdown-htmltabular-outputting-escape-characters-breaking-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!