2017-06-14 146 views
0

我想繪製20循環閃亮的圖形,我不想一個接一個地寫輸出。所以我正在做一個循環來輸出這些圖。我在閃亮的畫廊中找到了一個很好的例子,它展示瞭如何輸出文本。我試了它,它的工作。R shiny:如何循環輸出plotly圖

現在我的問題是:如何將文本輸出替換爲plotly?我已經準備好了(爲了簡化我沒有在這裏展示)。我所嘗試的是首先用我的情節對象替換strong(paste0(..行。其次,將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)) 
     }) 

     ) 

回答

1

檢查出這個示例閃亮的應用程序,顯示圖的動態數:https://gist.github.com/wch/5436415/

我適於上述應用用gglotlot繪製汽車數據集。

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 
     }) 
    } 
) 
+0

我應該與ui.r呢? – user3833612

+0

ui包含在上面的代碼中。我添加了評論,使每個部分更清晰。如果你願意的話,你可以把代碼分解成一個ui.R和server.R。 – blondeclover

+0

哦,對不起,我錯過了開始的ui部分。是的,我根據您的示例修改了我的代碼,現在它工作正常。謝謝! – user3833612