R Shiny服务器功能内的聚合功能

时间:2018-08-17 20:15:15

标签: r shiny

我正在尝试将aggregate函数中的server函数应用为闪亮:

library(data.table)

datT2<-fread(paste0('C:/data/','30092017ARB.csv'),header=TRUE, sep=";",stringsAsFactors = FALSE)

#datT2[is.na(datT2)]<- 0

function(input, output) {

  #read the data 
  dataset <- reactive({
    infile <- input$date
    if (is.null(infile)){
      return(NULL)
    }
     data<-read.table(paste0('C:/data/',infile),header=TRUE, sep=";")
     data[is.na(data)]<- 0
#

     if (input$konz != "All") {
       data <- data[data$Konzernbezeichnung == input$konz,]
     }
        # Aggregate
    data<- aggregate(x=data$'EL absolut',by=list(lgd=data$`LGD-class`),FUN=sum)
            })



  # 
  # Filter data based on selections
  output$plot <- renderPlot({

    y <- dataset()$x #Marktwert #EL.absolut
    barplot(y,names.arg = dataset()$lgd)

  })

 }

当我在shell中运行相同的aggregate函数时,我得到

  lgd        x
1   1   385668
2   2   249534
3   3 35477549
4   4  2248865
5   5  1425647

我什至尝试aggregate.data.frame,在两种情况下都出现以下错误:

Warning: Error in aggregate.data.frame: no rows to aggregate

Warning: Error in aggregate: no rows to aggregate

一个示例数据集是:

Konzernbe  Best  LGD-class  Marktwert   EL absolut 
6010    3   3   1142345261  1428757
6010    3   3   849738658   1028973
6010    1   3   1680222820  220554
6010    1   3   896459567   116673
6010    0   3   1126673222  72077
6010    1   3   704226037   93310
--      1   4   336164879   49299
6010    0   3   948607746   60443
6070    1   3   265014117   34170
6020    3   3   47661945    58551
6050    2   3   307011781   115959
6020    0   1   1064022992  20320
6010    0   3   831782040   52950
6080    3   3   19367641    20286
--      2   4   197857365   87608
6010    1   3   679828856   90884
6050    3   3   317092037   372362
6080    3   3   20223616    21929
6010    1   3   693736624   96899
6050    3   3   308447822   372915

我该如何解决问题? 是不是必须使用另一种方法在闪亮服务器中进行聚合?

1 个答案:

答案 0 :(得分:1)

尝试一下

function(input, output) {

  #read the data
  dataset <- reactive({
    infile <- input$date
    if (is.null(infile)){
      return(NULL)
    }

    data<-read.table(paste0('C:/data/',infile),header=TRUE, sep=";")
    data[is.na(data)]<- 0

    if (input$konz != "All") {
    data <- data[data$Konzernbezeichnung == input$konz,]
    }

    # Aggregate
    data<- data[,list(ELAbsolut=sum(`EL absolut`)),by=`LGD-class`]

  })

    # Filter data based on selections
    output$plot <- renderPlot({

     y <- dataset()$ELAbsolut
     barplot(y,names.arg = dataset()$`LGD-class`)

    })

  }