2016-05-13 12 views
1

私はプレートのランダム化のための簡単なアプリケーションを作成しようとしています。これは、(サブジェクト、訪問、反復の数のような)ユーザからの入力として、いくつかのパラメータを取ります。R Shiny:リアクティブ({})関数のリストを作成するループ

server.Rでは、ランダムなサブジェクトID番号のプレートを作成するループが必要です。その後、私は他の計算とプロットのためのリストの要素を使用する必要があります。私はこの機能をShinyで動作させることはできません。そうでなければうまくいく。コードはplates.w.subjects <で始まる動作しません - リスト()

はここserver.Rコードです:

n.subjects <- reactive({ as.numeric(input$n.subjects) }) 
n.visits  <- reactive({ as.numeric(input$n.visits) })  
n.replicates <- reactive({ as.numeric(input$n.replicates) }) 
n.buffers  <- reactive({ as.numeric(input$n.buffers) })  
n.ipc   <- reactive({ as.numeric(input$n.ipc) }) 
n.plates  <- reactive({ ceiling((n.subjects()*n.visits()*n.replicates())/(n.wells-sum(n.buffers(),n.ipc()))) }) 

subject.ids <- reactive({ seq(1, n.subjects()) }) 
## other calculations here 

plates.w.subjects <- list() 
plates.data <- reactive({ 

for(i in 1:n.plates()) { 
local({ 
    if (i == 1) { 
    used.samples <- NA 
    left.samples <- subject.ids() 
    } else { 
    used.samples <- melt(plates.w.subjects)[,1] 
    left.samples <- setdiff(subject.ids(), used.samples) 
    } 

    if(length(left.samples) > max.subjects.plate()) { 
    c <- sample(left.samples, max.subjects.plate(), replace=FALSE, prob=NULL) 
    } else { 
    c <- sample(left.samples, length(left.samples), replace=FALSE, prob=NULL) 
    } 

    name <- paste0('Plate.', i) 
    tmp <- list(subj.ids = c) 
    plates.w.subjects[[name]] <- tmp 

    #rm(tmp,name,c,used.samples,left.samples) 
}) 
} 
}) 

output$result <- renderPrint({ 
    plates.data()$plates.w.subjects[[1]] 
}) 

理想的には、結果は次のようになります。

$Plate.1 
$Plate.1$subj.ids 
[1] 23 16 13 20 24 10 19 25 3 21 4 12 9 
$Plate.2 
$Plate.2$subj.ids 
[1] 14 22 8 18 2 17 6 11 1 15 5 7 

私は は、任意の助けを本当に感謝だろう..「ローカル」または「lapply」を使用し、いくつかのソリューションを見たが、私はどちらかが仕事を得るカント! ありがとう! マリア

答えて

0

これがフルで問題を解決しますが、私は試してみて、あなたのバグを見つけるために、離れて、それを破った場合はわかりません。

plates.w.subjects <- list() 
name <- paste0('Plate.', 1) # i = 1 
tmp <- list(subj.ids = c(100, 200)) 
(plates.w.subjects[[name]] <- tmp) 


name <- paste0('Plate.', 2) # i = 2 
tmp <- list(subj.ids = c(2000, 3000)) 
(plates.w.subjects[[name]] <- tmp) 

代わりのplates.data()$plates.w.subjects[[1]]を使用して、あなたの代わりにplates.w.subjectsを試してみたいことがあります。

plates.w.subjects 
    # $Plate.1 
    # $Plate.1$subj.ids 
    # [1] 100 200 

    # $Plate.2 
    # $Plate.2$subj.ids 
    # [1] 2000 3000 

plates.w.subjects[[1]] 
    # $subj.ids 
    # [1] 100 200 

注:!それはc(100,200)コンバインと競合する可能性としても、最高のc = ...を避けるために。

+0

Thxをボブ。特に 'c'の提案。私は最終的にそれがジョー・チェンの助けを借りて動作するようになりました。下に解決策を掲載します。 –

0

私はそれは私が必要とする何を得るために多くの作品にコードを破る必要がありました。私はi == 1を別のチャンクに分けてしまった。ここで何が動作します。それは...しかし、プログラミングの用語であるどのように効率的にわからない

plates.w.subjects.1 <- reactive({ 
sample(subject.ids(), max.subjects.plate(), replace=FALSE, prob=NULL) 
}) 


plates.w.subjects <- reactive({ 

plates.data <- list() 
plates.data$Plate.1 <- list(subj.ids = plates.w.subjects.1()) 

    for(i in 2:n.plates()) { 

    used.samples <- melt(plates.data)[,1] 
    left.samples <- setdiff(subject.ids(), used.samples) 

    if(length(left.samples) > max.subjects.plate()) { 
     z <- sample(left.samples, max.subjects.plate(), replace=FALSE, prob=NULL) 
     } else { 
     z <- sample(left.samples, length(left.samples), replace=FALSE, prob=NULL) 
     } 

    tmp <- list(subj.ids = z) 
    plates.data[[paste0('Plate.', i)]] <- tmp 
    } 
    plates.data 
}) 


output$result <- renderPrint({ 
plates.w.subjects() 

})

関連する問題