尝试设置Knitr'文档'输出挂钩会导致代码块换行符丢失 [英] Trying to set Knitr 'document' output hook results in code chunk line breaks being lost

查看:53
本文介绍了尝试设置Knitr'文档'输出挂钩会导致代码块换行符丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读有关Knitr的输出挂钩的所有文档和示例用于编辑文档的输出.

I've been reading all the documentation and examples I can find for Knitr's output hooks for editing the output of the document.

我正在使用Knitr处理.Rmd文件,如下所示:

I'm using Knitr to process a .Rmd file, which looks like this:

[SOME_SHORTCODE_TO_BE_REPLACED](我最终希望在分析文档时将其替换为Knitr)

[SOME_SHORTCODE_TO_BE_REPLACED] (I eventually want this to be replaced by Knitr when it parses the document)

```{r echo=TRUE, eval=FALSE} 

data1 <- rnorm(10, mean = 0, sd = 10)

data2 <- rnorm(10, mean = 2, sd = 2)

model <- lm(data1 ~ data2)

summary(model) 
```

如果我运行

opts_chunk$set(tidy = FALSE)
knit(filename, output = outputFilename.markdown)

在文件上,它呈现(正确)如下:

on the file, it renders (correctly) like this:

[SOME_SHORTCODE_TO_BE_REPLACED](我最终希望在分析文档时将其替换为Knitr)

[SOME_SHORTCODE_TO_BE_REPLACED] (I eventually want this to be replaced by Knitr when it parses the document)

```r

data1<-rnorm(10,平均值= 0,sd = 10)

data1 <- rnorm(10, mean = 0, sd = 10)

data2<-rnorm(10,平均值= 2,sd = 2)

data2 <- rnorm(10, mean = 2, sd = 2)

模型<-lm(data1〜data2)

model <- lm(data1 ~ data2)

摘要(模型)

```

,但是,如果我要进行任何操作设置knit_hook文档" (解析后会遍历整个文档),则换行符和代码我的代码中的篱笆(```)已删除:

However, if I do anything to set the knit_hook "document" (which goes through the full document after it's parsed), the line breaks and code fences (```) in my code are deleted:

# As I understand, this *should* be identical to Knitr's default for the document hook, which is to just do:
# knit_hook$set(document = identity)

knit_hooks$set(document = function(x) {
    x
})
# (Then re-knit the file using the commands above)

结果:

[SOME_SHORTCODE_TO_BE_REPLACED](我最终希望在分析文档时将其替换为Knitr)

[SOME_SHORTCODE_TO_BE_REPLACED] (I eventually want this to be replaced by Knitr when it parses the document)

data1<-rnorm(10,平均值= 0,sd = 10)data2<-rnorm(10,平均值= 2,sd = 2)模型<-lm(data1〜data2)summary(model)

data1 <- rnorm(10, mean = 0, sd = 10)data2 <- rnorm(10, mean = 2, sd = 2)model <- lm(data1 ~ data2)summary(model)

我已经看到了几个示例,其中包括来自益辉的一个示例此处 ,这使我认为设置文档挂钩并让其返回x与其默认值没有什么不同.我在这里想念什么?

I've seen several examples, including one from Yihui here, that lead me to think that setting the document hook and just having it give back x shouldn't do anything different than its default. What am I missing here?

最终,我想将文档挂钩设置为使用gsub()将文档中的短代码替换为其他代码.但是与此同时,换行符和代码栅栏的问题使我感到困惑.

Eventually, I'd like to set the document hook to use gsub() to replace the shortcode in the document with other code. But this issue with line breaks and code fences is confusing me in the meantime.

对于在更改文档挂钩时如何保留这些换行符和代码围栏的任何建议,我将不胜感激!

I'd be grateful for any advice on how to retain those line breaks and code fences when changing the document hook!

推荐答案

您应该在Rmd文件的内部中设置挂钩函数.如果您在knit()之前执行此操作,则必须先调用render_markdown().

You should set up the hook function inside the Rmd file. If you do it before knit(), you must call render_markdown() first.

这篇关于尝试设置Knitr'文档'输出挂钩会导致代码块换行符丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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