想在 r shiny 中显示 for 循环图的所有变化(重叠)
Wanna show the all changes(overlap) of for loop plot in r shiny
是关于Rshiny的询问。
我创建了一个名为 foo 的函数,如下所示。
在函数中,for 循环中有 5 个图。当你在 Shiny 中画图时,只有最后一个图是可见的,其余的图是不可见的。你没看到正在创建(更新)五个地块吗?
foo <- function(iter = 5){
for(j in 1:iter){
plot(iris$Sepal.Length, iris$Sepal.Width, col = j)
Sys.sleep(0.5)
}
}
ui<-shinyUI(fluidPage(
sth
plotOutput('myplot')
))
server <- shinyServer(function(input, output, session){
sth ...
output$myplot <- renderPlot({
f <- foo(iter = 3)
})
})
})
您不能在这里使用循环,因为服务器在 UI 中呈现新输出之前执行所有代码,而 Sys.sleep()
只会导致整个 R 进程停止规定的时间。相反,您可以使用 invalidateLater()
使您的绘图函数以设定的时间间隔触发,同时仍允许程序的其余部分正常 运行。
library(shiny)
ui <- shinyUI(fluidPage(
sliderInput("iterations", "Iterations", 1, 10, 3),
sliderInput("interval", "Interval (ms)", 100, 1000, 500, step = 100),
actionButton("draw", "Draw"),
plotOutput('myplot')
))
server <- shinyServer(function(input, output, session) {
foo <- function(iterations = 5, interval = 500) {
i <- 0
output$myplot <- renderPlot({
i <<- i + 1
if (i < iterations)
invalidateLater(interval)
plot(iris$Sepal.Length, iris$Sepal.Width, col = i)
})
}
observeEvent(input$draw, foo(input$iterations, input$interval))
})
shiny::shinyApp(ui, server)
现在,您还可以将每个时间间隔都做某事的想法包装成一种延迟的 map
函数,看起来像这样:
map_later <- function(.x, .f, ..., .interval = 500) {
i <- 0
observe({
i <<- i + 1
if (i < length(.x))
invalidateLater(.interval)
.f(.x[i], ...)
})
}
这将产生一个更整洁、更易于管理的服务器:
ui <- shinyUI(fluidPage(
plotOutput('myplot')
))
server <- shinyServer(function(input, output, session) {
map_later(1:5, function(i) {
output$myplot <- renderPlot({
plot(iris$Sepal.Length, iris$Sepal.Width, col = i)
})
}, .interval = 500)
})
shiny::shinyApp(ui, server)
在这里命名可能不太好,但是,嘿,它做了它应该做的事情。
是关于Rshiny的询问。 我创建了一个名为 foo 的函数,如下所示。 在函数中,for 循环中有 5 个图。当你在 Shiny 中画图时,只有最后一个图是可见的,其余的图是不可见的。你没看到正在创建(更新)五个地块吗?
foo <- function(iter = 5){
for(j in 1:iter){
plot(iris$Sepal.Length, iris$Sepal.Width, col = j)
Sys.sleep(0.5)
}
}
ui<-shinyUI(fluidPage(
sth
plotOutput('myplot')
))
server <- shinyServer(function(input, output, session){
sth ...
output$myplot <- renderPlot({
f <- foo(iter = 3)
})
})
})
您不能在这里使用循环,因为服务器在 UI 中呈现新输出之前执行所有代码,而 Sys.sleep()
只会导致整个 R 进程停止规定的时间。相反,您可以使用 invalidateLater()
使您的绘图函数以设定的时间间隔触发,同时仍允许程序的其余部分正常 运行。
library(shiny)
ui <- shinyUI(fluidPage(
sliderInput("iterations", "Iterations", 1, 10, 3),
sliderInput("interval", "Interval (ms)", 100, 1000, 500, step = 100),
actionButton("draw", "Draw"),
plotOutput('myplot')
))
server <- shinyServer(function(input, output, session) {
foo <- function(iterations = 5, interval = 500) {
i <- 0
output$myplot <- renderPlot({
i <<- i + 1
if (i < iterations)
invalidateLater(interval)
plot(iris$Sepal.Length, iris$Sepal.Width, col = i)
})
}
observeEvent(input$draw, foo(input$iterations, input$interval))
})
shiny::shinyApp(ui, server)
现在,您还可以将每个时间间隔都做某事的想法包装成一种延迟的 map
函数,看起来像这样:
map_later <- function(.x, .f, ..., .interval = 500) {
i <- 0
observe({
i <<- i + 1
if (i < length(.x))
invalidateLater(.interval)
.f(.x[i], ...)
})
}
这将产生一个更整洁、更易于管理的服务器:
ui <- shinyUI(fluidPage(
plotOutput('myplot')
))
server <- shinyServer(function(input, output, session) {
map_later(1:5, function(i) {
output$myplot <- renderPlot({
plot(iris$Sepal.Length, iris$Sepal.Width, col = i)
})
}, .interval = 500)
})
shiny::shinyApp(ui, server)
在这里命名可能不太好,但是,嘿,它做了它应该做的事情。