如何使用拖放动态GGPLOT创建闪亮的应用?
我想创建一个闪亮的应用程序,该应用程序显示一个可变(动态)数量的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我整理了(双关语意图),
sortable_js()
需要将调用移动到ui
部分,并且需要将图放入tagList < /代码>。有了这些小更改,我现在有一个工作示例,可以保存为app.r并作为独立应用程序运行。
生成图的循环的
将花费大量地块需要一段时间,并且可能会进一步优化:
I sorted out (pun intended) that the
sortable_js()
call needed to be moved to theui
section, and the plots needed to be placed into atagList
. 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: