闪亮的模块renderTable无法使用输入变量更改进行更新

时间:2019-03-18 16:09:47

标签: r shiny shiny-reactivity shinymodules

我的代码中有一个复选框和一个表。我想要的是当用户选中某些框时,将生成具有相应名称的新列。

理想案例:

enter image description here

但是,这是我的代码所拥有的:

enter image description here

这是我的代码:

lineGraphUI <- function(id) {
  ns <- NS(id)
  tags$div(
    checkboxGroupInput(ns("variable"), "Variables to show:",
                       c("black" = "black",
                         "white" = "white",
                         "asian" = "asian")),
    tableOutput(ns("datatbr"))
  )
}

lineGraph <- function(input, output, session) {
  da <- read.csv(file = "RaceByYearTemplet.csv", header = TRUE)  

  output$datatbr <- renderTable({
    da[c("year",input$variable), drop = FALSE]
  }, rownames = TRUE)
}

navBlockUI <- function(id) {
  ns <- NS(id)
  tags$div(
    tags$div(class = "tabPanel-plotBlock",
             tabsetPanel(type = "tabs",
                         tabPanel("Graph", lineGraphUI(ns("line"))),
                         tabPanel("Line", tablePlotUI(ns("table")))
             )
    ) 
  )
}

navBlock <- function(input, output, session) {
  callModule(lineGraph, "line")

  callModule(tablePlot, "table")
}

我认为如果选中此复选框,可能无法更新闪亮的模块的问题?因为我试图将相同的代码直接放在app.R中,并且效果很好(如上面的“理想情况示例”中的图像所示)。

1 个答案:

答案 0 :(得分:0)

这是这样的:

lineGraphUI <- function(id) {
  ns <- NS(id)
  tags$div(
    checkboxGroupInput(ns("variable"), "Variables to show:",
                       c("black" = "black",
                         "white" = "white",
                         "asian" = "asian")),
    tableOutput(ns("datatbr"))
  )
}

lineGraph <- function(input, output, session) {
  da <- iris[1:5,]
  names(da) <- c("black", "white", "asian", "abcd", "year")

  output$datatbr <- renderTable({
    da[, c("year",input$variable), drop = FALSE]
  }, rownames = TRUE)
}

navBlockUI <- function(id) {
  ns <- NS(id)
  tags$div(
    tags$div(class = "tabPanel-plotBlock",
             tabsetPanel(type = "tabs",
                         tabPanel("Graph", lineGraphUI(ns("line")))
             )
    ) 
  )
}

ui <- fluidPage(
  navBlockUI("xxx")
)
navBlock <- function(input, output, session) {
  callModule(lineGraph, "xxx-line")
}

shinyApp(ui, navBlock)