2016-12-27 20 views
0

我无法将在R sweave中通过plotly创建的图表转换为Pdf。但是可以创建其他图表。是否还有其他附加代码需要添加到绘图图表中?无法编译从R sweave到Plotly的图表

下面是一个例子

\documentclass{article} 
\begin{document} 
\SweaveOpts{concordance=TRUE} 



<<echo = FALSE>>= 
C1<-c("A","B","C") 
C2<-c(10,20,30) 
Df<-data.frame(C1,C2) 
@ 

\begin {figure} 

<<echo = FALSE, fig = TRUE>>= 
suppressWarnings (library (plotly)) 
plot_ly(Df, x = ~C1, y = ~C2, type = "bar")%>% layout(title = "ABC") 
@ 

\caption {PlotlyChart} 
\end {figure} 

Normal Chart 

\begin {figure} 

<<echo = FALSE, fig = TRUE>>= 
barplot(Df[ ,2], names.arg = Df[ ,1]) 
@ 

\caption {Normal_Chart} 
\end {figure} 

\end{document} 

当我尝试R中正常的箱形图,它的工作原理。只有当我添加Plotly图表,我得到一个错误消息

Running pdflatex on ABC.tex...failed 

Error running /Library/TeX/texbin/pdflatex (exit code 1) 

我是新至R sweave和乳胶,所以任何帮助表示赞赏。谢谢!

回答

2

Plotly生成绘制图的JavaScript代码。此代码将在Web浏览器中执行。 Plotly仅在生成HTML文档时可用。没有PDF,没有乳胶。

但是,您可以使用包webshotphantomjs作为图像保存绘图,然后将图像包含在图中。

首先安装webshot:

install.packages("webshot") 
webshot::install_phantomjs() 

然后修改你Sweave文件是这样的:

\documentclass{article} 
\begin{document} 
\SweaveOpts{concordance=TRUE} 

<<echo = FALSE>>= 
C1<-c("A","B","C") 
C2<-c(10,20,30) 
Df<-data.frame(C1,C2) 
@ 

<<echo = FALSE, fig = FALSE>>= 
suppressWarnings (library (webshot)) 
suppressWarnings (library (plotly)) 
py <- plot_ly(Df, x = ~C1, y = ~C2, type = "bar")%>% layout(title = "ABC") 
export(py, file = "myplot.png") 
@ 
\begin {figure} 
\includegraphics{myplot.png} 
\end {figure} 

\end{document} 
+1

非常有趣的解决方案。另外值得注意的是,'phantomjs'可以作为独立的终端工具使用,如果你想简单地生成Plotly HTML输出的静态图像,这可能会很有用。 http://phantomjs.org/screen-capture.html – user5359531

+0

@HubertL - 像魔术般运作。感谢您的解决方案。 – Jessie