使用反应性数据集的模块内部renderHighchart

时间:2019-06-26 14:57:54

标签: r shiny r-highcharter

我正在尝试创建一个基于模块的Shiny App,我需要在模块内添加一个highchart,当用户从selectInput下拉列表中选择一个值时,图形就会更新。这个想法是,当用户从UI中的selectInput框中选择一个值时,这将过滤数据集,然后highchart被更新。问题是我无法将过滤后的数据集传递到myModuleServer(请参见下文)。我该怎么办?

这是数据集:

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

这些是UI和Server模块:

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和Server组件下面:

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)

0 个答案:

没有答案