如何在R Markdown代码块生成的表格中添加[^1]脚注?
解决R Markdown表格中添加脚注下标问题
要给表格里的「Median Pay」的「Pay」添加脚注下标¹,你之前的代码直接在R字符串里用[^1]会触发语法错误——R会把它当成无效表达式。以下是修正方案:
问题代码分析
你原本的代码尝试在key_points第一个条目中插入[^1],但这是Markdown语法,不能直接在R代码中使用:
mainPoints <- data.frame(key_points = c(paste0("Median Pay", [^1], "has increased by ", YearChangePct, "%"), paste0("Top decile Pay: £", TopDecile,"\nBottom decile Pay: £", BottomDecile), paste0(PayRisePct, "% of BLUB staff received a pay\nincrease between ", currentyear - 1, " and ", currentyear), paste0("Gender Pay gap is ", GenderPayGap, "%")), Commentary = c(paste0("The average (median full-time equivalent) pay of BLUB staff is £", CurrentYearMedian, " which is an increase of ", YearChangePct, "% on the equivalent figure for ", currentyear -1, ". The overall pay award for the BLUB for ", currentyear -1, " was 1%."), paste0("Pay of £", BottomDecile, " would put someone in the bottom 10% of BLUB staff, whereas pay of £", TopDecile, " would put someone in the top 10% of BLUB staff."), paste0("The extent of the increase varied between grades: ", AApct, "% of AA staff received a pay increase between 0.1% and 1.9%. ", IND2pct,"% of Industrial 2 staff received a pay increase between 4.0% and 5.9%."), paste0("There continues to be a gap between male and female pay in the BLUB – the median pay for females is ", GenderPayGap, "% lower than the median pay for males."))) names(mainPoints) <- c("Key Points", "Commentary")
修正方案
1. 替换脚注标记为可渲染格式
在R字符串中使用HTML的<sup>1</sup>(兼容性更好)或Markdown的^1作为脚注下标标记:
mainPoints <- data.frame( key_points = c( paste0("Median Pay<sup>1</sup> has increased by ", YearChangePct, "%"), paste0("Top decile Pay: £", TopDecile,"\nBottom decile Pay: £", BottomDecile), paste0(PayRisePct, "% of BLUB staff received a pay\nincrease between ", currentyear - 1, " and ", currentyear), paste0("Gender Pay gap is ", GenderPayGap, "%") ), Commentary = c( paste0("The average (median full-time equivalent) pay of BLUB staff is £", CurrentYearMedian, " which is an increase of ", YearChangePct, "% on the equivalent figure for ", currentyear -1, ". The overall pay award for the BLUB for ", currentyear -1, " was 1%."), paste0("Pay of £", BottomDecile, " would put someone in the bottom 10% of BLUB staff, whereas pay of £", TopDecile, " would put someone in the top 10% of BLUB staff."), paste0("The extent of the increase varied between grades: ", AApct, "% of AA staff received a pay increase between 0.1% and 1.9%. ", IND2pct,"% of Industrial 2 staff received a pay increase between 4.0% and 5.9%."), paste0("There continues to be a gap between male and female pay in the BLUB – the median pay for females is ", GenderPayGap, "% lower than the median pay for males.") ) ) names(mainPoints) <- c("Key Points", "Commentary")
2. 确保表格渲染时不转义标记
如果用knitr::kable生成表格,需添加escape = FALSE参数,让HTML/Markdown标记被正常渲染:
knitr::kable(mainPoints, escape = FALSE)
3. 在文档末尾添加脚注定义
在R Markdown文档任意位置(通常在末尾)补充脚注的具体内容:
[^1]: 此处填写脚注说明,例如「注:薪酬指全职员工等效中位数薪酬」
内容的提问来源于stack exchange,提问作者atm1984
相关产品推荐
相关产品推荐

