Markdown 中的交互式 ggplot

Interactive ggplot in Markdown

http://rmarkdown.rstudio.com/authoring_shiny.html 上的示例表明 renderPlot 本身将情节呈现为降价。我们怎样才能让我们的 markdown 允许点击、刷子等交互,这些交互被声明为后续 plotOutput 步骤的一部分?

此处 shiny 中与 plotOutput 的交互示例 - http://shiny.rstudio.com/articles/plot-interaction.html

代码片段-

```{r, echo = FALSE}

output[['Plot1']] = renderPlot(

   ggplot(mtcars) + geom_point(aes(x = cyl, y = qsec))

) 

renderPlot(

   ggplot(mtcars) + geom_point(aes(x = cyl, y = wt))

)


print("renderPlot above. plotOutput below (which doesn't get rendered).")

renderUI({
   plotOutput(
      'Plot1',
      brush = brushOpts(
         id = 'Brush1'
      ),
      dblclick = dblclickOpts(id = 'DblClick1'),
      click = 'Click1',
      height = "100%"

   )
})

```

问题是您在 plotOutput 中使用带有百分比的参数 height。我们可以在文档中找到?shiny::plotOutput:

Note that, for height, using "auto" or "100%" generally will not work as expected, because of how height is computed with HTML/CSS.

如果删除 height = 100%,在这种情况下是多余的,将渲染图。如果您想更改输出的高度,您可以使用像素而不是百分比。

然后您可以通过 input$Click1input$DblClick1input$Brush1 访问值并将它们传递给 render* 函数。


示例:

---
title: "Example"
author: "Unnamed_User"
date: "24 Sep 2016"
output: html_document
runtime: shiny
---

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

### Normal plot

```{r, echo = FALSE} 
ggplot(mtcars) + geom_point(aes(x = cyl, y = wt))
``` 


### Interactive plot

```{r, echo = FALSE}
renderUI({
   plotOutput(
      'Plot1',
      brush = brushOpts(
         id = 'Brush1'
      ),
      dblclick = dblclickOpts(id = 'DblClick1'),
      click = 'Click1'
   )
})

output[['Plot1']] <- renderPlot({ 
   ggplot(mtcars) + geom_point(aes(x = cyl, y = qsec))
})
```

### Clicked point

```{r, echo = FALSE}
renderPrint({ 
  cat(" x:", input$Click1$x, 
      "\n",
       "y:", input$Click1$y)
})
```