2

I am trying to make an app with R shiny and I have a little problem.

What I want is to have 2 inputs. First input contain many variables from data base and the second inputs contain the values of a specific variable.

Ex:

ui = tagList(
    shinythemes::themeSelector(),
    navbarPage(
      theme = "united",  # <--- To use a theme, uncomment this
      "shinythemes",
      tabPanel("Casco 3003",
               sidebarPanel(
                 tags$h5("Deafult actionButton:"),
                 actionButton("action", "Search"),
                 radioButtons('format', 'Document format', c('PDF', 'HTML', 'Word'),
                              inline = TRUE),
                 downloadButton('downloadReport'),

                 selectInput("select", h3("Categorii"), 
                             choices = list("By Client Category" = "CLIENT_TYPE", "By Client Type" = "Leasing",
                                            "By Agent Type" = "by_agent_type", "By Vehicle Category" = "Vehicle_Category",
                                            "By Brands Top 10 Cars"="top_10_cars","By Age Of Vehicle Car"="Car_age"),
                             selected = "CLIENT_TYPE"),
                 selectizeInput("select2",label = "Vehicle Category",
                             choices = Casco_l$Vehicle_Category),
                 selectInput("select21",label = "Client Type",
                             choices = list("All"= "All","Client Category" = "CLIENT_CATEGORY"),
                             selected = "All"),
                 tags$h5("actionButton with CSS class:"),
                 actionButton("action2", "Action button", class = "btn-primary")
               ),
               mainPanel(
                 tabsetPanel(
                   tabPanel("INSIS 3003",
                            h4("Table"),
                            tableOutput("table1")
                   ),
                   tabPanel("Graphics",
                            plotOutput("plot1"),
                            plotOutput("plot3"))
                 )
               )
      )))
server = function(input, output) {

    aggregated <- reactive({
    Casco_l = subset(Data,INSR_TYPE==3003)
    Casco_l %>%
      group_by_(input$select21,input$select) %>%
      filter_(input$select2) %>%
      summarise("Exposure" = format(round(sum(Expos)), digits = 0, big.mark=","),
                "Earned Premium" = format(round(sum(Earned_Premium)), digits = 0, big.mark=","),
                "GWP" = format(round(sum(GWP_RON)), digits = 0, big.mark=","),
                "Incurred" = format(round(sum(inc)), digits = 0, big.mark=","),
                "NO of events" = format(as.numeric(sum(No_ev)), digits = 0, big.mark=","),
                "Frequency %" = format((sum(No_ev)/sum(Expos)*100), digits = 2, nsmall =2, big.mark=","),
                "Loss Ratio %" = format(as.numeric((sum(inc)/sum(Earned_Premium))*100), digits = 2, nsmall=2, big.mark=","),
                "ULR %"= format(as.numeric((sum(inc)/sum(Earned_Premium))*ulr*100),digits = 2, nsmall=2, big.mark=","),
                "Avr premium" = format(round(ifelse((sum(Expos)==0),0,(sum(Earnend_Premium)/sum(Expos)))), digits = 0, big.mark=","),
                "Avr claim" = format(round(ifelse((sum(No_ev)==0),0,(sum(inc)/sum(No_ev)))), digits = 0, big.mark=",") 
      )


  })

I don't know how to use input$select2 to make the group. Because the app said that "Cars" doesn't exist ("Cars is a value from Vehicle Category variable).

What I am trying to say is that I want a group by a variable (input$select) and by a values (input select$2).

Thank you.

I added a image to show what I am trying to say. "By Agent Type" is a variable (a column) from dataset exactly like "Client Category". For the first input ("By Agent Type") I want to display all the values, for the second input ("Client Category") I want to could choose between "PF" and "PJ" and display only one (or "PF" or "PJ") but not in the same time.

EDIT:

table(AGENT_TYPE): Broker,Agents, Own Network
table(CLIENT_CATEGORY): PF, PJ

I want to display all values for AGENT_TYPE and only one value for CLIENT_CATEGORY and in group_by I do the sum on column.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mary Nastase
  • 111
  • 11
  • Maybe: `group_by_(unique(c(input$select21,input$select)))` – zx8754 Feb 26 '19 at 09:27
  • As the `group_by_` is deprecated, maybe use: `group_by_at(vars(one_of(unique(c(input$select21,input$select)))))` – zx8754 Feb 26 '19 at 09:32
  • Related post: https://stackoverflow.com/questions/21208801/group-by-multiple-columns-in-dplyr-using-string-vector-input – zx8754 Feb 26 '19 at 09:33
  • No, it doesn't work. It's say that "Unknown columns: `Others`", where "Others" is a value on the column "Vehicle Cathegory" (input$select2) – Mary Nastase Feb 27 '19 at 11:22
  • Not sure if this is helpful, but if you are asking how to use group_by with a known variable and also with a user-selected variable, then you could use `group_by_at(.vars = c(known_variable, input$user_selected_variable))`. See here: https://community.rstudio.com/t/issue-making-dplyr-reactive-variables-work-in-shiny-module/3694 – DaveM Jun 17 '20 at 01:05

0 Answers0