作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下代码。有没有办法将它写在循环或向量化语句中,例如 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_
})
最佳答案
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)]]
})
)
}
)
关于R Shiny : How to write loop for observeEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40038749/
我是一名优秀的程序员,十分优秀!