Reputation: 121
I am trying to make an app with shiny. The principle should to be simple: Show one table for every input. Here is my code. The app show the sum for enitre database, but I don't want that.
ui <- fluidPage(
titlePanel("Casco Monthly"),
sidebarLayout(
sidebarPanel(
h2(strong("Shiny App"),style = "color:blue"),
img(src = "groupama.jpg", height = 100, width = 200),
selectInput("select", h3("Categorii"),
choices = list("CLIENT_TYPE" = "CLIENT_TYPE", "Grupare.Leasing" = "Grupare.Leasing",
"AGENT_TYPE" = "AGENT_TYPE", "Grupare.vehicul" = "Grupare.vehicul",
"Grupare.vechime"="Grupare.vechime"),
selected = "CLIENT_TYPE"),
selectInput("select2",label = "Client Type",
choices = c ("NONE","Categorie_client"),
selected = "NONE")),
mainPanel(
h1(strong("CASCO 3003")),
mainPanel(tableOutput("table1")))
)
)
server <- function(input, output) {
aggregated <- reactive({
DATA %>%
group_by("3003"=input$select) %>%
summarise("Earned Premium" = sum(EP), "Exposure" = sum(EXP),"GWP" = sum(GWP_RON), "Incurred" = sum(INC),"NO of events" = sum(Nr.evenim))
})
output$table1 <- renderTable({
aggregated()
})
}
shinyApp(ui = ui, server = server)
For example:
CLIENT_TYPE EP EXP GWP INC NR.EVENIM
PF 10 1 1000 30 2
PF 20 4 2000 50 1
PF 50 5 1500 60 2
PJ 20 3 2500 20 1
PJ 40 7 2000 10 1
PJ 30 8 500 20 2
PJ 20 9 1500 20 1
I wanted to obtain smth like that:
CLIENT_TYPE Earnend Premium Exposure GWP Incurred No of events
PF 80 10 4500 140 5
PJ 110 27 6500 70 5
But, in fact the app shows grand total:
3003 Earnend Premium Exposure GWP Incurred No of events
CLIENT_TYPE 190 37 11000 210 10
I hope smbd understand what I am trying to say.
Thank you.
Upvotes: 2
Views: 2872
Reputation: 2185
When you used input$select
, it is a string (input$select = "CLIENT_TYPE"
).
So you need to use the function group_by_()
(or group_by_at()
) instead of the function group_by()
. This function will understand that the string should be consider as a variable of the data.frame.
More information here: dplyr: whats the difference between group_by and group_by_ functions?
aggregated <- reactive({
DATA %>%
group_by_(input$select) %>%
summarise( [...] )
})
or
aggregated <- reactive({
DATA %>%
group_by_at(input$select) %>%
summarise( [...] )
})
Upvotes: 5