2
votes

I have one question to open the topic already. Well, I'm trying to do a similar app to this one Shiny: dynamic dataframe construction; renderUI, observe, reactiveValues. And I would like to add a new category at the beginning which will select the variables from the table. I can not combine variables with other elements in an application. Could someone explain to me what I'm doing wrong? As you can see on the graphics program does not work well. enter image description here Below is a script

#rm(list = ls())
library(shiny)

data <- data.frame(Category1 = rep(letters[1:3],each=15),
                   Info = paste("Text info",1:45),
                   Category2 = sample(letters[15:20],45,replace=T),
                   Size = sample(1:100, 45),
                   MoreStuff = paste("More Stuff",1:45))

ui <- fluidPage(

  titlePanel("Test Explorer"),
  sidebarLayout(
    sidebarPanel(
      selectizeInput("show_vars", "Columns to show:",
                     choices = colnames(data), multiple = TRUE,
                     selected = c("Category1","Info","Category2")),
      uiOutput("category1"),
      uiOutput("category2"),
      uiOutput("sizeslider")
    ),
    mainPanel(
      tableOutput("table")
    )
  )
)

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


  data2 <- reactive({
    req(input$table)
    if(input$table == "All"){
      return(data)
    } 
    data[,names(data) %in% input$show_vars]
  })


  output$category1 <- renderUI({
    selectizeInput('cat1', 'Choose Cat 1', choices = c("All",sort(as.character(unique(data$Category1)))),selected = "All")
  })

  df_subset <- eventReactive(input$cat1,{
    if(input$cat1=="All") {df_subset <- data}
    else{df_subset <- data[data$Category1 == input$cat1,]}
  })

  df_subset1 <- reactive({
    if(is.null(input$cat2)){df_subset()} else {df_subset()[df_subset()$Category2 %in% input$cat2,]}
  })

  output$category2 <- renderUI({
    selectizeInput('cat2', 'Choose Cat 2 (optional):', choices = sort(as.character(unique(df_subset()$Category2))), multiple = TRUE,options=NULL)
  })

  output$sizeslider <- renderUI({
    sliderInput("size", label = "Size Range", min=min(df_subset1()$Size), max=max(df_subset1()$Size), value = c(min(df_subset1()$Size),max(df_subset1()$Size)))
  })

  df_subset2 <- reactive({
    if(is.null(input$size)){df_subset1()} else {df_subset1()[df_subset1()$Size >= input$size[1] & df_subset1()$Size <= input$size[2],]}
  })

  output$table <- renderTable({
    df_subset2()
  })
}

shinyApp(ui, server)
1
The issue is irrespective of what you select all the columns are displayed?amrrs
I want to be able to select which columns are shown. So it is in the example, but as you can see still showing up all the columns.Kim

1 Answers

0
votes

You don't need data2 since you are not using it and instead you can just use the same condition to filter columns with %in% everywhere you are displaying the dataframe.

#rm(list = ls())
library(shiny)

data <- data.frame(Category1 = rep(letters[1:3],each=15),
                   Info = paste("Text info",1:45),
                   Category2 = sample(letters[15:20],45,replace=T),
                   Size = sample(1:100, 45),
                   MoreStuff = paste("More Stuff",1:45))

ui <- fluidPage(

  titlePanel("Test Explorer"),
  sidebarLayout(
    sidebarPanel(
      selectizeInput("show_vars", "Columns to show:",
                     choices = colnames(data), multiple = TRUE,
                     selected = c("Category1","Info","Category2")),
      uiOutput("category1"),
      uiOutput("category2"),
      uiOutput("sizeslider")
    ),
    mainPanel(
      tableOutput("table")
    )
  )
)

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





  output$category1 <- renderUI({
    selectizeInput('cat1', 'Choose Cat 1', choices = c("All",sort(as.character(unique(data$Category1)))),selected = "All")
  })

  df_subset <- eventReactive(input$cat1,{
    if(input$cat1=="All") {df_subset <- data}
    else{df_subset <- data[data$Category1 == input$cat1,names(data) %in% input$show_vars]}
  })

  df_subset1 <- reactive({
    if(is.null(input$cat2)){df_subset()} else {df_subset()[df_subset()$Category2 %in% input$cat2,names(data) %in% input$show_vars]}
  })

  output$category2 <- renderUI({
    selectizeInput('cat2', 'Choose Cat 2 (optional):', choices = sort(as.character(unique(df_subset()$Category2))), multiple = TRUE,options=NULL)
  })

  output$sizeslider <- renderUI({
    sliderInput("size", label = "Size Range", min=min(df_subset1()$Size), max=max(df_subset1()$Size), value = c(min(df_subset1()$Size),max(df_subset1()$Size)))
  })

  df_subset2 <- reactive({
    if(is.null(input$size)){df_subset1()} else {df_subset1()[df_subset1()$Size >= input$size[1] & df_subset1()$Size <= input$size[2],names(data) %in% input$show_vars]}
  })

  output$table <- renderTable({
    df_subset2()
  })
}

shinyApp(ui, server)