gpt4 book ai didi

R Shiny : How to write loop for observeEvent

转载 作者:行者123 更新时间:2023-12-04 13:57:47 55 4
gpt4 key购买 nike

我有以下代码。有没有办法将它写在循环或向量化语句中,例如 lapply?在我的实际代码中,我有更多的画笔,所以这将非常有帮助。谢谢。

忽略这一行。只需要添加更多文本。

observeEvent(input$brush_1,{
Res=brushedPoints(D(),input$brush_1,allRows = TRUE)
vals$keeprows = Res$selected_
})

observeEvent(input$brush_2,{
Res=brushedPoints(D(),input$brush_2,allRows = TRUE)
vals$keeprows = Res$selected_

})

observeEvent(input$brush_3,{
Res=brushedPoints(D(),input$brush_3,allRows = TRUE)
vals$keeprows = Res$selected_

})

observeEvent(input$brush_4,{
Res=brushedPoints(D(),input$brush_4,allRows = TRUE)
vals$keeprows = Res$selected_

})

observeEvent(input$brush_5,{
Res=brushedPoints(D(),input$brush_5,allRows = TRUE)
vals$keeprows = Res$selected_

})

observeEvent(input$brush_6,{
Res=brushedPoints(D(),input$brush_6,allRows = TRUE)
vals$keeprows = Res$selected_

})

observeEvent(input$brush_7,{
Res=brushedPoints(D(),input$brush_7,allRows = TRUE)
vals$keeprows = Res$selected_

})

observeEvent(input$brush_8,{
Res=brushedPoints(D(),input$brush_8,allRows = TRUE)
vals$keeprows = Res$selected_

})

observeEvent(input$brush_9,{
Res=brushedPoints(D(),input$brush_9,allRows = TRUE)
vals$keeprows = Res$selected_

})

observeEvent(input$brush_10,{
Res=brushedPoints(D(),input$brush_10,allRows = TRUE)
vals$keeprows = Res$selected_

})

observeEvent(input$brush_11,{
Res=brushedPoints(D(),input$brush_11,allRows = TRUE)
vals$keeprows = Res$selected_

})

observeEvent(input$brush_12,{
Res=brushedPoints(D(),input$brush_12,allRows = TRUE)
vals$keeprows = Res$selected_

})

最佳答案

observeEventlapply 中效果很好:

library("shiny")
ui <- fluidPage(
fluidRow(
column(
width = 6,
lapply(
X = 1:6,
FUN = function(i) {
sliderInput(inputId = paste0("d", i), label = i, min = 0, max = 10, value = i)
}
)
),
column(
width = 6,
verbatimTextOutput(outputId = "test")
)
)
)
server <- function(input, output){

vals <- reactiveValues()

lapply(
X = 1:6,
FUN = function(i){
observeEvent(input[[paste0("d", i)]], {
vals[[paste0("slider", i)]] <- input[[paste0("d", i)]]
})
}
)

output$test <- renderPrint({
reactiveValuesToList(vals)
})
}
shinyApp(ui = ui, server = server)

编辑 :对于以前版本的 Shiny ,在服务器中使用它(添加 assign):
lapply(
X = 1:6,
FUN = function(i){
assign(
paste0("obs", i),
observeEvent(input[[paste0("d", i)]], {
vals[[paste0("slider", i)]] <- input[[paste0("d", i)]]
})
)
}
)

关于R Shiny : How to write loop for observeEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40038749/

55 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com