如何使用拖放动态GGPLOT创建闪亮的应用?

发布于 2025-02-10 05:55:10 字数 1783 浏览 1 评论 0原文

我想创建一个闪亮的应用程序,该应用程序显示一个可变(动态)数量的GGPLOTS,并且我希望能够重新排列(拖放)这些GGPLOTS。我查看了手册的sortable package()and( sortable_js参考)但是我尚不清楚我如何清楚应该在闪亮的app.r文件中实现此功能。

这是基于这篇文章,但它不起作用:


library(shiny)
library(tidyverse)
library(sortable)

# ui ---- 
ui <- fluidPage(

    # Application title
    titlePanel("the ggplot sorting app"),

    fluidRow(
      column(12, 
             uiOutput("plotCollection"))
    )
)

# server ---- 
server <- function(input, output) {
  plot_data <- mtcars
  
  output$plotCollection <- renderUI({
    n = nrow(plot_data)
    
    plot_output_list <- lapply(X=1:n, FUN=function(i) {
      plotOutput(paste0("plot", i), height = 80)
    })
    sortable_js(do.call(function(...) div(id="plotCollection", ...), plot_output_list))
  })
  
  # observers ---- 
  
  # generate multiple separate plots so they can be dragged-and-dropped
  observe({
    for(idx in 1:nrow(plot_data)) {
      local({
        local_id <- idx
        
        data_plot <- plot_data %>%
          slice(local_id)
        
        output[[paste0("plot", local_id)]] <- renderPlot({
          ggplot(data = data_plot, mapping = aes(x=disp, y=wt)) + 
            geom_point() + 
            labs(title = row.names(data_plot))
        })
      })
    }
  })
}

# Run the application 
shinyApp(ui = ui, server = server)


I want to create a shiny app that displays a variable (dynamic) number of ggplots, and I want to be able to rearrange (drag-and-drop) those ggplots. I looked at the manuals for the sortable package (sortable reference) and (sortable_js reference) but it is not clear how I should go about implementing this functionality in a shiny app.R file.

Here's a first attempt based on this post and this post but it doesn't work:


library(shiny)
library(tidyverse)
library(sortable)

# ui ---- 
ui <- fluidPage(

    # Application title
    titlePanel("the ggplot sorting app"),

    fluidRow(
      column(12, 
             uiOutput("plotCollection"))
    )
)

# server ---- 
server <- function(input, output) {
  plot_data <- mtcars
  
  output$plotCollection <- renderUI({
    n = nrow(plot_data)
    
    plot_output_list <- lapply(X=1:n, FUN=function(i) {
      plotOutput(paste0("plot", i), height = 80)
    })
    sortable_js(do.call(function(...) div(id="plotCollection", ...), plot_output_list))
  })
  
  # observers ---- 
  
  # generate multiple separate plots so they can be dragged-and-dropped
  observe({
    for(idx in 1:nrow(plot_data)) {
      local({
        local_id <- idx
        
        data_plot <- plot_data %>%
          slice(local_id)
        
        output[[paste0("plot", local_id)]] <- renderPlot({
          ggplot(data = data_plot, mapping = aes(x=disp, y=wt)) + 
            geom_point() + 
            labs(title = row.names(data_plot))
        })
      })
    }
  })
}

# Run the application 
shinyApp(ui = ui, server = server)


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独夜无伴 2025-02-17 05:55:10

我整理了(双关语意图),sortable_js()需要将调用移动到ui部分,并且需要将图放入tagList < /代码>。有了这些小更改,我现在有一个工作示例,可以保存为app.r并作为独立应用程序运行。

生成图的循环的将花费大量地块需要一段时间,并且可能会进一步优化:

library(shiny)
library(tidyverse)
library(sortable)

# ui ---- 
ui <- fluidPage(
    titlePanel("the ggplot sorting app"),

    fluidRow(
      column(12, 
             uiOutput("plotCollection"),
             sortable_js("plotCollection"))
    )
)

# server ---- 
server <- function(input, output) {
  plot_data <- mtcars
  
  output$plotCollection <- renderUI({
    n = nrow(plot_data)
    
    plot_output_list <- lapply(X=1:n, FUN=function(i) {
      plotOutput(paste0("plot", i), height = 80)
    })
    do.call(tagList, plot_output_list)
  })
  
  # observers ---- 
  
  # generate multiple separate plots so they can be dragged-and-dropped
  # see: 
  observe({
    for(idx in 1:nrow(plot_data)) {
      local({
        local_id <- idx
        
        data_plot <- plot_data %>%
          slice(local_id)
        
        output[[paste0("plot", local_id)]] <- renderPlot({
          ggplot(data = data_plot, mapping = aes(x=disp, y=wt)) + 
            geom_point() + 
            labs(title = row.names(data_plot))
        })
      })
    }
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

I sorted out (pun intended) that the sortable_js() call needed to be moved to the ui section, and the plots needed to be placed into a tagList. With those small changes, I now have a working example which can be saved as app.R and run as a standalone app.

The for() loop that generates the plots will take quite some time with a large number of plots and probably could be optimized further:

library(shiny)
library(tidyverse)
library(sortable)

# ui ---- 
ui <- fluidPage(
    titlePanel("the ggplot sorting app"),

    fluidRow(
      column(12, 
             uiOutput("plotCollection"),
             sortable_js("plotCollection"))
    )
)

# server ---- 
server <- function(input, output) {
  plot_data <- mtcars
  
  output$plotCollection <- renderUI({
    n = nrow(plot_data)
    
    plot_output_list <- lapply(X=1:n, FUN=function(i) {
      plotOutput(paste0("plot", i), height = 80)
    })
    do.call(tagList, plot_output_list)
  })
  
  # observers ---- 
  
  # generate multiple separate plots so they can be dragged-and-dropped
  # see: 
  observe({
    for(idx in 1:nrow(plot_data)) {
      local({
        local_id <- idx
        
        data_plot <- plot_data %>%
          slice(local_id)
        
        output[[paste0("plot", local_id)]] <- renderPlot({
          ggplot(data = data_plot, mapping = aes(x=disp, y=wt)) + 
            geom_point() + 
            labs(title = row.names(data_plot))
        })
      })
    }
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文