2017-08-09 78 views
0

我有這個閃亮的應用程序,我在繪製幾個dygraphs。不幸的是,我不知道有多少劇情會有劇情。它可能會不時變化。所以我想出了使用uiOutputrenderUI來構建一個應對圖的數量作出反應的應用程序。見https://shiny.rstudio.com/articles/dynamic-ui.htmldygraphs傳說外面劇情閃亮的應用程序多個劇情

現在我wnat顯示每個dygraph相應樣外面的傳說正如如下所示:現在Is there a way to add legend next to a dygraph in R, not exactly on the plot?

我的問題是,傳說中的<div>要素不具有相同的高度的人的情節。

我的代碼是:

UI:

library(dygraphs) 
shinyUI(fluidPage(
titlePanel("Simple example"), 
sidebarLayout(
    sidebarPanel(), 
    mainPanel(
    fluidRow(column(10, uiOutput("graphs")), 
      column(2, uiOutput("legends"))) 
    ) 
) 
)) 

服務器:

library(dygraphs) 
library(xts) 

shinyServer(function(input, output, session) { 

# load xts sample data 
data("sample_matrix") 
sample.xts <- as.xts(sample_matrix) 

output$graphs <- renderUI({ 
plot_output_list <- lapply(1:3, function(i) { 
    dygraphOutput(paste0('div_graph_', i)) 
}) 
}) 

output$legends <- renderUI({ 
legend_output_list <- lapply(1:3, function(i) { 
    htmlOutput(paste0("div_legende",i), height = "400px") 
}) 
}) 

# do the plotting 
lapply(1:3, function(i) { 
    output[[paste0('div_graph_', i)]] <- renderDygraph({ 
    dygraph(sample.xts[,i],main=i)%>% 
    dyLegend(labelsDiv = paste0("div_legende",i), show = "always") 
    }) 
}) 
}) 

這導致該地塊,在這裏你可以看到所有三個地塊的傳說是直接粘貼在一起。我希望他們對他們各自的情節是正確的。

sample plot with legend

回答

0

我知道了。 創建一個plotOutput和一個空的plot伎倆:

Ui保持不變。服務器:

library(dygraphs) 
library(xts) 

shinyServer(function(input, output, session) { 

data("sample_matrix") 
sample.xts <- as.xts(sample_matrix) 

output$graphs <- renderUI({ 
    plot_output_list <- lapply(1:3, function(i) { 
    dygraphOutput(paste0('div_graph_', i)) 
}) 
}) 

output$legends <- renderUI({ 
legend_output_list <- lapply(1:3, function(i) { 
    plotOutput(paste0("div_legende",i), height = "400px") 
}) 
}) 

lapply(1:3, function(i) { 
    output[[paste("div_legende",i)]] <- renderPlot(
    plot(1,1,type="n",xaxt="n",yaxt="n",ylab="",xlab="",bty="n"), 
    height = "400px" 
) 
    output[[paste0('div_graph_', i)]] <- renderDygraph({ 
    dygraph(sample.xts[,i],main=i)%>% 
    dyLegend(labelsDiv = paste0("div_legende",i), 
      show = "always") 
    }) 
}) 
})