【问题标题】:knitr + pandoc: cannot see plot when viewing html slidesknitr + pandoc:查看 html 幻灯片时看不到绘图
【发布时间】:2013-12-03 22:15:55
【问题描述】:

我已经使用 knitr 包和 pandoc 在 R Studio 中成功创建了 2 个测试幻灯片。在 R studio 中使用“knitr”按钮时的 R markdown 文件看起来很好并且可以正确显示绘图,但是当使用 pandoc 从 .Rmd 文件生成 .html 文件时,在浏览器中运行似乎会丢失所有代码包装和绘图.rmd 文件。这是我的两个脚本:

mdown.Rmd:

Title
========================================================

This is an R Markdown document. Markdown is a simple formatting syntax for authoring web pages (click the **MD** toolbar button for help on Markdown).

When you click the **Knit HTML** button a web page will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r}
summary(cars)
```

---

You can also embed plots, for example:

```{r fig.width=7, fig.height=6}
plot(cars)
```

以及运行它的脚本:

# Load packages
setwd("~Documents")
require(knitr)
require(markdown)

# Create slides
knit('mdown.Rmd')
system('pandoc -s -t slidy mdown.Rmd -o mdown.html')

这里也是pandoc生成的html文件

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="generator" content="pandoc" />
  <link rel="stylesheet" type="text/css" media="screen, projection, print" 
    href="http://www.w3.org/Talks/Tools/Slidy/slidy.css" /> 
  <script src="http://www.w3.org/Talks/Tools/Slidy/slidy.js.gz"
    charset="utf-8" type="text/javascript"></script>
</head>
<body>
<div class="slide">

<h1>Title</h1>
<p>This is an R Markdown document. Markdown is a simple formatting syntax for authoring web pages (click the <strong>MD</strong> toolbar button for help on Markdown).</p>
<p>When you click the <strong>Knit HTML</strong> button a web page will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:</p>
<p><code>{r} summary(cars)</code></p>
</div>

<div class="slide">

<p>You can also embed plots, for example:</p>
<p><code>{r fig.width=7, fig.height=6} plot(cars)</code></p>
</div>
</body>
</html>

我哪里错了?

【问题讨论】:

    标签: html r knitr pandoc


    【解决方案1】:

    您正在使用 pandoc 处理 .Rmd 文件,您应该在其中处理通过 knitr 运行 mdown.Rmd 后创建的 .md 文件。

    knit('mdown.Rmd')
    system('pandoc -s -t slidy mdown.md -o mdown.html')
    

    .Rmd 文件就是您上面显示的原始文件。

    【讨论】:

      猜你喜欢
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      • 2015-09-24
      相关资源
      最近更新 更多