2016-09-24 72 views
2

http://rmarkdown.rstudio.com/authoring_shiny.html上的示例指示renderPlot本身會將該圖繪製爲降價。我們如何獲得降價以允許互動,如點擊,刷子等,這些互動被宣佈爲隨後的plotOutput步驟的一部分?Markdown中的交互式ggplot

plotOutputshiny這裏的互動示例 - 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%" 

    ) 
}) 

``` 

回答

0

的問題是,您使用的參數heightplotOutput與百分比。我們可以在文檔中?shiny::plotOutput發現:

需要注意的是,對於高度,使用「自動」或「100%」一般不會達到預期效果,因爲高度是如何計算與HTML/CSS。

如果刪除height = 100%(在本例中爲冗餘),將繪製該圖。如果你想改變輸出的高度,你可以使用像素而不是百分數。

然後,您可以通過input$Click1input$DblClick1input$Brush1訪問值,並將它們傳遞給渲染*函數。


實施例:

--- 
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) 
}) 
```