pau*_*n32 3 r knitr r-markdown
我有一个输出正确渲染的html代码的函数,但是将其封装在带有'results =“ asis”`的markdown文档R代码块中会失败。我将问题追溯到HTML代码中的空格。我喜欢空格,因为它们使读取原始HTML文件更加容易。如果您可以考虑安装我的R软件包“ rockchalk”,则可以运行此程序并看到相同的内容。
我展示了问题是由以下MRE中HTML中的空格引起的(标记被视为代码的标记)。我得到的输出没有任何其他样式表魔术就可以使用:http : //pj.freefaculty.org/scraps/mre.html
向下滚动,您会看到表格已损坏,但是在筛选出多余的空格后,表格将按预期显示。
在您看来,这仅仅是我的用户错误,即依赖HTML代码中的空格,还是R markdown中“ asis”的错误。
---
title: "Guide Documents"
author:
- name: Paul Johnson
affiliation: Center for Research Methods and Data Analysis, University of Kansas
abstract:
Author, please REMEMBER TO INCLUDE AN ABSTRACT BEFORE FINALIZING THIS DOCUMENT!
date: "`r format(Sys.time(), '%Y %B %d')`"
output:
html_document:
highlight: haddock
---
```{r setup, include = FALSE}
##This Invisible Chunk is required in all CRMDA documents
outdir <- paste0("tmpout")
if (!file.exists(outdir)) dir.create(outdir, recursive = TRUE)
knitr::opts_chunk$set(echo = TRUE, comment = NA, fig.path = paste0(outdir, "/p-"))
options(width = 70)
```
```{r myregs}
library(rockchalk)
set.seed(2134234)
dat <- data.frame(x1 = rnorm(100), x2 = rnorm(100))
dat$y1 <- 30 + 5 * rnorm(100) + 3 * dat$x1 + 4 * dat$x2
dat$y2 <- rnorm(100) + 5 * dat$x2
m1 <- lm(y1 ~ x1, data = dat)
m2 <- lm(y1 ~ x2, data = dat)
m3 <- lm(y1 ~ x1 + x2, data = dat)
gm1 <- glm(y1 ~ x1, family = Gamma, data = dat)
or1 <- outreg(list("Amod" = m1, "Bmod" = m2, "Gmod" = m3),
title = "My Three Linear Regressions", float = FALSE, type = "html")
```
```{r browseme}
or1 <- outreg(list("Amod" = m1, "Bmod" = m2, "Gmod" = m3),
title = "My Three Linear Regressions", float = FALSE, type = "html")
```
```{r flawed, results = "asis"}
cat(or1)
```
```{r cleaned, results = "asis"}
or1 <- gsub(" "," ", or1)
or1 <- gsub("^\\ *", "", or1)
or1 <- paste(or1, collapse = "")
or1 <- gsub("\\ \\ \\ \\ \\ \\ ", " ", or1)
or1 <- gsub("\\ \\ \\ ", " ", or1)
cat(or1)
```
```{r sessionInfo, echo = FALSE}
sessionInfo()
```
Available under
[Created Commons license 3.0 <img src="http://crmda.dept.ku.edu/images/cc-3.0.png" alt="CC BY"
style="width: 75px;height: 20px;"/>](http://creativecommons.org/licenses/by/3.0/)
Run Code Online (Sandbox Code Playgroud)
该knitr块选项results='asis'已经正确地完成了它的任务。问题是Pandoc的Markdown将缩进的行(按四个空格)当作文字代码块(<pre></pre>)处理。
解决方案是删除前导空格或保护它们。看来您不想做前者。您可以将HTML代码传递给,htmltools::HTML()以便在Pandoc转换期间保护输出。
| 归档时间: |
|
| 查看次数: |
613 次 |
| 最近记录: |