XD-DENG / ECharts2Shiny

To insert interactive charts from ECharts into R Shiny applications (在R Shiny app中插入ECharts可交互图形)
https://CRAN.R-project.org/package=ECharts2Shiny
GNU General Public License v2.0
129 stars 47 forks source link

How to use this package in modules #61

Open Sixing-r opened 4 years ago

Sixing-r commented 4 years ago

Hi Xiaodong,

I try to use this package to display a pie chart in my shiny app including modules, but there is no error and no plot shown.

ui.R

library(shiny)
library(ECharts2Shiny)

fluidPage(
  h1("Test shiny app"),
  sidebarLayout(
    sidebarPanel(
      id = "User_input",
      h1(
        textOutput("edit_inv_title"), 
        class = "text-center"
      ),
      input_ui("input")
    ),
    mainPanel(
      graph_ui("graph")
    )
  )
)

server.R

function(input, output, session){

  input_list <- callModule(input_module, "input")

  observeEvent(input_list(),{
    callModule(graph_module, "graph", input_list = input_list)
  })
}

global.R

source("modules/graph.R")
source("modules/input.R")

input.R

input_ui <- function(id){

  ns <- NS(id)

  tagList(
    selectInput(
      ns("select_box"),
      "Choose what content you wanna check",
      choices = c("A",
                  "B",
                  "C",
                  "D",
                  "E"))
  )
}

input_module <- function(input, output, session){

  input_list <- reactive({
    list("select" = input$select_box
    )
  })
  return(input_list)
}

graph.R

library(ECharts2Shiny)

graph_ui <- function(id){

  ns <- NS(id)
  loadEChartsLibrary()

  tagList(
    tags$div(id="test", style="width:50%;height:400px;"),
    deliverChart(div_id = ns("pie_chart"),
                 running_in_shiny = T)
  )

}

graph_module <- function(input, output, session, input_list = NULL){

  p1 <- reactive({

    if(input_list()[["select"]] == "A"){

     dat <- data.frame(
       name = c("A", "B", "C", "D"),
       value = c(10,20,30,40)
     )
      option_list <- list(
        "data" = dat
        )
    }
  })

  observeEvent(p1()$data, {renderPieChart(
    div_id = 'pie_chart',
    data = p1()$data
  )})
}
XD-DENG commented 4 years ago

Hi @Sixing-r , thanks a lot for sharing the issue.

I have been away from R-related projects for quite a while, and may not have enough time to look into this issue you kindly shared. Sorry for it.

I would be more than happy if you can help dive deeper and check potential root-cause. Please share with me if you do find anything. Thanks a lot, and apologize again.