gpt4 book ai didi

r - 通过函数调用 Shiny 的分组输入

转载 作者:行者123 更新时间:2023-12-02 03:22:02 25 4
gpt4 key购买 nike

我正在尝试制作一个 Shiny 的应用程序。原则应该很简单:为每个输入显示一个表格。这是我的代码。该应用程序显示整个数据库的总和,但我不想要这样。

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)

例如:

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

我想获得这样的东西:

CLIENT_TYPE Earnend Premium Exposure    GWP    Incurred  No of events
PF 80 10 4500 140 5
PJ 110 27 6500 70 5

但是,事实上该应用程序显示总计:

3003          Earnend Premium   Exposure    GWP     Incurred     No of events
CLIENT_TYPE 190 37 11000 210 10

我希望 smbd 明白我想说的。

谢谢。

最佳答案

当您使用input$select时,它是一个字符串(input$select = "CLIENT_TYPE")。

因此您需要使用函数group_by_()(或group_by_at())而不是函数group_by()。该函数将理解该字符串应被视为 data.frame 的变量。

更多信息请点击:dplyr: whats the difference between group_by and group_by_ functions?

aggregated <- reactive({
DATA %>%
group_by_(input$select) %>%
summarise( [...] )
})

aggregated <- reactive({
DATA %>%
group_by_at(input$select) %>%
summarise( [...] )
})

关于r - 通过函数调用 Shiny 的分组输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54482025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com