什么命令将knitr R Markdown转换为Stack-Exchange-Friendly Markdown? [英] What command converts knitr R Markdown into Stack-Exchange-friendly Markdown?

查看:62
本文介绍了什么命令将knitr R Markdown转换为Stack-Exchange-Friendly Markdown?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

动机: 我经常想将使用R Markdown进行快速分析的结果粘贴到StackExchange站点中.其中包括堆栈溢出R标签交叉验证,甚至在认知科学堆栈交换之类的网站上进行特定领域的分析(例如,对 OECD寿命指数数据).

Motivation: I often want to paste the results of a quick analysis using R Markdown into a StackExchange site. This includes the R-tag on Stack Overflow, Cross Validated, or even a domain specific analysis on sites like Cognitive Sciences Stack Exchange (e.g., this quick analysis of OECD life index data).

默认转化问题: knitr默认降价输出不适合包含在StackExchange中. 我可以看到的主要问题是

Problems with default conversion: The default markdown output of knitr is not suitable for inclusion on StackExchange. The main problems I can see are that

  • 图像引用到本地硬盘驱动器
  • 代码块没有制表符或空格缩进;而是使用github样式Markdown(即,没有缩进)

即,块看起来像这样:

```r
some code
```

输出看起来像这样

```
## some output
## ...
```

可能还需要考虑其他特定问题,例如

There might also be other specific issues to consider, such as

  • 正确包含保证表
  • 确保为支持MathJax的站点正确传递方程式,例如交叉验证和认知科学堆栈交换.

什么是将R Markdown转换为Markdown(或HTML)并适合简单地包含在Stack Exchange网站中的好命令?

我认为理想的命令是使用R Markdown文件并生成一个文件的单行代码,该文件中的所有内容都可以直接粘贴到Stack Exchange中,以产生格式合理的问题或答案.

I think an ideal command would be a one-liner that takes an R Markdown file and generates a file where the entire content can be pasted directly into Stack Exchange to yield a well-formatted question or answer.

我与以下人共享这个简单的rmd文件几个代码块,一个图形和一个等式作为测试示例.

I share this simple rmd file with a couple of code chunks, a figure, and an equation as a test example.

初步想法: 在imgur上托管图片可能会解决图片问题.可以通过在R Markdown文件中包含以下内容来完成此操作,但如果将此指令合并到某个单行命令中,则可能会更简单.

Initial thoughts: Hosting of images on imgur would presumably sort out the issue with images. This can be done by including the following in the R Markdown file, but it would probably be simpler if this instruction was incorporated into some one-liner command.

``` {r }
opts_knit$set(upload.fun = imgur_upload) 
````

可能值得考虑的是,将HTML还是Markdown粘贴到StackExchange中是更好的格式. markdown软件包提供了很大的灵活性.

It might be worth considering whether HTML or Markdown is the better format for pasting into StackExchange. The markdown package provides a lot of flexibility.

推荐答案

这是一个实用程序功能,可以帮助您入门.它将自动上载设置为imgur,以及使用选项卡而不是受围栏的代码对源代码进行降价渲染.您可以增强此功能,以添加其他有用的选项.

Here is a utility function that should get you started. It sets auto uploads to imgur, as well as markdown rendering of source code using tabs instead of fenced blocks. You can enhance this function to add other options that would be useful.

stackify <- function(rmd_file, ...){
  require(knitr)
  opts_knit$set(upload.fun = imgur_upload) 
  render_markdown(strict = TRUE)
  out <- knit(rmd_file, ...)
  return(invisible(out))
}

更新:我在您的测试文件上测试了此功能,并且在启用mathjax的stats.stackexchange.com上呈现良好.

UPDATE: I tested this function on your test file, and it renders well on stats.stackexchange.com which is mathjax enabled.

这篇关于什么命令将knitr R Markdown转换为Stack-Exchange-Friendly Markdown?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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