title: Shiny implementation
body: <p>Hello my question is about user interactivity with my code, I developed this simple loop for time series using the great forecast Hybrid package, here is the rmd for it</p>
  
  <p>The strange language is Portuguese sorry I was too lazy to translate the whole thing it shouldn't matter anyway.</p>
  
  <pre><code>```{r Primeira Vez, clique no play, include=FALSE, include=FALSE}
  setwd("~/R")
  install.packages("forecastHybrid")
  setwd("~/R")
  library(forecastHybrid)
  ```
  
  
  
  
   ```{r Inputs}
  # A dataset variable in the global enviroment
  Data&lt;- SemZero 
  
  #How to save a witout dummies regression
  ComoSalvar&lt;- "Exemplo2.csv"
  
  #How to save a with dummies regression
  ComoSalvarReg&lt;- "ExemploparaDummies.csv"
  
  #Where to save them.
  OndeSalvar &lt;- "~/R"
  
  #Month,Year, and Day variable for the ts
  Mes&lt;- 9
  Ano&lt;- 2012
  Dia&lt;- 1
  
  #Frequency
  Freq&lt;- 12
  
  #Forecast Period.
  Forecast = 12
  
  #Confidance intervals
  IC&lt;- c(0)
  
  
  #Variables in the Data dataset that will be used for the lapply regression, usually I would read an excel file with headers.
  VStart = 1
  VFinish = 2
  
  #Simple regressor dataset, they can be matrix as well as line since de data.fram combines everyone example data.frame(OddMonths,Christmas,WasTrumpPresident?,RainInThatSeason)
  Regressores&lt;- data.frame(0)
  ```
  
  
      ```{r Logic for rolling test and data simplification}
  setwd(OndeSalvar)
  
  if(IC[1] &gt; 0) pi&lt;-TRUE else
    pi&lt;- FALSE
  
  
  if (nrow(Data) &gt;= Freq*3 + Forecast*2) {
    Weights &lt;- "cv.error"   
    Multiplicador&lt;-floor((nrow(Data)-Forecast*2)/Freq) } else 
    if (nrow(Data) &lt;= Freq*3 + Forecast*2) {
      Weights &lt;- "cv.error"   
      Multiplicador&lt;-3 } else 
      if (nrow(Data) &gt;= Freq*2.5 + Forecast*2){
        Weights &lt;-  "cv.error"
        Multiplicador = 2.5} else
          Weights &lt;- "equal"
  
  
  
  ```
  
  
  ```{r The bunk of the regression process}
    if (nrow(Regressores) &lt; nrow(Data)) {
    my_forecast1 &lt;- try({function(x){
      print(x)
      print(summary(x))
      names(x)
      x[is.na(x)] &lt;- 0; x
      if(sum(abs(x)) &lt; Freq){
        Model&lt;- "aenst"} else
          if(mean(x[1:Freq]) == 0)Model&lt;- "aenst" else Model&lt;- "aenstf"
    x&lt;- ts(x, start = c(Ano, Mes,Dia), frequency = Freq)
    hm&lt;-hybridModel(x, models = Model, lambda = NULL, 
    a.args = list(trace = FALSE,test = "kpss", ic ="aicc", max.P = 2, max.p = 9,max.q=9,max.Q = 2,max.d = 2,max.D = 2,start.p = 9,start.P = 2,start.Q = 2,start.q = 9,allowdrift = TRUE,allowmean = TRUE
  
    #Se tiver tempo apague o # abaixo para uma maior qualidade no modelo arima. 
                             #,stepwise = FALSE,parallel = TRUE,num.cores = NULL 
    ),
  
    e.args = list(ic = "aicc"),
  
    n.args = list(repeats = nrow(Data)),
  
    s.args = NULL,
  
    t.args = NULL,
  
    weights = Weights,
    errorMethod = "RMSE",cvHorizon = Forecast,windowSize = frequency(x)*Multiplicador, horizonAverage = FALSE,
    verbose = TRUE)
  
  lapply(seq_along(x), function(i) paste(names(x)[[i]], x[[i]]))
    fcast1&lt;- forecast(hm,h = Forecast,level = IC,PI = pi)
    return(fcast1)
  
    }})
  Listas&lt;- lapply(Data[,VStart:VFinish], try(my_forecast1))
  
  if (pi == FALSE) ListaResultado&lt;- as.data.frame(lapply(Listas, '[[', 'mean')) else
    ListaResultado &lt;- Listas
  
  write.csv(ListaResultado , file = ComoSalvar)
  
  } else {
    my_forecastreg &lt;- function(x){
      print(x)
      print(summary(x))
      names(x)
      x[is.na(x)] &lt;- 0; x
      x&lt;- ts(x, start = c(Ano, Mes,Dia), frequency = Freq)
      hmreg &lt;- hybridModel(x ,models = "ans",
                           a.args = list(xreg = Regressores[1:nrow(Data),],trace = TRUE,test = "kpss", ic ="aicc", max.P = 2, max.p = 9,max.q=9,max.Q = 2,max.d = 2,max.D = 2,start.p = 9,start.P = 2,start.Q = 2,start.q = 9,allowdrift = TRUE,allowmean = TRUE
                                         #Se tiver tempo apague o # abaixo para uma maior qualidade no modelo arima. 
                                         #,stepwise = FALSE,parallel = TRUE,num.cores = NULL            
  
                           ),
                           n.args = list(xreg = Regressores[1:nrow(Data),], repeats= nrow(Regressores)),
                           s.args = list(xreg = Regressores[1:nrow(Data),], method = "arima"))
  
  
      fcast2&lt;- forecast(hmreg,h = Forecast,level = IC,PI = pi, xreg = Regressores[nrow(Data):(nrow(Data)+Forecast-1),])
  
      return(fcast2)
  
    }
  
    Listas2&lt;- lapply(Data[,VStart:VFinish], try(my_forecastreg))
  
    if (pi == FALSE) ListaResultado2&lt;- as.data.frame(lapply(Listas2, '[[', 'mean')) else
      ListaResultado2 &lt;- Listas2
  
    write.csv(ListaResultado2 , file = ComoSalvarReg)
  }
  ```
  </code></pre>
  
  <p>I want to develop something to get the user input and run the regressions, my end user doesn't usually like how ugly a R Markdown file looks, I was looking into using shiny, but i dont know a few details</p>
  
  <p>Who runs this regressions if I upload the whole thing successfully to shiny? My computer,the server, the user, I have no idea?</p>
  
  <p>Can the user input go into the users own global environment so that the whole thing could be kept as a strictly offline process(using Shiny as a beautification app that substitutes this input chunk?)</p>
  
  <p>Can someone please give an example of an Shiny app that does something similar?</p>
  
  <p>Can the user read.xlsm into shiny server, or use his global environment to define a data for the shiny app to use as input?</p>
  
  <p>Also is the thief package possible to implement on this lapply function as a way to increase the forecast quality, I would of course Drop the stlm and theta option from the model as they behave rather poorly in a wide range of simulations that I performed with toy sets, the stlm crashes on cross validation with a few observations and the theta model just doesn't work.</p>
  
  <p>Can someone teach me how to on error inside the function ignore the variable and just keep applying the function to the next variable? or change the model to something less problematic my solution was to try to catch these cases where the model would crash and drop the theta model before it happens but it is just an ugly hack to the underlying problem.
  Also if you see something ugly in the code itself feel free to criticize.</p>