Knitr HTML输出格式问题

时间:2018-10-06 19:58:36

标签: html r knitr xtable

我的图表和大多数表格都可以很好地编织为HTML,但是某些带有“子类别”的表格存在格式问题,这些问题显示HTML代码而不是已处理表格。

最上面的表格是我想要的,但是最下面的表格格式不正确。

代码如下:

library(tableone)
tableone_f <- function(vars) {
  table <- CreateTableOne(vars = vars, strata = c("Gender"), 
                          data = dataset) %>% 
           print(printToggle = FALSE, noSpaces = TRUE)
  return(xtable(table))
}

# Pad table cells
cat("
<style>
th, td {
  padding: 2px 5px 2px 5px;
}
</style>
")

outs <- c("ISL_cor", "GMCT_mps", "IDN_lmn", "ONB_acc", 
          "TWOB_acc", "ISRL_cor", "PA_Total", "Spatial", 
          "Flanker")
print(tableone_f(outs), type = "html")

symptoms <- c("MHQ_Unintentional_Impacts_Category", "MHQ_Symptom_Binary")
print(tableone_f(symptoms), type = "html")

有人知道发生了什么/如何解决此问题?

数据:

structure(list(Cluster = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 
3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 6L, 6L, 6L), SubjectID_Full = c("2012-658-0101-BH", 
"2012-658-0101-BH", "2012-658-0101-BH", "2012-658-0102-JB", "2012-658-0102-JB", 
"2012-658-0102-JB", "2012-658-0102-JB", "2012-658-0103-SM", "2012-658-0103-SM", 
"2012-658-0103-SM", "2012-658-0103-SM", "2012-658-0103-SM", "2012-658-0104-SN", 
"2012-658-0104-SN", "2012-658-0104-SN", "2012-658-0104-SN", "2012-658-0105-JF", 
"2012-658-0106-JR", "2012-658-0106-JR", "2012-658-0106-JR"), 
    SubjectID = c("SOC0101", "SOC0101", "SOC0101", "SOC0102", 
    "SOC0102", "SOC0102", "SOC0102", "SOC0103", "SOC0103", "SOC0103", 
    "SOC0103", "SOC0103", "SOC0104", "SOC0104", "SOC0104", "SOC0104", 
    "SOC0105", "SOC0106", "SOC0106", "SOC0106"), Visit = c(0, 
    18, 24, 0, 6, 18, 24, 0, 6, 12, 24, 18, 0, 6, 18, 24, 6, 
    0, 6, 18), Gender = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("Male", 
    "Female"), class = "factor"), Visit_age = c(37, 38, 39, 22, 
    23, 24, 24, 20, 21, 21, 22, 22, 36, 37, 38, 38, 22, 42, 42, 
    43), AHQ_YearsSimilarFreq = c(13, 13, 13, 5, 5, 5, 5, 12, 
    12, 12, 12, 12, 6, 6, 6, 6, 11, 4, 4, 4), AHQ_Out_Games_MainPos = structure(c(3L, 
    3L, 3L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 1L, 1L, 1L), .Label = c("Forward", "Midfield", "Defense", 
    "Goaltender"), class = "factor"), AlcWeek_Category = structure(c(2L, 
    2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 1L, 1L, 1L), .Label = c("I do not drink", "Light Drinking (M:1-7, F:1-2)", 
    "Mod-Heavy Drinking (M:8+, F:3+)"), class = "factor"), CigsYN = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    2L, 1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), 
    Concussion = structure(c(3L, 3L, 3L, 2L, 2L, 2L, 2L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("0", 
    "1", "2+"), class = "factor"), SchoolYears = c(19, 19, 19, 
    16, 16, 16, 16, 15, 15, 15, 15, 15, 20, 20, 20, 20, 15, 20, 
    20, 20), HeightCm = c(170, 170, 170, 182, 182, 182, 182, 
    177.8, 177.8, 177.8, 177.8, 177.8, 168, 168, 168, 168, 162.56, 
    164, 164, 164), WeightKg = c(63.18181, 58.63636, 60.45454, 
    70.90909, 77.72727, 75.45454, 80.45454, 78.86363, 81.36363, 
    80, 83.18181, 82.72727, 68.18181, 69.0909, 68.18181, 65, 
    69.0909, 48.18181, 50.45454, 47.72727), BMI = c(21.86222, 
    20.28939, 20.91852, 21.40716, 23.46554, 22.77941, 24.28889, 
    24.94671, 25.73752, 25.30617, 26.31266, 26.16888, 24.15739, 
    24.47948, 24.15739, 23.03004, 26.14529, 17.91412, 18.75912, 
    17.74511), HI_LateralityQuotient = c(0.529411765, 0.529411765, 
    0.529411765, 0.846153846, 0.846153846, 0.846153846, 0.846153846, 
    0.888888889, 0.888888889, 0.888888889, 0.888888889, 0.888888889, 
    1, 1, 1, 1, 0.8, 1, 1, 1), NeckLengthCm = c(16, 16, 16, 14, 
    14, 14, 14, 16, 16, 16, 16, 16, 16, 16, 16, 16, 15, 15, 15, 
    15), NeckCircCm = c(35, 30, 32, 35, 34, 34, 36, 38, 39, 38, 
    40, 41, 39, 24, 36, 38, 34, 30, 29, 30), HeadCircCm = c(58, 
    58, 58, 56, 56, 56, 56, 57, 57, 57, 57, 57, 58, 58, 58, 58, 
    55, 52, 52, 52), NeckVolumeCm = c(1559.718, 1145.915, 1303.797, 
    1364.753, 1287.881, 1287.881, 1443.853, 1838.557, 1936.597, 
    1838.557, 2037.183, 2140.315, 1936.597, 733.3859, 1650.118, 
    1838.557, 1379.873, 1074.295, 1003.869, 1074.295), MHQ_Heading_Male_Quartile = structure(c(NA, 
    NA, NA, 4L, 4L, 3L, 2L, 2L, 3L, 2L, 3L, 2L, 1L, 3L, 2L, 3L, 
    NA, NA, NA, NA), .Label = c("0-6", "6.01-15", "15.01-53", 
    "53.01+"), class = "factor"), AHQ_Heading_Male_Quartile = structure(c(NA, 
    NA, NA, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    NA, NA, NA, NA), .Label = c("0-435", "435.01-861", "861.01-1964", 
    "1964.01+"), class = "factor"), MHQ_Heading_Female_Quartile = structure(c(1L, 
    1L, 2L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    4L, 2L, 3L, 3L), .Label = c("0-2", "2.1-7", "7.1-24", "24.1+"
    ), class = "factor"), AHQ_Heading_Female_Quartile = structure(c(1L, 
    1L, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    3L, 2L, 2L, 2L), .Label = c("0-108", "108.1-430", "430.1-1278", 
    "1278.1+"), class = "factor"), ISL_cor = c(27, 33, 30, 29, 
    28, 28, 29, 33, 28, 32, 27, 25, 26, 23, 22, 24, 26, 31, 31, 
    30), GMCT_mps = c(0.066638, 0.166589, 1.06656, 1.83315, 1.39986, 
    1.366439, 1.732987, 1.89962, 1.599627, 1.76649, 1.79964, 
    1.49935, 0.69993, 0.499967, 1.133031, 1.033575, 1.866542, 
    1.932174, 1.866418, 1.666167), IDN_lmn = c(2.712426, 2.666696, 
    2.691574, 2.60214, 2.66739, 2.666007, 2.683421, 2.64958, 
    2.637634, 2.592968, 2.627395, 2.626299, 2.794555, 2.799669, 
    2.883404, 2.76647, 2.668145, 2.691837, 2.711009, 2.665336
    ), ONB_acc = c(NA, 1.570796, 1.570796, NA, 1.2692, 1.188903, 
    1.322056, NA, 1.393086, 1.393086, 1.322056, 1.393086, NA, 
    1.322056, 1.225939, 1.2692, 1.393086, NA, 1.2692, 1.322056
    ), TWOB_acc = c(1.325818, 1.570796, 1.570796, 1.230959, 1.162158, 
    1.570796, 1.325818, 1.230959, 1.395827, 1.325818, 1.273674, 
    1.395827, 1.021329, 1.230959, 1.273674, 1.325818, 1.230959, 
    1.273674, 1.395827, 1.570796), ISRL_cor = c(9, 10, 12, 11, 
    11, 11, 10, 12, 12, 12, 11, 11, 10, 10, 9, 8, 11, 11, 12, 
    12), PA_Total = c(21, 23, 24, 20, 13, 20, 21, 21, 24, 23, 
    24, 21, 16, 21, 22, 23, 23, 19, 22, 22), Spatial = c(0.451184, 
    0.166667, 0.083333, 0.5, 1.339256, 1.533743, 1.622678, 0.333333, 
    0.166667, 0.416667, 0.333333, 0.25, 0.970857, 1.235702, 2.41257, 
    1.27022, 0.686887, 1.206559, 0.436339, 1.235702), Flanker = c(206.48, 
    106.09, 68.9, 155.93, 102.03, 129.46, 135.85, 89.29, 134.4, 
    125.57, 104.29, 105.33, 182.3, 108.25, 92, 109.75, 93.85, 
    41.9, 75.25, 16.43), MHQ_Unintentional_Impacts_Category = structure(c(3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 
    3L, 1L, 3L, 2L), .Label = c("0", "1", "2+"), class = "factor"), 
    MHQ_Symptom_Binary = structure(c(1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L), .Label = c("None/Mild", 
    "Moderate/Severe/Very Severe"), class = "factor"), MHQ_Symptom_Binary_num = c(0, 
    0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1), 
    ExtensorAVG = c(NA, 40.26666, 32.03333, NA, NA, 40.1, 30.76666, 
    NA, NA, 54.6, 45.86666, 37.7, NA, NA, 36.2, 48.2, NA, NA, 
    NA, 21.36666), FlexorAVG = c(NA, 15.33333, 15, NA, NA, 19.66666, 
    22.5, NA, NA, 30.36666, 28.93333, 31.2, NA, NA, 19.86666, 
    21.56666, NA, NA, NA, 11.6), RightAVG = c(NA, 19.13333, 27.03333, 
    NA, NA, 17, 19.43333, NA, NA, 21.73333, 22.66666, 24.03333, 
    NA, NA, 15.06666, 19.96666, NA, NA, NA, 10.73333), LeftAVG = c(NA, 
    27.4, 27.03333, NA, NA, 16.7, 20.8, NA, NA, 24.86666, 25.06666, 
    29.4, NA, NA, 12.2, 19.8, NA, NA, NA, 10.96666), LateralAVG = c(NA, 
    23.26666, 27.03333, NA, NA, 16.85, 20.11666, NA, NA, 23.3, 
    23.86666, 26.71666, NA, NA, 13.63333, 19.88333, NA, NA, NA, 
    10.85), ExtensorPeak = c(NA, 41.2, 32.8, NA, NA, 42.2, 34.5, 
    NA, NA, 56.8, 51.1, 43.3, NA, NA, 37.8, 51.8, NA, NA, NA, 
    22.7), FlexorPeak = c(NA, 15.9, 15.5, NA, NA, 22.4, 25.3, 
    NA, NA, 34, 30.7, 32.8, NA, NA, 22, 23.6, NA, NA, NA, 12), 
    RightPeak = c(NA, 20.9, 28.3, NA, NA, 19.3, 20.8, NA, NA, 
    23, 24.3, 24.3, NA, NA, 16.7, 21.8, NA, NA, NA, 11.1), LeftPeak = c(NA, 
    28.2, 29.5, NA, NA, 20.2, 22.4, NA, NA, 25.6, 27.4, 32.2, 
    NA, NA, 13.3, 20.8, NA, NA, NA, 11.4), LateralPeak = c(NA, 
    24.55, 28.9, NA, NA, 19.75, 21.6, NA, NA, 24.3, 25.85, 28.25, 
    NA, NA, 15, 21.3, NA, NA, NA, 11.25), FlexExtDiff_AVG = c(NA, 
    -24.93333, -17.03333, NA, NA, -20.43334, -8.26666, NA, NA, 
    -24.23334, -16.93333, -6.5, NA, NA, -16.33334, -26.63334, 
    NA, NA, NA, -9.76666), FlexExtDiff_Peak = c(NA, -25.3, -17.3, 
    NA, NA, -19.8, -9.2, NA, NA, -22.8, -20.4, -10.5, NA, NA, 
    -15.8, -28.2, NA, NA, NA, -10.7)), row.names = c(NA, -20L
), class = c("tbl_df", "tbl", "data.frame"))

1 个答案:

答案 0 :(得分:0)

我已经弄清楚了-子类别的字符串中有空格,这导致了奇怪的格式化。使用stringr中的str_trim解决了该问题!