R markdown html(tabular())输出转义字符&破桌子 [英] R markdown html(tabular()) Outputting escape characters & breaking tables

查看:263
本文介绍了R markdown html(tabular())输出转义字符&破桌子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码示例将生成我想要的表格 R Markdown:

  --- 
标题:表
作者:尼克
日期:2017年6月9日
输出:html_document
---

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

data< - data.frame(Age,Unborn,GenderBand,EthnicityGroup,InitialCategory)
年龄< - 6
数据$年龄[数据$ 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(1岁以下,b $ b1至4岁,b $ b5至9岁,b $ b10至15岁,
16至50岁,
其他))
数据$ Unborn< - factor(数据$ Unborn,levels = c(0,1),labels = c(Born ,未出生))
数据$ GenderBand< - factor(数据$ GenderBand,levels = c(1,2,3),labels = c(男性,女性,未知) )
data $ EthnicityGroup< - factor(data $ EthnicityGroup,
levels = c(1,2,3,4,5,6),
labels = c(White, 混合,亚洲,黑色,其他,拒绝))
数据$ InitialCategory< - factor(数据$ InitialCategory,
levels = c(1,2,3) ,4,5),
labels = c(Emotional,
Multiple,
忽略,
Phyical,
Sexual))
表< - 表格(GenderBand +(未出生*年龄)+ EthnicityGroup~InitialCategory,data = data)
```

``` {r output,echo = FALSE,results =asis}
html(表格)
```

这非常完美我的工作原理想要它。给我这个:

但是当我使用我的真实数据做到这一点时,我得到了这个:



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



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

解决方案

有点晚了,但我有同样的问题,最近想出了什么正在发生。当R输出我的表时,它合理并在摘要数据的每个单元格周围添加引号:

 按组分层
1 2 3
n676137827245
DON_AGE(mean(sd))41.24(12.76)36.92(11.03)39.89(17.70)
DON_LF_LU_BRONCHO( %)
异常8(1.2)15(1.1)2258(8.3)
缺失631(93.3)1333(96.7) 19343(71.0)
普通37(5.5)30(2.2)5644(20.7)

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



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


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?

解决方案

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.

这篇关于R markdown html(tabular())输出转义字符&amp;破桌子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆