0

I need your help, because I don't know how to solve my problem. I have my shiny app where I have data frame (imported from file) and checkboxgroupinput where I can mark which columns are for me interesting. After that in other tabpanel I would like to get two plot for each column (in one facet_wrap). All facet_wrap one under the other. The problem is that number of interesting columns is not constant. It is easy for my if I could hardcode number of rows with plots, but where it can change dynamically I have no idea how to program it, any tips from your side?

tomsu
  • 371
  • 2
  • 16
  • Please provide a minimal and reproducible example. See: [How to make a great R reproducible example?](http://stackoverflow.com/questions/5963269) – Axeman Jan 02 '18 at 09:17
  • Take a look at `uiOutput` and `renderUI`: https://shiny.rstudio.com/reference/shiny/latest/renderUI.html. – GyD Jan 02 '18 at 11:50

1 Answers1

0

We can't solve your question without a reproducible example but you should be able to figure it out from this quick example of using uiOutput along with renderUI. This allows the use of dynamic values in UI elements.

Normally you would define your static input as checkboxGroupInput("columns", "Select the variables to plot", choices = vector_of_known_values).

However as per your question, this doesn't work if the dataset is not known beforehand (e.g.: user file upload). In this case use uiOutput in the UI part: uiOutput("ui"), so that you delay evaluation to server side. In server side you can dynamically set the choices regardless of the data structure.

output$ui <- renderUI( {
  checkboxGroupInput("columns", "Select the variables to plot", choices = colnames(rv$data))
})

See full example:

library(shiny)
library(DT)
library(dplyr)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("datasets", "Select a dataset", choices = c("mtcars", "iris"), selected = "mtcars"),
      uiOutput("ui")
    ),
    mainPanel(
      DT::dataTableOutput("table")  
    )
  )
)

server <- function(input, output, session) {
  rv <- reactiveValues(data = NULL)

  observe( {
    rv$data <- eval(parse(text = input$datasets))
  })

  filtered <- reactive( {
    req(input$columns)
    if( all(!input$columns %in% colnames(rv$data))) {
      NULL
    } else {
      rv$data %>% select(input$columns) 
    }
  })

  output$ui <- renderUI( {
    checkboxGroupInput("columns", "Select the variables to plot", choices = colnames(rv$data))
  })

  output$table <- DT::renderDataTable( {
    req(filtered())
    DT::datatable(filtered())
  })
}

shinyApp(ui, server)
GyD
  • 3,902
  • 2
  • 18
  • 28