在Rnw中使用Knitr使用ggvis [英] Using ggvis in Rnw with knitr
问题描述
我想知道是否可以在knitr的.Rnw中使用ggvis.我在RStudio Version 0.98.1091
中尝试了以下代码.但这不起作用.
I wonder if I can use ggvis in .Rnw with knitr. I tried the following code in RStudio Version 0.98.1091
. But it is not working.
\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)
行将编译文档而没有任何错误.
Commenting the line print(p)
will compile the document without any error.
如果有类似ggsave()
的命令来保存ggvis
图,就足够了.
Would be sufficient if there is a command like ggsave()
to save the ggvis
plots.
推荐答案
是.
export_png
函数可以从ggvis
对象创建PNG图像.
The export_png
function can create a PNG image from a ggvis
object.
它使用node
javascript解释器,并且node
需要安装vega
软件包.
It uses the node
javascript interpreter, and node
needs the vega
package installed.
在linux命令行上,我可以这样:
At the linux command line, I can do this with:
sudo npm -g install vega
使用节点程序包管理器全局安装vega
程序包.我不知道您如何在Windows或Mac电脑上执行此操作.
to install the vega
package globally using the node package manager. I don't know how you do this on a Windows or Mac box.
完成后,您可以:
\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将此更好地集成到编织器中,或者可能有一种使用某些编织钩的方法.无论如何,概念证明了……
Note you'll have to add captions and labels manually. Perhaps Yihui can be persuaded to integrate this better into knitr, or there may be a way using some of the knitr hooks. Anyway, concept proved...
这篇关于在Rnw中使用Knitr使用ggvis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!