我正在尝试为板块随机化创建一个简单的应用程序。它需要一些参数作为用户的输入(如主题数量,访问次数,重复次数)。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
THX鲍勃。特别是对'c'的建议。我终于在Joe Cheng的帮助下完成了工作。将在下面发布解决方案。 –