Error in $: $ operator is invalid for atomic vectors & observeEvent not working


#1

So I have two problems here, first when selecting individual months the following create the same error message:
April, May, August, September, April & August, April & September, May & August, May & September.

Additionally, when you click on a row on the Renewal Selection Policies tab should take you to the corresponding data on the “Individual Policy” tab… it doesn’t. from what I can tell I have coded the second issue correctly. As for the first, all other combinations work, so I have no clue what to fix… please help thanks

I have sample data, but can’t seem to find where I upload it… And everything else is working the way I wanted it to. Thanks in advance

Main App

## This script will be ablle to query an agents number &/ name and pull up the statistics on how healthy and agents book is.
if(exists("Item") == FALSE) {
  source("global.R",local = FALSE)
} else if(exists("Item") == TRUE) {
}
library(DT)
library(shiny)
library(shinythemes)
library(shinydashboard)
#define UI dataset viewer application
shinyApp(
  ui<- dashboardPage(
    dashboardHeader(
      title = "Menu Options"
    ),
    dashboardSidebar(
      width = 330,
      fluidRow(  ## enter in a root number and bypass everything else
        uiOutput(outputId = "polNum"),
        #textInput(inputId = "polNum", label = NULL, placeholder = "Enter Root number here"),
        tags$em(div(style = "text-align:center","Note: the root number must include all 6 digits e.g. 00107")),
        tags$em(div(style = "text-align:left",".....Or select from options below:")) # the periods are in effect an indentation for the text so the text all appears on the sidebar
      ),
      fluidRow(  ## enter the region to look at and reduce the availabe root numbers
        checkboxGroupInput(inputId = "regionSelect","Region:",list("Eastern Idaho",
                                                                   "Magic Valley",
                                                                   "Northern Idaho",
                                                                   "Treasure Valley"), selected = NULL, inline = FALSE)
      ),
      fluidRow( ## enter a month to look at and reduce the available root number
        checkboxGroupInput( inputId = "monthSelect", label = "Renewal Month", choices = list("January",
                                                                                        "February",
                                                                                        "March",
                                                                                        "April",
                                                                                        "May",
                                                                                        "June",
                                                                                        "July",
                                                                                        "August",
                                                                                        "September",
                                                                                        "October",
                                                                                        "November",
                                                                                        "December"),inline = FALSE, selected = NULL
        )
      )
    ),
    dashboardBody(
      theme = shinytheme("readable"),
      tags$h4("The Idaho Farm Bureau ",
              tags$img(height = 40, width = 40, src = "shiny.png",align = "bottom"),
              " Underwriter Renwals Search App", align = "center"  
      ),
      tabsetPanel( id = "Underwriter RAM app",
        tabPanel( title = "Renewal Selection Policies",
                  mainPanel(
                    div(DT::dataTableOutput("polResults"),
                        style = "font-size:70%; width:155%")
                  )
        ),
        tabPanel( title = "Individual Policy",
          verticalLayout(
            splitLayout(cellWidths = c("50%","5%","45%"),
              div(DT::dataTableOutput(outputId = "PolSum"),style = "font-size:70%; width:50%"),
              tags$em(""),
              div(DT::dataTableOutput(outputId = "ModSum"),style = "font-size:70%; width:50%")
            ),
            mainPanel( title = "Individual Policy's Line Items",
                       div(DT::dataTableOutput("indPolResults"),
                           style = "font-size:70%; width:150%")
            )
          )        
        )
      )
    ),
    tags$h4("Brought to you by ",
            tags$img(height = 30, width = 80, src = "RStudio.png",align = "bottom"),
            " and the Farm Bureau RAM", align = "center"
    )
  ),
  server <- function(input,output, session) {
    # this creates a table of all the Policies
    PolSummary <- reactive({
      PolSummary1 <- matrix(nrow = 0, ncol = 66)
      if(!"Eastern Idaho" %in% input$regionSelect & !"Magic Valley" %in% input$regionSelect & !"Northern Idaho" %in% input$regionSelect &
         !"Treasure Valley" %in% input$regionSelect) {
        PolSummary1 <-Policy
      } else if("Eastern Idaho" %in% input$regionSelect | "Magic Valley" %in% input$regionSelect | "Northern Idaho" %in% input$regionSelect |
                "Treasure Valley" %in% input$regionSelect)  {
        if("Eastern Idaho" %in% input$regionSelect) {
          Eastern <- Policy[Policy$Region =="Eastern Idaho",]
          PolSummary1 <- rbind(PolSummary1, Eastern)
        }
        if("Magic Valley" %in% input$regionSelect) {
          Magic <- Policy[Policy$Region =="Magic Valley",]
          PolSummary1 <- rbind(PolSummary1, Magic)
        }
        if("Northern Idaho" %in% input$regionSelect) {
          Northern <- Policy[Policy$Region =="Northern Idaho",]
          PolSummary1 <- rbind(PolSummary1, Northern)
        }
        if("Treasure Valley" %in% input$regionSelect) {
          Treasure <- Policy[Policy$Region =="Treasure Valley",]
          PolSummary1 <- rbind(PolSummary1, Treasure)
        }
      }
      PolSummary2 <- matrix(nrow = 0, ncol = 66)
      if(!"January" %in% input$monthSelect & !"February" %in% input$monthSelect & !"March" %in% input$monthSelect & !"April" %in% input$monthSelect &
         !"May" %in% input$monthSelect & !"June" %in% input$monthSelect & !"July" %in% input$monthSelect & !"August" %in% input$monthSelect &
         !"September" %in% input$monthSelect & !"October" %in% input$monthSelect & !"November" %in% input$monthSelect & !"December" %in% input$monthSelect) {
        PolSummary2 <- PolSummary1
      } else if("January" %in% input$monthSelect | "February" %in% input$monthSelect | "March" %in% input$monthSelect | "April" %in% input$monthSelect &
                "May" %in% input$monthSelect | "June" %in% input$monthSelect | "July" %in% input$monthSelect | "August" %in% input$monthSelect &
                "September" %in% input$monthSelect | "October" %in% input$monthSelect | "November" %in% input$monthSelect | "December" %in% input$monthSelect) {
        if ("January" %in% input$monthSelect) {
          Month1 <- PolSummary1[PolSummary1$Month == "01",]
          PolSummary2 <- rbind(PolSummary2,Month1)
        }
        if ("February" %in% input$monthSelect) {
          Month2 <- PolSummary1[PolSummary1$Month == "02",]
          PolSummary2 <- rbind(PolSummary2,Month2)
        }
        if ("March" %in% input$monthSelect) {
          Month3 <- PolSummary1[PolSummary1$Month == "03",]
          PolSummary2 <- rbind(PolSummary2,Month3)
        }
        if ("April" %in% input$monthSelect) {
          Month4 <- PolSummary1[PolSummary1$Month == "04",]
          PolSummary2 <- rbind(PolSummary2,Month4)
        }
        if ("May" %in% input$monthSelect) {
          Month5 <- PolSummary1[PolSummary1$Month == "05",]
          PolSummary2 <- rbind(PolSummary2,Month5)
        }
        if ("June" %in% input$monthSelect) {
          Month6 <- PolSummary1[PolSummary1$Month == "06",]
          PolSummary2 <- rbind(PolSummary2,Month6)
        }
        if ("July" %in% input$monthSelect) {
          Month7 <- PolSummary1[PolSummary1$Month == "07",]
          PolSummary2 <- rbind(PolSummary2,Month7) 
        }
        if ("August" %in% input$monthSelect) {
          Month8 <- PolSummary1[PolSummary1$Month == "08",]
          PolSummary2 <- rbind(PolSummary2,Month8)
        }
        if ("September" %in% input$monthSelect) {
          Month9 <- PolSummary1[PolSummary1$Month == "09",]
          PolSummary2 <- rbind(PolSummary2,Month9)
        }
        if ("October" %in% input$monthSelect) {
          Month10 <- PolSummary1[PolSummary1$Month == "10",]
          PolSummary2 <- rbind(PolSummary2,Month10)
        }
        if ("November" %in% input$monthSelect) {
          Month11 <- PolSummary1[PolSummary1$Month == "11",]
          PolSummary2 <- rbind(PolSummary2,Month11)
        }
        if ("December" %in% input$monthSelect) {
          Month12 <- PolSummary1[PolSummary1$Month == "12",]
          PolSummary2 <- rbind(PolSummary2,Month12)
        }
      }
      PolSummary2 <- PolSummary2[order(PolSummary2$Root),]
      PolSummary2[,c(1,5,6,64,31,9,12:14,17,19,21:23,25,26,29,52,53,55,57:63)] 
    })  
    indPol <- reactive({
      Item[Item$Root == input$polNum,c(2:4,24,35,46,47,54,55,62:63,72,83,90,98:99,129,130,131,141,147:151)]## corrected
    })
    indPolSummary <- reactive({
      Policy[Policy$Root == input$polNum,c(1,5:7,9,13,24:25,28,52:53,31,58,61,59)]
    })
    ExpMod <- reactive({
      Modifier <- matrix(nrow=3,ncol=7)
      colnames(Modifier) <- c("-6 Years","-5 years","-4 Years","-3 Years","-2 Years","-1 Year","Totals")
      rownames(Modifier)<- c("Premium","Losses","Modifier")
      Modifier[1,1]<- Policy[Policy$Root == input$polNum,32]
      Modifier[1,2]<- Policy[Policy$Root == input$polNum,33]
      Modifier[1,3]<- Policy[Policy$Root == input$polNum,34]
      Modifier[1,4]<- Policy[Policy$Root == input$polNum,35]
      Modifier[1,5]<- Policy[Policy$Root == input$polNum,36]
      Modifier[1,6]<- Policy[Policy$Root == input$polNum,37]
      Modifier[1,7]<- Policy[Policy$Root == input$polNum,38]
      Modifier[2,1]<- Policy[Policy$Root == input$polNum,39]
      Modifier[2,2]<- Policy[Policy$Root == input$polNum,40]
      Modifier[2,3]<- Policy[Policy$Root == input$polNum,41]
      Modifier[2,4]<- Policy[Policy$Root == input$polNum,42]
      Modifier[2,5]<- Policy[Policy$Root == input$polNum,43]
      Modifier[2,6]<- Policy[Policy$Root == input$polNum,44]
      Modifier[2,7]<- Policy[Policy$Root == input$polNum,45]
      Modifier[3,1]<- Policy[Policy$Root == input$polNum,46]
      Modifier[3,2]<- Policy[Policy$Root == input$polNum,47]
      Modifier[3,3]<- Policy[Policy$Root == input$polNum,48]
      Modifier[3,4]<- Policy[Policy$Root == input$polNum,49]
      Modifier[3,5]<- Policy[Policy$Root == input$polNum,50]
      Modifier[3,6]<- Policy[Policy$Root == input$polNum,51]
      Modifier[3,7]<- Policy[Policy$Root == input$polNum,52]
      Modifier <- as.data.frame(Modifier)
    })
    output$polResults <- DT::renderDataTable(
      DT::datatable(PolSummary(), rownames = FALSE, extensions = c('Buttons','FixedHeader'), selection = "single", 
                    options = list(lengthMenu = c(15,20,50,100,150,200), pageLength = 15, class = 'cell-border', fixedHeader = FALSE, scrollX=TRUE),
                    colnames = c("Root","Renew Date","Insured","Region","Program","Policy Discount","Insurance Score Factor","Loyalty","Loyalty Factor",
                                 "Payment Type Factor","Defensible Space Factor","Item Age Factor","Territory","Territory Factor","IRPM","IRPM Factor","Insured Age Factor",
                                 "Experience Modification Factor","Loss Ration Factor","Protection Class Factor","Policy Size Factor","Actual Premium","Indicated Premium",
                                 "Total Correction Factor","Correction Dollar Difference","Underwriting Gain/Loss Percentage","Underwriting Gain/Loss Dollar"))
      %>%formatStyle(c('ISCorr','LoyCorr','PmtCorr','DSCorr','ItmAgeCorr','TerrCorr','IRPMCorr','InsAgeCorr','ExpMod',
                       'LRCorr','PCCorr','PolSizeCorr','CorrFac'), 
                     color = styleInterval(c(.75,1.5),c('white','black','white')),
                     backgroundColor = styleInterval(c(.75,.87,.925,1.08,1.15,1.5),
                                                     c('darkgreen','green','lightgreen','white','pink','red','darkred')),fontWeight = 'bold')
    )
    
    Policy.dft <- reactive ({
      PolSummary()[input$polResults_rows_selected, "Root"]  
    })
    output$polNum <- renderUI({
      if(length(input$polResults_rows_selected)>0) {
        textInput(inputId = "polNum", label = NULL, value = Policy.dft())
      } else{
        textInput(inputId = "polNum", label = NULL, placeholder = "Enter Root number here")
      }
    })
    observeEvent(input$polResults_rows_selected, {
      updateTabsetPanel(session, "Underwriter RAM app", selected = "Individual Policy")
    })
    observeEvent(input$polNum_updated, {
      updateTabsetPanel(session, "Underwriter RAM app", selected = "Individual Policy")
    })
    output$PolSum <- DT::renderDataTable(
      DT::datatable(indPolSummary(), rownames = FALSE, options = list(dom = 't',  selection = 'single',class = 'cell-border', fixedHeader = FALSE), 
                    colnames = c("Root","Renewal Date","Insured Name","Agent","Policy Discount","Loyalty","Sibling Policy","IRPM","Insured Age",
                                 "Experience Modifier","Loss Ratio Factor","Program","Actual Premium","Indicated Premium","Total Correction Factor")
      )
    )
    output$ModSum <- DT::renderDataTable(
      DT::datatable(ExpMod(), rownames = TRUE, options = list(dom = 't',  selection = 'single',class = 'cell-border', fixedHeader = FALSE) 
      )
    )
    output$indPolResults <- DT::renderDataTable(
      DT::datatable(indPol(),rownames = FALSE, extensions = c('Buttons','FixedHeader'), selection = "single", 
                    options = list(lengthMenu = c(15,20,50), pageLength = 15, class = 'cell-border', fixedHeader = TRUE, scrollX=TRUE),
                    colnames = c("Section","Item No.","Item Type", "Isurance Score","Loyalty Factor","Payment Factor","Defensible Space",
                                 "Defensible Space Factor","Item Age","Item Age Factor","Territory","Territory Factor","IRPM Factor",
                                 "Item Type Factor","Insured Age Factor","Loss Ratio","Experience Modifier","Loss Ratio Factor","Protection Class",
                                 "Protection Class Factor","Policy Size Factor","Actual Premium","Total Correction Factor","Correction Dollar Difference",
                                 "Indicated Premium")
      )%>%formatStyle(c('ISCorr','LoyCorr','PmtCorr','DSCorr','ItmAgeCorr','TerrCorr','IRPMCorr','InsAgeCorr','LRCorr','PCCorr','PolSizeCorr','CorrFac'),
                      color = styleInterval(c(.75,1.5),c('white','black','white')), 
                      backgroundColor = styleInterval(c(.75,.87,.925,1.08,1.15,1.5),c('darkgreen','green','lightgreen','white','pink','red','darkred')),fontWeight = 'bold') 
    )
  }
)
shinyApp(ui=ui,server=server)

global

## this is to load the fourtran programs for the RAM and create a data lookup to see everything that the RAM creates.
## set the working directory to the directory of this app.
  
## read in the Fortran Data
AgentRegion <- read.delim("Agent by Region List.txt", header = TRUE, sep="\t", quote = "")
Item <- read.delim("by_itm10.txt",header = TRUE, sep="|", quote = "", dec = ".", colClasses = c(Root  ="character", DS="character"))
Policy <-read.delim("by_pol10.txt",header = TRUE,sep="|",quote="",dec = ".", colClasses = c(Root  ="character", DS="character"))
## make the two dataframes match in column order and headers
NewCol1 <- c("Sec", "ItmNo","ItmType")
Policy[NewCol1] <- "All"
Policy <- subset(Policy, select = c(1,61:63,2:55,57:58,56,59:60))
colnames(Policy)[colnames(Policy)=="IndPrem"] <- "CorrPrem"
colnames(Policy)[colnames(Policy)=="Terr"] <- "Territory"
colnames(Item)[colnames(Item)=="CombISCorr"] <- "ISCorr"
colnames(Item)[colnames(Item)=="Prem"] <- "ActPrem"
colnames(Item)[colnames(Item)=="Corr"] <- "CorrFac"
NewCol1 <- c("Region","Year","Month")
Policy[NewCol1] <- ""
Policy$Region <- AgentRegion$Region[match(Policy$AgtNo, AgentRegion$NO)]
Policy$RenewDate <- as.Date(Policy$RenewDate, "%m/%d/%Y")
Policy$Year <- substring(Policy[,5],1,4)
Policy$Month<- substring(Policy[,5],6,7)