[英]How to render different plot output types in R Shiny dependent of user input?
In R Shiny I'd like to render different plot output types depending of the ticked radiobutton. 在R Shiny中,我想根据选中的单选按钮呈现不同的绘图输出类型。 This is easily implementable as seen in: create plots based on radio button selection R Shiny .
如以下所示,这很容易实现: 基于单选按钮选择R Shiny创建图 。 Now taking this approach further, how to implement this with different output types such as dygraphs and amCharts?
现在,进一步采用这种方法,如何用不同的输出类型(如笔形图和amCharts)来实现呢? An attempt of what I am trying to achieve:
尝试实现的目标:
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)
})
}
))
Thanks in advance 提前致谢
Something like that. 这样的事情。 In
server
: 在
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)
})
In ui
: 在
ui
:
conditionalPanel('input.pType=="A"', plotOutput("plot"))
conditionalPanel('input.pType=="B"', dygraphOutput("dygraph"))
Perhaps the conditional panels are not needed, since plotOutput("plot")
will render nothing is pType
is not A
, and dygraphOutput("dygraph")
will render nothing is pType
is not B
. 也许不需要条件面板,因为
plotOutput("plot")
如果pType
不是A
则什么都不呈现,而dygraphOutput("dygraph")
则pType
不是B
则什么也不呈现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.