高速图的动态数量

时间:2016-04-19 08:36:40

标签: r highcharts shiny

我跟随this example根据不同的参数(作为数据框列)绘制多个图。所以情况是每天要做的情节数量会有所不同。

我修改了代码以使用Highcharter获取javascript图表而不是基本图表,但它不起作用。

此外,我想知道我要添加到此代码中以绘制2,3或4列中的图表?

由于

ui.R

fluidPage(
  # Application title
  titlePanel("Hello World!"),

  # Show a plot
  fluidRow(      
    column(width = 6,
           highchartOutput("hcontainer", height = "400px")
    ) 
  )
)

server.R

get_plot_output_list <- function() {
    plot_output_list <- lapply(1:NCOL(df), FUN = function(i) {
      plot_output_object <- highchartOutput("hcontainer")
      plot_output_object <- renderHighchart({
        hc <- highchart() %>%
          hc_add_serie(name = "df name", data = df)
        return(hc)
      })
    })
    do.call(tagList, plot_output_list) # needed to display properly.
    return(plot_output_list)
  }

observe({
    output$hcontainer <- renderUI({ get_plot_output_list() })
    #output$hcontainer <- renderHighchart({ get_plot_output_list() })
  })

1 个答案:

答案 0 :(得分:2)

您好,您可以尝试使用此解决方案,它不会使用与您相同的功能,而是使用@jenesaisquoi(找到here),此函数会创建多个图并正确处理布局:

# Packages
library("highcharter")
library("shiny")

# data
df <- data.frame(
  var1 = rnorm(10),
  var2 = rnorm(10),
  var3 = rnorm(10),
  var4 = rnorm(10),
  var5 = rnorm(10),
  var6 = rnorm(10),
  var7 = rnorm(10)
)

# Fun by @jenesaisquoi (modified with highchartOutput)
makePlotContainers <- function(n, ncol=2, prefix="plot", height=100, width="100%", ...) {
  ## Validate inputs
  validateCssUnit(width)
  validateCssUnit(height)

  ## Construct plotOutputs
  lst <- lapply(seq.int(n), function(i)
    highchartOutput(sprintf('%s%g', prefix, i), height=height, width=width))

  ## Make columns
  lst <- lapply(split(lst, (seq.int(n)-1)%/%ncol), function(x) column(12/ncol, x))
  do.call(tagList, lst)
}

您可以直接在ui中使用@ jenesaisquoi的函数,并在服务器中使用lapply来定义与cols数量一样多的输出:

# App
ui <- fluidPage(
  # Application title
  titlePanel("Hello World!"),

  # Show plots
  makePlotContainers(n = ncol(df), ncol = 3, prefix = "hcontainer", height = "400px")
)

server <- function(input, output) {
  lapply(
    X = seq_len(ncol(df)),
    FUN = function(i) {
      output[[paste0("hcontainer", i)]] <- renderHighchart({
        highchart() %>%
          hc_add_serie(name = paste("df name", i), data = df[[i]])
      })
    }
  )
}
shinyApp(ui = ui, server = server)