闪亮的输入图选择
Shiny input plot selection
我有这段代码,我想通过两种不同的绘图方式为 select 提供一个 Select 输入选项。
我希望,如果您选择 "level" 选项,"fit 2" 会被绘制出来,而不仅仅是适合。
这是fit2.
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))
})
}
不过,这使得二人剧情"more similar"。正如您在标题中看到的那样。所以你需要在 if
中定义额外的变量。这归结为权衡
我有这段代码,我想通过两种不同的绘图方式为 select 提供一个 Select 输入选项。
我希望,如果您选择 "level" 选项,"fit 2" 会被绘制出来,而不仅仅是适合。
这是fit2.
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))
})
}
不过,这使得二人剧情"more similar"。正如您在标题中看到的那样。所以你需要在 if
中定义额外的变量。这归结为权衡