Reputation: 33
I would like to generate dynamic multiplots in R with shiny and echarts4r. I expect 3 different charts, but get 3 times the same chart and i don't get why. If I plot them non dynamically, I get the expectet plots.
rm(list=ls())
library(shiny)
library(echarts4r)
#library(pryr)
data1=1:3
data2=rep(0,3)
data = data.frame(data1,data2)
ui <- fluidPage(
headerPanel("MultiPlot"),
mainPanel(
uiOutput("main"),
actionButton("start","Start")
)
)
plotdata = function(listeX){
liste = listeX[[1]]
data = listeX[[2]]
while(any(data$data2==0))
{
pos_0 = which(data$data2==0)[1]
data$data2[pos_0]=1
data_plot=data[pos_0,]
liste[[length(liste)+1]] = renderEcharts4r({
data_plot %>%
e_charts(data1) %>%
e_line(data2) %>%
e_tooltip("axis","item") %>%
e_title(paste0("Chart",pos_0))
})
listeX = list(liste,data)
plotdata(listeX)
}
return(listeX)
}
server <- function(input, output,session) {
observeEvent(input$start,{
output$main = renderUI({
liste = list(
tags$br()
)
listeX = list(liste,data)
listeX = plotdata(listeX)
liste = listeX[[1]]
tagList(
liste
)
})
})
}
shinyApp(ui = ui, server = server)
I have tried recursive code as well as iterative code, but i get 3 times the last generated plot each time
Upvotes: 0
Views: 167
Reputation: 33
I have solved my problem. First I created all the echarts4rOutputs and after that I plot iterativ for every row. Here the working code:
library(shiny)
library(echarts4r)
data1=1:3
data2=1:3
data_all = data.frame(data1,data2)
ui <- fluidPage(
headerPanel("MultiPlot"),
mainPanel(
actionButton("start","Start"),
uiOutput("plotMain"),
)
)
plotdata = function(data){
data_plot = data
plotx <- renderEcharts4r({
data_plot %>%
e_charts(data1) %>%
e_line(data2) %>%
e_tooltip("axis","item") %>%
e_title(paste0("Chart",data_plot$data1))
# e_mark_line(data = list(yAxis=y_min),title="UT",col="red") %>%
# e_mark_line(data = list(yAxis=y_max),title="OT")
})
return(plotx)
}
server <- function(input, output,session) {
output$plotMain = renderUI({
liste = list()
for(i in 1:nrow(data_all))
{
liste[[(length(liste)+1)]] = echarts4rOutput(paste0("plot",i))
}
liste
})
observeEvent(input$start,{
for(i in 1:nrow(data_all))
{
plotx = plotdata(data_all[i,])
output[[paste0("plot",i)]] = plotx
}
})
}
shinyApp(ui = ui, server = server)
Upvotes: 0