[英]R shiny: How to loop output plotly graphs
I want to draw 20 graphs in shiny by loop and I don't want to write the output one by one. 我想在循环中绘制20个图形,我不想逐个编写输出。 So I am thing doing a loop to output these graphs.
所以我正在做一个输出这些图的循环。 I found a very good example in the shiny gallery which shows how to output texts.
我在闪亮的画廊中找到了一个很好的例子,展示了如何输出文本。 I tiried it and it worked.
我把它弄好了,它起作用了。
Now my problem is: How can I replace the text output to plotly? 现在我的问题是:如何将文本输出替换为plotly? I have the plotly ready(to simplify I am not showing here).
我已经准备好了(为了简化我没有在这里展示)。 What I tried is first replace the
strong(paste0(..
line with my plotly object. Second, replace renderUI
to renderplotly
and replace uiOutput
to plotOutput
. I am getting errors ggplotly has no applicable method for shiny.tag
which I understand that plotOutput is not compatible with tagged output. So what can I do here? 我尝试的是首先替换
strong(paste0(..
与我的plotly对象的行。第二,将renderUI
替换为renderplotly
并将uiOutput
替换为plotOutput
。我收到错误ggplotly has no applicable method for shiny.tag
,我理解plotOutput是与标记输出不兼容。那么我该怎么办?
server.r: server.r:
shinyServer(function(input, output,session) {
lapply(1:2, function(i) {
output[[paste0('b', i)]] <- renderUI({
strong(paste0('Hi, this is output B#', i)) })# to be replaced with a plotly object p
})})
ui.r: ui.r:
fluidRow(
lapply(1:2, function(i) {
uiOutput(paste0('b', i))
})
)
Check out this example Shiny app that displays a dynamic number of plots: https://gist.github.com/wch/5436415/ 看看这个示例Shiny应用程序,它显示动态数量的图: https : //gist.github.com/wch/5436415/
I adapted the above app to plot the cars dataset with ggplotly. 我改编了上面的应用程序,用ggplotly绘制汽车数据集。
library(shiny)
library(ggplot2)
library(plotly)
shinyApp(
##### ui #######
ui = fluidPage(
fluidRow(
sliderInput("n",
"Number of plots",
value = 1, min = 1, max = 5)),
fluidRow(
uiOutput("plots"))
),
##### server ######
server = function(input, output) {
data("cars")
# define max number of plots
max_plots <- 5
# generate the plots
output$plots <- renderUI({
plot_output_list <- lapply(1:input$n, function(i) {
plotname <- paste0("plot", i)
plotlyOutput(plotname)
})
# convert the list to a tagList - this is necessary for the list of
# items to display properly
do.call(tagList, plot_output_list)
})
# call renderPlotly for each plot. Plots are only generated when they are
# visible on the web page
for(i in 1:max_plots) {
# Need local so that each item gets its own number. Without it, the value
# of i in the renderPlotly() will be the same across all instances, because
# of when the expression is evaluated
local({
my_i <- i
plotname <- paste0("plot", my_i)
output[[plotname]] <- renderPlotly({
g <- ggplot(cars, aes(x = speed, y = dist)) +
geom_point() +
labs(title = paste0("Plot ", my_i))
g <- ggplotly(g)
dev.off()
g
})
})
}
}
)
library(shiny) library(ggplot2) library(plotly) library(grid) shinyApp( ##### ui ####### ui = fluidPage( fluidRow( sliderInput("n", "Number of plots", value = 1, min = 1, max = 5)), fluidRow( plotlyOutput("plots") ) ), ##### server ###### server = function(input, output) { data("cars") # define max number of plots max_plots <- 5 # generate the plots output$plots <- renderPlotly({ plot_list <- lapply(1:input$n, function(i) { g <- ggplot(cars, aes(x = speed, y = dist)) + geom_point() + theme(plot.margin = unit(c(3, 1, 1, 1), "lines")) ggplotly(g) }) p <- subplot(plot_list[1:input$n], shareX = TRUE, shareY = TRUE) %>% layout(title = "Car Plots") dev.off() p }) } )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.