Error: unused argument (.) when trying to do forecast based on moving average in R Shiny

Viewed 21

I am new to R and RShiny and I feel a little lost right now. I am trying to do a simple financial forecast based on the small moving medium average in R Shiny. I tried it in R Studio and it worked, but I can not get the code to work in my app. I receive the error message "unused argument (.)"

  1. This is how I get the data into R Shiny
  # acquiring data
  dataInput <- reactive({ ##reactive expressions make code faster, they can be called anywhere in the app
    req(input$symb)
    req(input$dates)
   
    
    return(isolate({  ## with isolate reactive expression can be read, but cannot cause the reactive scope of the caller to be re-evaluated when they change
      getSymbols(input$symb, src="yahoo",  ##here we get the input data from yahoo 
                 from = input$dates[1],
                 to = input$dates[2],
                 warnings = FALSE,
                 auto.assign = FALSE) 
      
    }))
  })
  1. This is how I try to plot the data in the UI
  mainPanel(tabsetPanel(id = "tab", ##creates different tabs, id can be referenced in Server logic
tabPanel("Forecast", icon = icon("rectangle-list"), plotOutput(outputId = "forecast", height = "400px"))
                                
  1. This is what in do in the server function
  output$forecast<- renderPlot({ 
    req(input$dates)
    df <- data.frame(dataInput()) #convert into data frame
    names(df) <-c("Open", "High", "Low", "Close", "Volume", "Adjusted")
    df$Date <- as.Date(rownames(df))
    df$MA10 <- TTR::SMA (df$Close, n = 100)
    ts<- df$MA10%>%dataInput()
    fma <-forecast(ts, h=100)
    fma
  })## End renderPlot
  

2.This is what I did in R Studio and it worked

#required libraries
install.packages("install.load")
require ("install.load")
install_load(c("ggplot2", "TTR", 
               "quantmod", "forecast", "TSstudio", "pracma", "dplyr"))


#define variables so that you can change the ticker symbol

Share <- "FSLR"
StartDate <- "2000-01-01"
EndDate <- "2022-01-15"

#get data in df
#load stock prices into object called df, hence auto.assign = FALSE
# if auto.assign = TRUE the data gets loaded to an object which is the same as the name(symbol) of the stock

ts <- getSymbols(Share
                 , from = StartDate
                 , to = EndDate
                 , warnings = FALSE
                 , auto.assign = FALSE)


df <- data.frame(ts) #convert into data frame
names(df) <-c("Open", "High", "Low", "Close", "Volume", "Adjusted")
df$Date <- as.Date(rownames(df))
df$MA10 <- TTR::SMA (df$Close, n = 100)
ts <-  df$MA10%>%ts
fma <-forecast (ts, h=100)
fma
plot(fma, xlab ="Days", ylab = "Closing prices", main = "100 days forecast based on moving average")



  1. I think the problem lies here, but I am not sure on how to solve it
ts<- df$MA10%>%dataInput()
1 Answers

The dataInput() is an accessor only, meaning it retrieves data but is not otherwise a function you can use for anything else. That is, you cannot add a new column to it that way.

I suggest that you keep the SMA out of the plot reactive, placing it either in the original dataInput block or a new reactive block, and simplify your renderPlot block.

Perhaps this (untested):

  dataInput <- reactive({
    req(input$symb)
    req(input$dates)
    df <- getSymbols(input$symb, src="yahoo",
                 from = input$dates[1],
                 to = input$dates[2],
                 warnings = FALSE,
                 auto.assign = FALSE)
    df <- data.frame(df)
    names(df) <-c("Open", "High", "Low", "Close", "Volume", "Adjusted")
    df$Date <- as.Date(rownames(df))
    df$MA10 <- TTR::SMA (df$Close, n = 100)
    df
  })

  output$forecast <- renderPlot({
    req(dataInput())
    df <- dataInput()
    fma <- forecast(df$MA10, h=100)
    plot(fma, xlab ="Days", ylab = "Closing prices", main = "100 days forecast based on moving average")
  })
Related