R markdown asis破坏了有效的html代码 [英] R markdown asis breaks valid html code

查看:109
本文介绍了R markdown asis破坏了有效的html代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输出正确渲染的html代码的函数,但是将其封装在带有'结果="asis"`的markdown文档R代码块中会失败.我将问题追溯到HTML代码中的空格.我喜欢这些空格,因为它们使读取原始HTML文件更加容易.如果您可以考虑安装我的R软件包"rockchalk",则可以运行此程序并看到相同的内容.

I have a function that outputs html code that renders properly, but encasing it in a markdown document R code chunk with ´results = "asis"` fails. I trace problem back to spaces in my HTML code. I like the spaces because they make it easier to read the raw HTML file. If you would please consider installing my R package "rockchalk", you could run this and see the same.

我展示了问题是由以下MRE中的空格(标记被视为代码的标记)引起的.我得到的输出没有任何其他样式表魔术就可用: http://pj.freefaculty.org /scraps/mre.html

I show the problem is caused by spaces in HTML (which markdown treats as markup for code) in the following MRE. The output I get without any additional style sheet magic is available: http://pj.freefaculty.org/scraps/mre.html

向下滚动,您会看到表格已损坏,但是在筛选出多余的空格后,表格将按预期显示.

You scroll down you see the table is broken, but after sifting out the extra spaces, then the table shows as intended.

您认为,这仅仅是我的用户错误,是因为它依赖HTML代码中的空格,还是R markdown中"asis"中的错误.

In your opinion, is this simply my user error for relying on spaces in HTML code, or is it a bug in "asis" in R markdown.

---
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("&nbsp;"," ", 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/)

推荐答案

针织块选项results='asis'已正确完成其工作.问题是 Pandoc的Markdown 将缩进的行(四个空格)视为文字代码块(<pre></pre>).

The knitr chunk option results='asis' has done its job correctly. The problem is Pandoc's Markdown treats indented lines (by four spaces) as literal code blocks (<pre></pre>).

解决方案是删除前导空格或保护前导空格.看来您不想做前者.您可以将HTML代码传递到htmltools::HTML(),以便在Pandoc转换期间保护输出.

The solution is either remove the leading spaces, or protect them. It seems you do not want to do the former. You can pass your HTML code to htmltools::HTML() so that the output is protected during Pandoc's conversion.

这篇关于R markdown asis破坏了有效的html代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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