使用循环在Shiny应用程序中生成多个gvis图表

时间:2014-04-29 00:47:51

标签: r shiny

我正在尝试生成多个gvis图表,而不必在$outputs文件中列出大量server.R

server.R

max_plots <- 30

output$plots <- renderUI({
plot_output_list <- lapply(1:input$n, function(i) {
    plotname <- paste("plot", i, sep="")
    plotOutput(plotname, height = 400, width = 7000)
})

do.call(tagList, plot_output_list)

})

for (i in 1:max_plots) {
# Need local so that each item gets its own number. Without it, the value
# of i in the renderPlot() will be the same across all instances, because
# of when the expression is evaluated.
local({
    my_i <- i
    plotname <- paste("plot", my_i, sep="")

    output[[plotname]] <- renderGvis({

        t <- as.data.frame(getClusters[5])

        t <- as.data.frame(t[i,])

        t <- getSnapFrame(t)

        gvisColumnChart(t, xvar = "Feature", yvar = "KPIs", options=list(width=600, height=270, backgroundColor='transparent',  hAxis.gridlines.color="green", colors="['yellowgreen']", fontSize=10, hAxis="{title:'Features', titleTextStyle:{color:'white'}, textStyle:{color: 'white'}}", vAxis="{title:'Relation to Average', titleTextStyle:{color:'white', fontSize: 16}, textStyle:{color: 'white'}}", tooltip="{textStyle: {color: '#0276FD', fontSize: 16}}", legend.position="none"))
    })
})

}

ui.R

sidebarPanel(
sliderInput("n", "Number of plots", value=1, min=1, max=5)
),

uiOutput("plots"), # This is the dynamic UI for the plots

0 个答案:

没有答案