我有以下代码。有没有办法在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_
})
答案 0 :(得分:9)
observeEvent
在lapply
中效果很好:
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)
编辑:对于以前版本的闪亮,请在服务器中使用此功能(添加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)]]
})
)
}
)