繁体   English   中英

R有光泽:如何循环输出图形图

[英]R shiny: How to loop output plotly graphs

我想在循环中绘制20个图形,我不想逐个编写输出。 所以我正在做一个输出这些图的循环。 我在闪亮的画廊中找到了一个很好的例子,展示了如何输出文本。 我把它弄好了,它起作用了。

现在我的问题是:如何将文本输出替换为plotly? 我已经准备好了(为了简化我没有在这里展示)。 我尝试的是首先替换strong(paste0(..与我的plotly对象的行。第二,将renderUI替换为renderplotly并将uiOutput替换为plotOutput 。我收到错误ggplotly has no applicable method for shiny.tag ,我理解plotOutput是与标记输出不兼容。那么我该怎么办?

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:

    fluidRow(
         lapply(1:2, function(i) {
           uiOutput(paste0('b', i))
         })

       )   

看看这个示例Shiny应用程序,它显示动态数量的图: https//gist.github.com/wch/5436415/

我改编了上面的应用程序,用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM