[英]Plot the Plotly Graph Dynamically in the Shiny App in R
我想在R的Shiny App中绘制一个Plotly图。我想要这种功能,以便在一个循环中绘制一定数量的点(例如20)。
这是我的Server.R代码:
xAxis = vector("numeric", as.numeric(input$Generations))
yAxis = vector("numeric", as.numeric(input$Generations))
graphDF = data.frame(cbind(xAxis, yAxis))
for(i in 1 : 5)
{ output$GA = renderPlotly({
print(graphDF) # Testing
graphDF$yAxis[i] = i
graphDF$xAxis[i] = i
print(graphDF) # Testing
# Plotly functionality
p <- plot_ly(graphDF, x = graphDF$xAxis, y = graphDF$yAxis)
})
}
非常感激任何的帮助。
亲切的问候
这比看起来要复杂。 您似乎想要迭代并创建一系列绘图图,并在操作时更改数据值。
由于“ Generations
滑块将向量重新初始化为新的长度,并且每次迭代都会更改要绘制的数据的状态,因此,您不仅可以级联反应函数。 将状态存储在reactiveValues
是处理此问题的好方法。
主要变化如下:
reactiveValues
存储xAxis
和yAxis
observeEvent
以在其值更改时重新初始化这些值 animate
参数,(可能)自己创建了一个反应式计时器。 编码:
library(shiny)
library(plotly)
u <- fluidPage(
titlePanel("Iterations of a plotly graph"),
sidebarLayout(
sidebarPanel(
sliderInput("Generations","Number of Generations:",
min = 1, max = 50, value = 20),
sliderInput("iter", "Iteration range:",
value = 1, min = 1, max = 1000, step = 1,
animate=animationOptions(interval=800, loop=T)),
p("To start click on the blue arrowhead")
),
mainPanel(
plotlyOutput("GA")
)
))
s <- shinyServer(function(input,output){
rv <- reactiveValues(xAxis=NULL,yAxis=NULL)
observeEvent(input$Generations,{
rv$xAxis=vector("numeric", as.numeric(input$Generations))
rv$yAxis=vector("numeric", as.numeric(input$Generations))
})
output$GA = renderPlotly({
rv$yAxis[input$iter] <- input$iter
rv$xAxis[input$iter] <- input$iter
gdf <- data.frame(xAxis=rv$xAxis, yAxis=rv$yAxis)
plot_ly(gdf, x = ~xAxis, y = ~yAxis, type="scatter",mode="markers")
})
})
shinyApp(u,s)
因为它是动态的,所以您必须运行它以查看其实际工作原理,但是下面是经过多次迭代的屏幕截图:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.