首页 > 解决方案 > 使用反应数据集的模块内部的renderHighchart

问题描述

我正在尝试基于模块创建一个闪亮的应用程序,并且我需要在模块highchart内部添加一个模块,当用户从selectInput下拉列表中选择一个值时,图形会在该模块中更新。这个想法是,当用户从 UI 中的框中选择一个值时selectInput,这会过滤数据集,然后highchart更新。问题是我无法将过滤后的数据集传递给myModuleServer(见下文)。我怎样才能做到这一点?

这是数据集:

df <- data.frame(label = c('A','B'), value = c(10,25))

这些是模块 UI 和服务器:

myModuleUI <- function(id) {
  ns <- NS(id)
  tagList(
    textOutput(ns("title")),
    textOutput(ns("text")),
    highchartOutput(ns("graph")))}

myModuleServer <- function(input, output, session, action, dataset) {  
  output$title <- renderText({paste0("You selected letter: ",action())})
  output$text <- renderText({paste0("In the dataset the letter selected corresponds to number: ", dataset()$value)})
  output$graph <- renderHighchart({hchart(dataset(), 'bar', hcaes(x=dataset()$label, y=dataset()$value))})
  } 

在 Shiny App 的 UI 和服务器组件下方:

ui <- fluidPage(
  fluidRow(
    column(width = 3, wellPanel(selectInput('dummy', 'Select a letter', choices = c('A', 'B')))),
    column(width = 6, myModuleUI("module1")))
  )

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

  reactiveData <- reactive({input$dummy})
  reactiveDataSet <- reactive({df %>% filter(label == input$dummy)})

  callModule(myModuleServer, 'module1', action = reactiveData, dataset = reactiveDataSet)
}

shinyApp(ui, server)

标签: rshinyr-highcharter

解决方案


推荐阅读