简体   繁体   English

在R markdown中嵌入使用函数生成的googleVis图表

[英]Embed in R markdown a googleVis chart generated using a function

I have a function that creates the same type of googleVis plot for different data frames. 我有一个函数,可以为不同的数据框创建相同类型的googleVis图。 These plots are to be embedded in a Markdown file. 这些图将嵌入在Markdown文件中。 Embedding a single plot with the result='asis' option fails to achieve my objective when the chart object is created by my function. 当通过函数创建图表对象时,使用result='asis'选项嵌入单个图无法实现我的目标。 Following is a dummy code for the same: 以下是相同的伪代码:

Embedded googleVis plots
=====================

Some text here

```{r}
library(googleVis)
op <- options(gvis.plot.tag="chart")
```

And now the plot

```{r result='asis', tidy=TRUE}
mark_func <- function(data) {
    data$Mean=mean(data$Popularity)
    CC <- gvisComboChart(data, xvar='City',
          yvar=c('Mean', 'Popularity'),
          options=list(seriesType='bars',
                       width=450, height=300,
                       title='City Popularity',
                       series='{0: {type:"line"}}'))
    return(CC)
}
```

```{r result='asis', tidy=TRUE}
plt <- mark_func(CityPopularity)
plot(plt)`
```  

I am converting this Markdown file to a HTML using knit2html from the knitr package and viewing this HTML in Firefox. 我正在使用knitr软件包中的knit2html将此Markdown文件转换为HTML,并在Firefox中查看此HTML。 Instead of seeing a plot, I see a long HTML code. 我没有看到情节,而是看到了很长的HTML代码。

What is it that I am missing? 我想念的是什么? Thanks for your help. 谢谢你的帮助。

It is just typos. 这只是错别字。 You are forgetting to escape your quotemarks at the end and the code chunk option should be results , not result . 您忘记了最后使用引号引起来,并且代码块选项应该是结果 ,而不是结果

Working code: 工作代码:

%\VignetteEngine{knitr::knitr}

```{r}
library(googleVis)
op <- options(gvis.plot.tag="chart")
```

```{r results='asis', tidy=TRUE}
mark_func <- function(d) {
    d$Mean=mean(d$Popularity)
    CC <- gvisComboChart(d, xvar='City',
          yvar=c('Mean', 'Popularity'),
          options=list(seriesType='bars',
                       width=450, height=300,
                       title='City Popularity',
                       series='{0: {type:\"line\"}}'))
    return(CC)
}
```

```{r results='asis', tidy=TRUE}
plt <- mark_func(CityPopularity)
plot(plt)
```  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM