【问题标题】:Interactive ggplot in MarkdownMarkdown 中的交互式 ggplot
【发布时间】:2017-02-01 23:58:35
【问题描述】:

http://rmarkdown.rstudio.com/authoring_shiny.html 上的示例表明renderPlot 本身将绘图渲染为降价。我们如何获得我们的降价以允许在后续plotOutput 步骤中声明的交互,例如点击、画笔等?

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

代码 sn-p -

```{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%"

   )
})

```

【问题讨论】:

    标签: r shiny markdown r-markdown


    【解决方案1】:

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

    请注意,对于高度,使用“auto”或“100%”通常不会按预期工作,因为高度是使用 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)
    })
    ```
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      • 2019-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多