0

I have a checkbox and a table in my code. What I want is when user check certain box, new column with corresponding name with be generated.

Ideal Case Example:

enter image description here

However, this is what I have with my code:

enter image description here

Here is my code:

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")
}

I think the problem might shiny module can not be update when the checkbox is checked? Because I have tried to put the same code directly in app.R and it works just fine(as it shows in the 'ideal case example' image above).

Xinyi Li
  • 123
  • 1
  • 2
  • 8
  • It's hard to tell what's going on without a [reproducible example](https://stackoverflow.com/questions/48343080/how-to-convert-a-shiny-app-consisting-of-multiple-files-into-an-easily-shareable). Can you provide working code and some example data? Modules can be tricky, and they certainly require seeing the full code to help others test your app and see what you're missing. – phalteman Mar 18 '19 at 17:58

1 Answers1

0

This works like this:

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)
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225