R 使用循环更新ggplot2线图的数据
嗨,我是编程新手,还有“R”。编写以下代码以测试ping连续性:R 使用循环更新ggplot2线图的数据,r,loops,ggplot2,ping,R,Loops,Ggplot2,Ping,嗨,我是编程新手,还有“R”。编写以下代码以测试ping连续性: library(pingr) library(ggplot2) cc=2 counts <- c(1:1) pings <- c(ping("8.8.8.8",count = 1)) rgh <- data.frame(counts,pings) ggplot(rgh, aes(x=counts,y=pings))+geom_line(aes(col="red"))+coord_cartesian(xlim=
library(pingr)
library(ggplot2)
cc=2
counts <- c(1:1)
pings <- c(ping("8.8.8.8",count = 1))
rgh <- data.frame(counts,pings)
ggplot(rgh, aes(x=counts,y=pings))+geom_line(aes(col="red"))+coord_cartesian(xlim=(c(0,300)),ylim=(c(0,100)))
#qplot(x=counts,y=pings,data=rgh)
while (cc<300) {
counts <- c(counts,cc)
pings <- c(pings,ping("8.8.8.8",count = 1))
rgh <- data.frame(counts,pings)
print(ggplot(rgh, aes(x=counts,y=pings))+geom_line(aes(col="red"))+coord_cartesian(xlim=(c(0,300)),ylim=(c(0,100))))
cc <- cc+1
}
库(pingr)
图书馆(GG2)
cc=2
计数以下是一个示例,说明如何使用闪亮且生动的方式设置所需的行为:
library(shiny)
library(plotly)
library(pingr)
带有开始按钮和绘图区域的简单用户界面:
ui <- fluidPage(
div(actionButton("button", "start")),
div(plotlyOutput("plot"), id='graph')
)
server <- function(input, output, session) {
p <- plot_ly(
y = ping("8.8.8.8",count = 1),
type = 'scatter',
mode = 'lines')
output$plot <- renderPlotly(p)
observeEvent(input$button, {
while(TRUE){
Sys.sleep(1)
plotlyProxy("plot", session) %>%
plotlyProxyInvoke("extendTraces", list(y=list(list(ping("8.8.8.8",count = 1)))), list(0))
}
})
}
shinyApp(ui, server)
ui-Hmmm,我认为ggplot2方法可能不是最好的,甚至不是“正确的”工具/方法。您应该查看闪亮的
。ggplot2是为漂亮的静态图形而设计的,但shiny是为动态显示数据/图形而创建的。我们将尝试:)Thanks@Amar您仍然需要一个用于在shiny中绘图的包。如果不太麻烦的话。。您能否在Shining中共享一个示例代码以实现此结果?所以在我去阅读关于闪亮的教程之前,我会知道这是否是我想要的。。如果做起来不太容易,请忽略它。汉克斯:)我会试试那一个。嘿,我遇到了这个错误;好心的建议。侦听错误:[on_request_read]通过origRenderFunc()中的对等警告重置连接:忽略显式提供的小部件ID“231439807955”;Shiny不使用Nevermind,它在“运行窗口”上运行良好:)谢谢!但是,我们在代码的哪里提到了要执行的ping的数量?更新的答案。请核对。谢谢@missuse:)
server <- function(input, output, session) {
p <- plot_ly(
y = ping("8.8.8.8",count = 1),
type = 'scatter',
mode = 'lines')
a = 1
output$plot <- renderPlotly(p)
observeEvent(input$button, {
while(a <= 30){
a <- a + 1
Sys.sleep(1)
plotlyProxy("plot", session) %>%
plotlyProxyInvoke("extendTraces", list(y=list(list(ping("8.8.8.8",count = 1)))), list(0))
}
})
}