简体   繁体   English

从Shiny,Plotly-R中提取所有点击事件图

[英]Extract all click event plots from Shiny, Plotly - R

In the following shiny app, the plotly package is used to create an interactive correlation heat map. 在下面的shiny应用程序中, plotly包用于创建交互式相关热图。 When individual tiles are clicked, the corresponding scatter plot appears. 单击单个图块时,将显示相应的散点图。 One can then download the individual scatters by clicking download plot as png . 然后,可以通过单击download plot as pngdownload plot as png单个download plot as png But is there a way to download all the possible scatter plots at once without having to click each individual tile and save each individual one? 但有没有办法一次下载所有可能的散点图而不必点击每个单独的瓷砖并保存每个单独的瓷砖? Thank you 谢谢

library(plotly)
library(shiny)

# compute a correlation matrix
correlation <- round(cor(mtcars), 3)
nms <- names(mtcars)

ui <- fluidPage(
  mainPanel(
    plotlyOutput("heat"),
    plotlyOutput("scatterplot")
  ),
  verbatimTextOutput("selection")
)

server <- function(input, output, session) {
  output$heat <- renderPlotly({
    plot_ly(x = nms, y = nms, z = correlation, 
            key = correlation, type = "heatmap", source = "heatplot") %>%
      layout(xaxis = list(title = ""), 
             yaxis = list(title = ""))
  })

  output$selection <- renderPrint({
    s <- event_data("plotly_click")
    if (length(s) == 0) {
      "Click on a cell in the heatmap to display a scatterplot"
    } else {
      cat("You selected: \n\n")
      as.list(s)
    }
  })

  output$scatterplot <- renderPlotly({
    s <- event_data("plotly_click", source = "heatplot")
    if (length(s)) {
      vars <- c(s[["x"]], s[["y"]])
      d <- setNames(mtcars[vars], c("x", "y"))
      yhat <- fitted(lm(y ~ x, data = d))
      plot_ly(d, x = ~x) %>%
        add_markers(y = ~y) %>%
        add_lines(y = ~yhat) %>%
        layout(xaxis = list(title = s[["x"]]), 
               yaxis = list(title = s[["y"]]), 
               showlegend = FALSE)
    } else {
      plotly_empty()
    }
  })

}

shinyApp(ui, server)

You can use webshot to capture a static image of Plotly's HTML output using the instructions here: https://plot.ly/r/static-image-export/ 您可以使用webshot使用webshot说明捕获Plotly的HTML输出的静态图像: https ://plot.ly/r/static-image-export/

An example for loop below generates random scatter plots from mtcars . 下面循环的示例生成来自mtcars随机散点图。

library(plotly)
library(webshot)

## You'll need to run the function the first time if you dont't have phantomjs installed
#webshot::install_phantomjs()
ColumnOptions <- colnames(mtcars)

for (i in seq_len(5)){
  xCol <- sample(ColumnOptions,1)
  yCol <- sample(ColumnOptions,1)
  ThisFileName <- paste0("Scatter_",xCol,"_vs_",yCol,".png")

  plot_ly(x = mtcars[[xCol]], y = mtcars[[yCol]], type = "scatter", mode = "markers") %>% 
    export(., file = ThisFileName)
}

However, if you're going to be potentially doing this dozens of times, the amount of computation required to go through the following steps really adds up. 但是,如果您可能会执行此操作数十次,则执行以下步骤所需的计算量确实会增加。

  1. Generate a JSON plotly object from R R生成JSON plotly对象
  2. Use htmlwidgets / htmltools to generate a self-contained HTML web page 使用htmlwidgets / htmltools生成一个自包含的HTML网页
  3. Render that HTML as a browser would see it with an external program -- webshot 将HTML作为浏览器呈现为外部程序 - webshot
  4. Use webshot to render an image of that HTML and save it as a PNG 使用webshot呈现该HTML的图像并将其另存为PNG

This isn't really a reflection of plotly being slow, but to make an analogy it's kind've like using an airplane to travel half a mile -- the plane gets you there, but if you need to make that trip more than a few times you should probably consider a car. 这并不是plotly缓慢的反映,但作为一个类比,它有点像使用飞机旅行半英里 - 飞机让你到达那里,但如果你需要让这次旅行超过一些你应该考虑一辆车。

The plotly loop above takes 27 seconds to render 5 PNG images, but the alternative method below using ggplot2 takes 1.2 seconds. 上面的plotly循环需要27秒来渲染5个PNG图像,但下面使用ggplot2的替代方法需要1.2秒。

library(ggplot2)

ColumnOptions <- colnames(mtcars)

for (i in seq_len(5)){
  xCol <- sample(ColumnOptions,1)
  yCol <- sample(ColumnOptions,1)
  ThisFileName <- paste0("ggplot2_Scatter_",xCol,"_vs_",yCol,".png")

  ggplot() + 
    geom_point(aes(x = mtcars[[xCol]], y = mtcars[[yCol]])) +
    labs(x = xCol, y = yCol) -> ThisPlot 

  ggsave(plot = ThisPlot, filename = ThisFileName)
}

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

相关问题 R绘图+闪亮的反应耦合事件-刷新图表并单击同一图表上的参数 - R plotly + shiny reactive coupled event - Refresh chart with argument from click on same chart Shiny R:plotly 中的绘图显示错误 - Shiny R: bad display of plots in plotly R Shiny error:如何处理两个情节图中的点击事件? - R Shiny error: How to work with the click events incase of two plotly plots? 一个 R 闪亮的应用程序,显示 ggplot 图和情节图 - An R shiny app that displays both ggplot plots and plotly plots 如何在 R Shiny 中显示来自 plotly_click 的许多点? - How to display many points from plotly_click in R Shiny? 当已经具有 event_data(&quot;plotly_click&quot;) 时,如何使用 r、plolty、shin 为条形图中单击的条着色 - How to color a clicked bar from barchart with r, plolty, shiny when having already event_data("plotly_click") 如何将来自不同来源/数据集的绘图(线/迹线)动态添加到 R (Shiny) 中的绘图对象? - How to dynamically add plots (lines/traces) from different sources/datasets to a plotly object in R (Shiny)? 使用 2 个 SunburstR 图将点击事件添加到 shiny 应用程序 - Add click event to shiny app with 2 SunburstR plots 将 plotly_click 应用于 shiny 应用程序中的 2 个以上图 - Apply plotly_click to more than 2 plots in a shiny app R闪闪发亮的图例点击事件 - R shiny and plotly getting legend click events
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM