【问题标题】:How to render different plot output types in R Shiny dependent of user input?如何根据用户输入在 R Shiny 中渲染不同的绘图输出类型?
【发布时间】:2018-05-24 19:25:03
【问题描述】:

在 R Shiny 中,我想根据勾选的单选按钮呈现不同的绘图输出类型。这很容易实现,如:create plots based on radio button selection R Shiny。现在进一步采用这种方法,如何使用不同的输出类型(例如 dygraphs 和 amCharts)来实现它?我正在尝试实现的目标:

library(shiny)
library(dygraphs)
library(amCharts)

myData <- runif(100)
myData <- ts(runif(72,0,10), start=c(2009, 1), end=c(2014, 12), frequency=12) 
myData 

outType <- function(x,type){
  switch(type,
         A = renderPlot({ hist(x)}),
         B = renderDygraph({ barplot(x)}),
         C = amChartsOutput({ pie(x)}))
}

plotOut <- function(type,plotlabel,data){
  switch(type,
         A = plotOutput(plotlabel,data),
         B = dygraph(plotlabel,data),
         C = plotOutput(plotlabel,data))
}

runApp(list(
  ui = bootstrapPage(
    radioButtons("pType", "Choose plot type:",
                 list("A", "B", "C")),
    plotOut('A','plot',myData)
  ),

  server = function(input, output) {
    observe({
      type <<- input$pType
      output$plot <- outType(myData, input$pType)  
    })
  }
))

提前致谢

【问题讨论】:

    标签: r shiny shinyjs


    【解决方案1】:

    类似的东西。在server:

    output$plot <- renderPlot({
      validate(need(input$pType=="A", message=FALSE))
      hist(myData)
    })
    output$dygraph <- renderDygraph({
      validate(need(input$pType=="B", message=FALSE))
      barplot(myData)
    })
    

    ui:

    conditionalPanel('input.pType=="A"', plotOutput("plot"))
    conditionalPanel('input.pType=="B"', dygraphOutput("dygraph"))
    

    也许不需要条件面板,因为plotOutput("plot") 将不呈现任何内容pType 不是AdygraphOutput("dygraph") 将不呈现任何内容pType 不是B

    【讨论】:

      猜你喜欢
      • 2021-07-27
      • 2019-02-05
      • 2019-07-27
      • 1970-01-01
      • 2021-12-29
      • 2019-05-05
      • 2015-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多