您能建议一种为shiny
添加动画点图/带状图的方法吗?
我要添加的数字是
x = sample(1:10, 100, TRUE)
for(i in seq_along(x)){
stripchart(x[1:i], method="stack", at=0.05, frame.plot=FALSE, xaxt="n", xlim=range(x))
axis(1, pretty(x))
Sys.sleep(0.05)
}
(理想情况下,我希望它的速度快于0.05,但建议使用R baulks)
当我尝试在shiny
中执行此操作时,它会延迟,然后一步一步绘制。
(ggplotly
doesn't seem like an option)
测试shiny
代码:
library(shiny)
ui = fluidPage( plotOutput("plot") )
server = function(input, output, session) {
x = sample(1:10, 100, TRUE)
rng = range(x)
output$plot <- renderPlot({
for(i in seq_along(x)){
stripchart(x[1:i], method="stack", at=0.05, frame.plot=FALSE,
pch=16, cex=2, xaxt="n", xlim=rng)
axis(1, pretty(x))
Sys.sleep(0.05)
}})}
shinyApp(ui = ui, server = server)
答案 0 :(得分:2)
根据StéphaneLaurent的建议,在侧面板中使用sliders,您可以使用滑块值对传递到stripchart
的数据进行子集化。
library(shiny)
ui <- fluidPage(
titlePanel("Sliders"),
sidebarLayout(
sidebarPanel(
sliderInput("animation", "Looping Animation:",
min = 0, max = 100,
value = 1, step = 10,
animate = animationOptions(interval = 300, loop = FALSE))
),
mainPanel(plotOutput("plot"))
))
server <- function(input, output) {
x = sample(1:10, 100, TRUE)
sliderValues <- reactive({ (input$animation)})
output$plot <- renderPlot({
stripchart(x[1:sliderValues()], method="stack", at=0.05, frame.plot=FALSE,
pch=16, cex=2, xaxt="n", xlim=range(x))
axis(1, pretty(x))
})
}
# Create Shiny app ----
shinyApp(ui, server)