我想知道我是否可以在 .Rnw 中使用 ggvis 和 knitr。我在 RStudio Version 0.98.1091
中尝试了以下代码。但它不起作用。
\documentclass{book}
\usepackage[T1]{fontenc}
\usepackage{float}
\usepackage{booktabs}
\usepackage{dcolumn}
\usepackage{geometry}
\geometry{verbose,tmargin=2cm,bmargin=2cm,lmargin=2cm,rmargin=2cm}
\begin{document}
\chapter{Test}
\begin{figure}[H]
<< label = Plot1, fig.lp = "Plot1", fig.cap = "Test Plot" >>=
library(ggvis)
p <- mtcars %>% ggvis(x = ~wt, y = ~mpg) %>% layer_points()
print(p) # Commenting this line will compile the document
@
\end{figure}
\end{document}
它抛出以下错误:
LaTeX errors:
! Missing $ inserted.
<inserted text>
$
l.70 \end{kframe}<!--html_
preserve--><div id="plot_id298740869-container" cl...
! Please use \mathaccent for accents in math mode.
已编辑
注释 print(p)
行将编译文档而不会出现任何错误。
如果有像 ggsave()
这样的命令来保存 ggvis
图就足够了。
最佳答案
是的。
export_png
函数可以从 ggvis
对象创建 PNG 图像。
它使用node
javascript解释器,node
需要安装vega
包。
在 linux 命令行中,我可以这样做:
sudo npm -g install vega
使用节点包管理器全局安装 vega
包。我不知道您如何在 Windows 或 Mac 机器上执行此操作。
完成后,您可以:
\documentclass{book}
\usepackage[T1]{fontenc}
\usepackage{float}
\usepackage{booktabs}
\usepackage{dcolumn}
\usepackage{geometry}
\geometry{verbose,tmargin=2cm,bmargin=2cm,lmargin=2cm,rmargin=2cm}
\begin{document}
\chapter{Test}
\begin{figure}[H]
<< label = Plot1, fig.lp = "Plot1", fig.cap = "Test Plot" >>=
library(ggvis)
p <- mtcars %>% ggvis(x = ~wt, y = ~mpg) %>% layer_points()
export_png(p,"Plot1.png")
@
\includegraphics[width=0.8\textwidth]{Plot1.png}
\end{figure}
\end{document}
做:knit2pdf("gg.Rnw")
并得到:
请注意,您必须手动添加标题和标签。也许可以说服 Yihui 将其更好地集成到 knitr 中,或者可能有一种使用 knitr hooks 的方法。不管怎样,概念证明了...
关于r - 在 Rnw 中使用 ggvis 和 knitr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27193287/