[英]Create input$selection to subset data AND radiobuttons to choose plot type in Shiny
[英]Shiny input plot selection
我有这段代码,我想有一个“选择输入”选项,可以从两种不同的图形绘制方式中进行选择。
我希望如果您选择“级别”选项,则“拟合2”将被绘制,而不仅仅是拟合。
这很合适。
plot(forecast(fit2,
#Confidence Interval %
level = c(70,90)),
sub= "Confidence Interval 70% ~ 90%
or Determined by user",
ylab= "Y Axis Variable",
main= "Forecast Linear Structural Model @ Level-Wise",
ylim = c(0,400))
这是闪亮代码。
library(forecast)
library(shiny)
timese <- ts(WWWusage, start= c(2008,1), end= c(2016,1), frequency=12)
fit <- StructTS(timese,"trend")
fit2 <- StructTS(timese,"level")
ui <- fluidPage(
(titlePanel("app | Forecast Models", windowTitle = "app")),
#Select input
selectInput(inputId = "select", label = "Select Forecast",
choices = c("Trend","Level"),
plotOutput(outputId = "hist")),
#Range Input
sliderInput(inputId = "range",
label = "Set Confidence Interval. Up to 99%",
min = 0,max = 99, value = c(60,90)),
mainPanel(plotOutput(outputId = "plot"))
)
server <- function(input, output) {
output$plot <- renderPlot({
plot(forecast(fit, #Confidence Interval %
level = c(input$range)),
sub= "Confidence Interval 70% ~ 90% or Determined by user",
ylab= "Y Axis Variable",
main= "Forecast Linear Structural Model @ Trend-Wise",
ylim = c(0,400))
})
}
shinyApp(ui, server)
喜欢
server <- function(input, output) {
output$plot <- renderPlot({
if(input$select=="Trend")
plot(forecast(fit, #Confidence Interval %
level = c(input$range)),
sub= "Confidence Interval 70% ~ 90% or Determined by user",
ylab= "Y Axis Variable",
main= "Forecast Linear Structural Model @ Trend-Wise",
ylim = c(0,400))
else
plot(forecast(fit2,
#Confidence Interval %
level = c(70,90)),
sub= "Confidence Interval 70% ~ 90% or Determined by user",
ylab= "Y Axis Variable",
main= "Forecast Linear Structural Model @ Level-Wise",
ylim = c(0,400))
})
}
基本上,您将根据input$select
在渲染绘图中绘图的内容。
一个稍微更优雅的版本。
server <- function(input, output) {
output$plot <- renderPlot({
if(input$select=="Trend")
method <- fit
else
method <- fit2
plot(forecast(method, #Confidence Interval %
level = c(input$range)),
sub= "Confidence Interval 70% ~ 90% or Determined by user",
ylab= "Y Axis Variable",
main= "Forecast Linear Structural Model @ Trend-Wise",
ylim = c(0,400))
})
}
但是,这使两个图更加“相似”。 如标题所示。 因此,您需要在if
定义其他var。 这归结为权衡
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.