pApaAPPApapapa
pApaAPPApapapa

Reputation: 385

Creating seperat calculating and plots based on radio buttons in ShinyApp

I have a time series which I am analyzing. I want to create a dashboard using ShinyApp, where the user can choose to carry out a forecast using either ARIMA og Holt Winter's.

I am not sure how I take the input of the radio buttons and carry them over to the different steps.

I have tried the following, but get the following error:

Error in source("~/ShinyApp/TEST.R") : ~/ShinyApp/TEST.R:31:18: unexpected '=' 30: 31: if (input$n3 =

library(shiny)
require(forecast)

name = c (2,4,3,8,6,12,4,16)
name <- ts(name, frequency=12, start=c(2007,1))

ui <- fluidPage(

  numericInput(inputId = "n2", "Size of training set", value = 12),

  radioButtons(inputId = "n3", "Forecast type",  c("Holt Winter's" = "HW", "ARIMA" = "arima")),

  plotOutput(outputId = "graph"), 

  plotOutput(outputId = "graph2")

)




server <- function(input, output) { 

  output$graph <- renderPlot({

        plot(name, type = "l")  } ) }


  output$graph2 <- renderPlot( {

    if (input$n3 = "HW") { 

      fit <- ets (name, model = "ZAZ", damped=TRUE)

     plot(forecast(fit,level=c(80,80,99), h = input$n2), main = "Forecast, HW")
    }

    if (input$n3 = "arima") { 


      fit <- auto.arima (name)

      plot(forecast(fit, 12), main = "Forecast, ARIMA")

    } ) } 



shinyApp(ui = ui, server = server)

Upvotes: 0

Views: 644

Answers (1)

GyD
GyD

Reputation: 4072

You used the = operator instead of the == operator inside if statements (= is for assigning, == is for checking equality)

Also the readability is pretty bad, you have parentheses all around the place. After refactoring it works:

library(shiny)
require(forecast)

name = c(2,4,3,8,6,12,4,16)
name <- ts(name, frequency=12, start=c(2007,1))

ui <- fluidPage(
  numericInput(inputId = "n2", "Size of training set", value = 12),
  radioButtons(inputId = "n3", "Forecast type",  c("Holt Winter's" = "HW", "ARIMA" = "arima")),
  plotOutput(outputId = "graph"), 
  plotOutput(outputId = "graph2")
)

server <- function(input, output) { 

  output$graph <- renderPlot({
    plot(name, type = "l")
  })

  output$graph2 <- renderPlot({
    if (input$n3 == "HW") { 
      fit <- ets (name, model = "ZAZ", damped=TRUE)
      plot(forecast(fit,level=c(80,80,99), h = input$n2), main = "Forecast, HW")
    }

    if (input$n3 == "arima") { 
      fit <- auto.arima (name)
      plot(forecast(fit, 12), main = "Forecast, ARIMA")
    }
  }) 
}

shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions