2015-10-14 48 views
0

我有一個閃亮的應用程序,必須讀取由客戶端提供的大表並執行一些計算。 爲此,我用的fread下面的函數內fread性能和shinnyapp

read_shiny_files=function(path){ 
    file = data.table::fread(path,stringsAsFactors = F,header=TRUE,data.table=FALSE,skip=0) 
    rownames(file)=make.names(samples(file[,1]),unique=TRUE) 
    file=file[,-1] 
    return(file) 
} 

然後我允許基於用戶的一些數據預處理參數

read_files=function(features_list,rm,rmv,im,imv,nr){ 

    features_list=lapply(features_list,read_shiny_files) 
    #remove rows and columns with more than rmv missing values 
    if(rm==TRUE) features_list=lapply(features_list,remove_missing,rmv) 
    #impute based on imv neighbours. Method based on Troyanskaya et al 
    if(im==TRUE) features_list=lapply(features_list,function(x,imv) t(impute.knn(t(x),k=imv)$data),imv) 
    if(nr==TRUE) features_list=lapply(features_list,standardNormalization) 

return(features_list)         
} 

運行在RStudio此功能20M的表花費不超過2秒 但隨着閃亮絕對需要更多的時間(幾乎一分鐘)。無論是使用RunApp在本地運行它,還是在shinyapp.io網絡應用程序上使用它,都會發生這種情況 您能幫我提高速度嗎?

這裏是我已經寫了什麼

> fl=reactive({ 
>  validate(
>  need(input$files$datapath != "", "Please select a data set") 
> ) read_files(features_list=input$files$datapath,input$rm,input$rmv,input$im,input$imv,input$nr) 
> }) 

PS

我也改變server.R參數來改變表最大尺寸(5M的限制)

options(shiny.maxRequestSize=100*1024^2) 

回答

0

我在我閃亮的應用程序中使用data.tables和fread,但我沒有看到您指示的性能損失。

由於您沒有提供可重複使用的示例,因此我只會對我的經驗做出評論。

  • FREAD是data.table庫的一部分,它的輸出是一個data.table(你可以在函數調用相應的選項設置,如果你想有一個data.frame輸出 等)。
  • 用一張大桌子努力減少R的機率 您的桌子的副本:我會避免使用功能,除非絕對必要,但我會在那個時候使用 的閃光功能進行任何操作。
  • 以同樣的方式我會只有刪除與:=運營商的列。 不幸的是,如果您需要刪除行,:=運營商尚不可用,並且您確實需要複製,但[運營商應該比lapply更快。

總之,我會做所有操作的data.table動態建立data.table表達式(偉大的技術由馬特多伊爾建議)。

這是從我的代碼示例:

str <- paste0("cal_[dtf_,allow.cartesian=TRUE][, ftehours := sum(fteSum),by=c('", appTime_, 
      "', 'subfunctionname')]") 
dtf_ <- eval(parse(text= str))