在带有变量的 rMarkdown 中使用 ggvis 的问题 [英] Problems using ggvis in rMarkdown with variables

查看:40
本文介绍了在带有变量的 rMarkdown 中使用 ggvis 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有几个问题

a) 创建正确的文本以将变量传递给 ggvis - 甚至不确定 aes_string 是否适用

a) creating the correct text to pass variables to ggvis - not even sure aes_string is applicable

b) 绘图在浏览器中传播而不是在 rmarkdown 文档中呈现

b) The plot propagates in browser rather than rendering in the rmarkdown document

这是一个例子

---
title: "Untitled"
author: "pssguy"
date: "Sunday, August 24, 2014"
output: html_document
runtime: shiny
---
```{r, echo = FALSE, message=FALSE}
library(ggplot2)
library(ggvis)
library(dplyr)

selectInput("category3", "Choose Dataset:", c("mpg", "disp", "qsec"))

# ggplot renders correctly within renderPlot
  renderPlot({
   ggplot(mtcars,aes_string(input$category3,"disp"))+geom_point() 
  })

# ggvis works within document with hard coded info
   mtcars %>% ggvis(~wt,~disp)


mtcars %>% ggvis(aes_string(paste("~",input$category3,","),"~disp"))
#Operation not allowed without an active reactive context. (You tried to do something     that can only be done from inside a reactive expression or observer.)


# This needs correcting anyways
renderPlot({
   mtcars %>% ggvis(aes_string(paste("~",input$category3,","),"~disp"))
 })
# <text>:1:7: unexpected ',' 1: ~ mpg ,


# even if the above is corrected the plot opens in a browser rather than the document
renderPlot({
 mtcars %>% ggvis(~wt,~disp)
  })
```

TIA

推荐答案

应该这样做:

---
title: "Untitled"
output: html_document
runtime: shiny
---

```{r, echo = FALSE, message=FALSE}
library(ggplot2)
library(ggvis)
library(dplyr)

selectInput("category3", "Choose Dataset:", c("mpg", "disp", "qsec"))

# ggplot renders correctly within renderPlot
renderPlot({
  print(input$category3)
  ggplot(mtcars,aes_string(input$category3,"disp"))+geom_point()
})

# ggvis with dynamically changing columns
reactive({
  if (!is.null(input$category3))
    col <- input$category3
  else
    col <- "mpg"

  mtcars %>% ggvis(prop("x", as.name(col)), ~disp)

}) %>% bind_shiny('foo')

ggvisOutput('foo')
```

有点复杂,因为您需要对类别进行NULL检查,并且您需要明确告诉 knitr 在页面上放置 ggvis 输出.

It's a little complicated because you need a NULL check for the category, and you need to explicitly tell knitr to put a ggvis output on the page.

这篇关于在带有变量的 rMarkdown 中使用 ggvis 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆