2016-05-13 110 views
1

我正在尝试为板块随机化创建一个简单的应用程序。它需要一些参数作为用户的输入(如主题数量,访问次数,重复次数)。R Shiny:For循环创建具有反应性({})功能的列表

在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',但我不能让任何一个工作.. 真的很感谢任何帮助! 谢谢! Maria

回答

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 = ...,因为它可能与c(100,200)联合收割机冲突。

+0

THX鲍勃。特别是对'c'的建议。我终于在Joe Cheng的帮助下完成了工作。将在下面发布解决方案。 –

0

我不得不把代码分解成更多的部分来让它做我所需要的。我结束了分离,如果我== 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() 

})