issues with script

Hello I am having an issues with a script I am writing. I know there are multiple problems and I am trying to fix them as the show up. The latest issue, I am having issues with

library(shiny)
library(readxl)
library(plotly)
library(dplyr)
library(ggplot2)
library(forecast)

ndl_data<-read_excel("STE1 Account Work.xlsx")

# convert the Closed column to an actual date so that we can easily select all days in a given year.  
ndl_data$Closed <- as.Date(ndl_data$Closed, format = "%d-%m-%Y")

# select everything by year thate is a specific network

df2018 <- ndl_data[which(ndl_data$"Closed" >= "2018-01-01" & ndl_data$"Closed" <= "2018-12-31"),]
df2019 <- ndl_data[which(ndl_data$"Closed" >= "2019-01-01" & ndl_data$"Closed" <= "2019-12-31"),]
df2020 <- ndl_data[which(ndl_data$"Closed" >= "2020-01-01" & ndl_data$"Closed" <= "2020-12-31"),]

df2018c <- ndl_data[which(ndl_data$"Closed" >= "2018-01-01" & ndl_data$"Closed" <= "2018-12-31" & ndl_data$"Network" == "COE"),]
df2018c<-df2018c %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2018c<-subset(df2018c, select=c("Closed", "Number"))


dfc2019 <- ndl_data[which(ndl_data$"Closed" >= "2019-01-01" & ndl_data$"Closed" <= "2019-12-31" & ndl_data$"Network" == "COE"),]
df2019c <-dfc2019 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2019c<-subset(df2019c, select=c("Closed", "Number"))

dfc2020 <- ndl_data[which(ndl_data$"Closed" >= "2020-01-01" & ndl_data$"Closed" <= "2020-12-31" & ndl_data$"Network" == "COE"),]
df2020c <-dfc2020 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2020c<-subset(df2020c, select=c("Closed", "Number"))

#___________________________________________________________________________________________________________________________________________
dfs12018 <- ndl_data[which(ndl_data$"Closed" >= "2018-01-01" & ndl_data$"Closed" <= "2018-12-31" & ndl_data$"Network" == "SECNet"),]
df2018s1 <-dfs12018 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2018s1<-subset(df2018s1, select=c("Closed", "Number"))

dfs12019 <- ndl_data[which(ndl_data$"Closed" >= "2019-01-01" & ndl_data$"Closed" <= "2019-12-31" & ndl_data$"Network" == "SECNet"),]
df2019s1 <-dfs12019 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2019s1<-subset(df2019s1, select=c("Closed", "Number"))

dfs12020 <- ndl_data[which(ndl_data$"Closed" >= "2020-01-01" & ndl_data$"Closed" <= "2020-12-31" & ndl_data$"Network" == "SECNet"),]
df2020s1 <-dfs12020 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2020s1<-subset(df2020s1, select=c("Closed", "Number"))
#___________________________________________________________________________________________________________________________________________
dfs22018 <- ndl_data[which(ndl_data$"Closed" >= "2018-01-01" & ndl_data$"Closed" <= "2018-12-31" & ndl_data$"Network" == "SBUNet"),]
df2018s2 <-dfs22018 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2018s2<-subset(df2018s2, select=c("Closed", "Number"))

dfs22019 <- ndl_data[which(ndl_data$"Closed" >= "2019-01-01" & ndl_data$"Closed" <= "2019-12-31" & ndl_data$"Network" == "SBUNet"),]
df2019s2 <-dfs22019 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2019s2<-subset(df2019s2, select=c("Closed", "Number"))

dfs22020 <- ndl_data[which(ndl_data$"Closed" >= "2020-01-01" & ndl_data$"Closed" <= "2020-12-31" & ndl_data$"Network" == "SBUNet"),]
df2020s2 <-dfs22020 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2020s2<-subset(df2020s2, select=c("Closed", "Number"))
#___________________________________________________________________________________________________________________________________________

ui <- fluidPage(
  
  # Application title
  titlePanel("NCL User Account Forcasting"),
  
  # Sidebar 
  sidebarLayout(
    sidebarPanel(
      selectInput("year", "Year",
                         list("2020"= "2020","2019"="2019","2018"="2018","2017"="2017")),
      numericInput("ahead", "Months to Forecast Ahead:", 12),
      submitButton("Update View"),
      ),
      
      # Show a plot of the generated distribution
      mainPanel(
        h3(textOutput("caption")),
        tabsetPanel(
          tabPanel(
            splitLayout(cellWidths=c("50%","50%"), plotOutput("fitt"),plotOutput("etsForecasttPlot")),
            tabPanel(
                splitLayout(cellWidths=c("50%","50%"), plotOutput("fits"),plotOutput("etsForecasttPlot")),
              tabPanel(
                splitLayout(cellWidths=c("50%","50%"), plotOutput("fitu"),plotOutput("etsForecasttPlot"))
              )
            )
          )
        )
      )
  )
)

server <- function(input, output) {
  getDataset<-reactive({
    if(input$year=='2020')
    {
      return(df2020)
    }
    else if (input$year=='2019')
    {
      return(df2019)
    }
    else if (input$year=='2018')
    {
      return(df2018)
    }
  })
                     
  output$caption<-renderText({
    paste("Year: ", input$year)
  })
  output$etsForecasttPlot <- renderPlot({
    fitt<-ets(getDataset(),"Network" == "COE")
    plot(forecast(fitt,h=input$ahead))
  })
  output$etsForecastsPlot <- renderPlot({
    fits<-ets(getDataset(),"Network"== "SECNet")
    plot(forecast(fits, h=input$ahead))
  })
  output$etsForecastuPlot <- renderPlot({
    fitu<-ets(getDataset(),"Network"=="SBUNet")
    plot(forcast(fitu,h=input$ahead))
  })
  output&plot <- renderPlot({
    plot(fitt)
  })
  output&plot <- renderPlot({
    plot(fits)
  })
  output&plot <- renderPlot({
    plot(fitu)
  })
}

shinyApp(ui = ui, server = server)

I would appreciate any help.
I know I am having two issues wright now
Warning: Error in <-: could not find function "&<-"
47: server [C:\Users\yatestm\Documents\R\Capstone\Capstone\Trial 5/app.R#139]
Error in output & plot <- renderPlot({ : could not find function "&<-"
Warning: Error in ets: y should be a univariate time series
I also think I have an unending loop

ampersands like & should only be used in logical evaluations in your code, check your code for where they appear.

& are not used to access items from lists in R in general, and Shiny uses the output$ list, so this is not shiny syntax. rather output$plot

but you have other problems, the output$plotname should be both unique, and should also correspond with a named ui placeholder in the UI. your ui names fitt fits and fitu so you should have output$fitt etc presumably, rather than output$plot 3 times (wihch is definitely not unique)

Ok I have revised my code and now I can get the UI to pop up, but it will not so anything

library(shiny)
library(readxl)
library(plotly)
library(dplyr)
library(ggplot2)
library(forecast)

ndl_data<-read_excel("STE1 Account Work.xlsx")

# convert the Closed column to an actual date so that we can easily select all days in a given year.  
ndl_data$Closed <- as.Date(ndl_data$Closed, format = "%d-%m-%Y")

# select everything by year thate is a specific network

df2018 <- ndl_data[which(ndl_data$"Closed" >= "2018-01-01" & ndl_data$"Closed" <= "2018-12-31"),]
df2019 <- ndl_data[which(ndl_data$"Closed" >= "2019-01-01" & ndl_data$"Closed" <= "2019-12-31"),]
df2020 <- ndl_data[which(ndl_data$"Closed" >= "2020-01-01" & ndl_data$"Closed" <= "2020-12-31"),]

df2018c <- ndl_data[which(ndl_data$"Closed" >= "2018-01-01" & ndl_data$"Closed" <= "2018-12-31" & ndl_data$"Network" == "COE"),]
df2018c<-df2018c %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2018c<-subset(df2018c, select=c("Closed", "Number"))


dfc2019 <- ndl_data[which(ndl_data$"Closed" >= "2019-01-01" & ndl_data$"Closed" <= "2019-12-31" & ndl_data$"Network" == "COE"),]
df2019c <-dfc2019 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2019c<-subset(df2019c, select=c("Closed", "Number"))

dfc2020 <- ndl_data[which(ndl_data$"Closed" >= "2020-01-01" & ndl_data$"Closed" <= "2020-12-31" & ndl_data$"Network" == "COE"),]
df2020c <-dfc2020 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2020c<-subset(df2020c, select=c("Closed", "Number"))

#___________________________________________________________________________________________________________________________________________
dfs12018 <- ndl_data[which(ndl_data$"Closed" >= "2018-01-01" & ndl_data$"Closed" <= "2018-12-31" & ndl_data$"Network" == "SECNet"),]
df2018s1 <-dfs12018 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2018s1<-subset(df2018s1, select=c("Closed", "Number"))

dfs12019 <- ndl_data[which(ndl_data$"Closed" >= "2019-01-01" & ndl_data$"Closed" <= "2019-12-31" & ndl_data$"Network" == "SECNet"),]
df2019s1 <-dfs12019 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2019s1<-subset(df2019s1, select=c("Closed", "Number"))

dfs12020 <- ndl_data[which(ndl_data$"Closed" >= "2020-01-01" & ndl_data$"Closed" <= "2020-12-31" & ndl_data$"Network" == "SECNet"),]
df2020s1 <-dfs12020 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2020s1<-subset(df2020s1, select=c("Closed", "Number"))
#___________________________________________________________________________________________________________________________________________
dfs22018 <- ndl_data[which(ndl_data$"Closed" >= "2018-01-01" & ndl_data$"Closed" <= "2018-12-31" & ndl_data$"Network" == "SBUNet"),]
df2018s2 <-dfs22018 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2018s2<-subset(df2018s2, select=c("Closed", "Number"))

dfs22019 <- ndl_data[which(ndl_data$"Closed" >= "2019-01-01" & ndl_data$"Closed" <= "2019-12-31" & ndl_data$"Network" == "SBUNet"),]
df2019s2 <-dfs22019 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2019s2<-subset(df2019s2, select=c("Closed", "Number"))

dfs22020 <- ndl_data[which(ndl_data$"Closed" >= "2020-01-01" & ndl_data$"Closed" <= "2020-12-31" & ndl_data$"Network" == "SBUNet"),]
df2020s2 <-dfs22020 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2020s2<-subset(df2020s2, select=c("Closed", "Number"))
#___________________________________________________________________________________________________________________________________________

ui <- fluidPage(
  
  # Application title
  titlePanel("NCL User Account Forcasting"),
  
  # Sidebar 
  sidebarLayout(
    sidebarPanel(
      selectInput("year", "Year:",
                         list("2020"= "2020","2019"="2019","2018"="2018","2017"="2017")),
      numericInput("ahead", "Months to Forecast Ahead:", 12),
      submitButton("Update View"),
      ),
      
      # Show a plot of the generated distribution
      mainPanel(
        h3(textOutput("caption")),
        tabsetPanel(
          tabPanel("SCI",plotOutput("fitt")),
          tabPanel("SC"),plotOutput("fitc"),
          tabPanel("U"),plotOutput("fitu")
        )
      )
  )
)

server <- (function(input, output) {
  getDataset<-reactive({
    if(input$year=='2020')
    {
      return(df2020)
    }
    else if (input$year=='2019')
    {
      return(df2019)
    }
    else if (input$year=='2018')
    {
      return(df2018)
    }
  })
                     
  output$caption<-renderText({
    paste("Dataset: ", input$year)
  })
  output$dcompPlot<-renderPlot({
    ds_ts<-ts(getDataset(), frequency = 12)
    a=decompose(ds_ts)
    plot(a)
  })
  output$etsForecasttPlot <- renderPlot({
    fitt<-ets(getDataset())
    plot(forecast(fitt,h=input$ahead))
  })
  output$etsForecastsPlot <- renderPlot({
    fits<-ets(getDataset())
    plot(forecast(fits, h=input$ahead))
  })
  output$etsForecastuPlot <- renderPlot({
    fitu<-ets(getDataset())
    plot(forcast(fitu,h=input$ahead))
  })
})

shinyApp(ui = ui, server = server)
...

Sorry, I know I am having basic issues. I am not really good with programming.

ok now I am getting a new error. I am not sure if I fixed the previous issue or this new issue supersedes the previous issue.
Error in : n() should only be called in a data context.

The code I have now is


# convert the Closed column to an actual date so that we can easily select all days in a given year.  
ndl_data$Closed <- as.Date(ndl_data$Closed, format = "%d-%m-%Y")

# select everything by year
df2018 <- ndl_data[which(ndl_data$"Closed" >= "2018-01-01" & ndl_data$"Closed" <= "2018-12-31"),]
df2019 <- ndl_data[which(ndl_data$"Closed" >= "2019-01-01" & ndl_data$"Closed" <= "2019-12-31"),]
df2020 <- ndl_data[which(ndl_data$"Closed" >= "2020-01-01" & ndl_data$"Closed" <= "2020-12-31"),]

df2018c <- ndl_data[which(ndl_data$"Closed" >= "2018-01-01" & ndl_data$"Closed" <= "2018-12-31" & ndl_data$"Network" == "COE"),]
df2018c<-df2018c %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2018c<-subset(df2018c, select=c("Closed", "Number"))

dfs12018 <- ndl_data[which(ndl_data$"Closed" >= "2018-01-01" & ndl_data$"Closed" <= "2018-12-31" & ndl_data$"Network" == "SECNet"),]
df2018s1 <-dfs12018 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2018s1<-subset(df2018s1, select=c("Closed", "Number"))

dfs22018 <- ndl_data[which(ndl_data$"Closed" >= "2018-01-01" & ndl_data$"Closed" <= "2018-12-31" & ndl_data$"Network" == "SBUNet"),]
df2018s2 <-dfs22018 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2018s2<-subset(df2018s2, select=c("Closed", "Number"))
#_______________________________________________________________________________________________________________________________________
dfc2019 <- ndl_data[which(ndl_data$"Closed" >= "2019-01-01" & ndl_data$"Closed" <= "2019-12-31" & ndl_data$"Network" == "COE"),]
df2019c <-dfc2019 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2019c<-subset(df2019c, select=c("Closed", "Number"))

dfs12019 <- ndl_data[which(ndl_data$"Closed" >= "2019-01-01" & ndl_data$"Closed" <= "2019-12-31" & ndl_data$"Network" == "SECNet"),]
df2019s1 <-dfs12019 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2019s1<-subset(df2019s1, select=c("Closed", "Number"))

dfs22019 <- ndl_data[which(ndl_data$"Closed" >= "2019-01-01" & ndl_data$"Closed" <= "2019-12-31" & ndl_data$"Network" == "SBUNet"),]
df2019s2 <-dfs22019 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2019s2<-subset(df2019s2, select=c("Closed", "Number"))

#_______________________________________________________________________________________________________________________________________

dfc2020 <- ndl_data[which(ndl_data$"Closed" >= "2020-01-01" & ndl_data$"Closed" <= "2020-12-31" & ndl_data$"Network" == "COE"),]
df2020c <-dfc2020 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2020c<-subset(df2020c, select=c("Closed", "Number"))

dfs12020 <- ndl_data[which(ndl_data$"Closed" >= "2020-01-01" & ndl_data$"Closed" <= "2020-12-31" & ndl_data$"Network" == "SECNet"),]
df2020s1 <-dfs12020 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2020s1<-subset(df2020s1, select=c("Closed", "Number"))

dfs22020 <- ndl_data[which(ndl_data$"Closed" >= "2020-01-01" & ndl_data$"Closed" <= "2020-12-31" & ndl_data$"Network" == "SBUNet"),]
df2020s2 <-dfs22020 %>%
  group_by("Closed") %>%
  mutate("Number"=n())
df2020s2<-subset(df2020s2, select=c("Closed", "Number"))
#___________________________________________________________________________________________________________________________________________

ui <- fluidPage(
  
  # Application title
  titlePanel("NCL User Account Forcasting"),
  
  # Sidebar 
  sidebarLayout(
    sidebarPanel(
      selectInput("year", "Year:",
                         list("2020"= "2020","2019"="2019","2018"="2018","2017"="2017")),
      numericInput("ahead", "Months to Forecast Ahead:", 12),
      submitButton("Update View"),
      ),
      
      # Show a plot of the generated distribution
      mainPanel(
        h3(textOutput("caption")),
        tabsetPanel(
          tabPanel("General",plotOutput("ts_plot")),
          tabPanel("SCI",plotOutput("etsForecasttPlot")),
          tabPanel("SC"),plotOutput("etsForecastsPlot"),
          tabPanel("U"),plotOutput("etsForecastuPlot")
        )
      )
  )
)

server <- (function(input, output) {
  getDataset<-reactive({
    if(input$year=='2020')
    {
      return(df2020)
    }
    else if (input$year=='2019')
    {
      return(df2019)
    }
    else if (input$year=='2018')
    {
      return(df2018)
    }
  })
                     
  output$caption<-renderText({
    paste("Dataset: ", input$year)
  })
  ts_plotdataset<-renderPlot({
    ds_ts<-ts(getDataset(), frequency = 12)
    dataset<-getDataset()
    mutate("Number"=n())
    plot(ds_ts)
  })
  output$etsForecasttPlot <- renderPlot({
    fitt<-ets(ds_ts(), frequency = 12)
    mutate("Number"=t())
    plot(forecast(fitt,h=input$ahead))
  })
  output$etsForecastsPlot <- renderPlot({
    fits<-ets(ds_ts(), frequency = 12)
    mutate("Number"=s())
    plot(forecast(fits, h=input$ahead))
  })
  output$etsForecastuPlot <- renderPlot({
    fitu<-ets(ds_ts(), frequency = 12)
    mutate("number" =u())
    plot(forcast(fitu,h=input$ahead))
  })
})

shinyApp(ui = ui, server = server)

I know not that it is databaset related. I have two shiny scripts running. One has access to my database, the other has access to a database with only headers and no row data. I am getting the error one the script that has actual data.

You are using mutate without any dataframe.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Yeah thank you for that catch, my spelling is horrible.

ok that is fixed now I am getting an error: need finite 'ylim' values

again, you arent matching your output$plotnames to your plotOutput("plotnames")

if its fitt they should match as fitt, or if its not fitt but its etsForecasttPlot , they should both be etsForecasttPlot.
if there is only one mention of etsForecasttPlot in your shiny app, you cant expect it to do anytihng, it provides the render function for which ui element ? (it should be one with the same inputId)