R Shiny display full dataframe without filter input until filterinputs are changed

前端 未结 2 687
悲哀的现实
悲哀的现实 2020-12-22 01:26

I am trying to build an app which displays a dataframe as a datatable. I wanted to provide users with ability to subset the datatable with selectinput and daterangeinput fil

相关标签:
2条回答
  • 2020-12-22 01:38

    ===Updated answer===

    Here is a working solution.

    library(shiny)
    library(dplyr)
    
    Region <- c("USA","UK","JPN","JPN","UK","USA","USA")
    Company <- c("A","B","C","A","B","C","A")
    Status <- c("Completed","In Production","In Design","New","In Production","In Design","New")
    date <- c("2015-05-01","2015-05-01","2015-06-04","2015-06-20","2015-07-15","2015-08-12","2015-08-12")
    date <- as.Date(date,"%Y-%m-%d")
    
    df <- data.frame(Region, Company, Status, date)
    
    ui <- shinyUI(
      fluidPage(
        titlePanel("Summary"),
    
        sidebarLayout(
          sidebarPanel(
    
            selectInput("region", 
                        label = "Choose a Region",
                        choices = c("", as.vector(unique(df$Region)))),
    
            selectInput("company", 
                        label = "Choose a company",
                        choices = c("", as.vector(unique(df$Company)))),
    
            selectInput("status", 
                        label = "Choose Proj status",
                        choices = c("", as.vector(unique(df$Status)))),
    
            dateRangeInput("projdate", 
                           label = "Date of interest:",
                           start = Sys.Date() - 60, 
                           end = Sys.Date())
          ),
    
          mainPanel(
            tableOutput("table")
          )
        )
      ))
    
    server <- shinyServer(function(input,output){
    
      output$table <- renderTable({
        region_sel <- if (nchar(input$region)==0) unique(as.vector(df$Region)) else input$region
        company_sel <- if (nchar(input$company)==0) unique(as.vector(df$Company)) else input$company
        status_sel <- if (nchar(input$status)==0) unique(as.vector(df$Status)) else input$status
    
        filter(df, Region %in% region_sel, Company %in% company_sel, Status %in% status_sel)
      })
    })
    
    shinyApp(ui = ui, server = server)
    

    ===Previous Answer===

    Not a direct answer, but here is a very similar example that may help you.

    library(shiny)
    library(dplyr)
    
    ui <- shinyUI(fluidPage(
    
       titlePanel("Old Faithful Geyser Data"),
    
       sidebarLayout(
          sidebarPanel(
            selectInput("cyl", "cyl", unique(mtcars$cyl), multiple = TRUE),
            selectInput("vs", "vs", unique(mtcars$vs), multiple = TRUE),
            selectInput("am", "am", unique(mtcars$am), multiple = TRUE)
          ),
    
          mainPanel(
             tableOutput("table")
          )
       )
    ))
    
    server <- shinyServer(function(input, output) {
    
        output$table <- renderTable({
            cyl_sel <- if (is.null(input$cyl)) unique(mtcars$cyl) else as.numeric(input$cyl)
            vs_sel <- if (is.null(input$vs)) unique(mtcars$vs) else as.numeric(input$vs)
            am_sel <- if (is.null(input$am)) unique(mtcars$am) else as.numeric(input$am)
            filter(mtcars, cyl %in% cyl_sel, vs %in% vs_sel, am %in% am_sel)
        })
    
    })
    
    shinyApp(ui = ui, server = server)
    
    0 讨论(0)
  • 2020-12-22 01:46

    Server.R

    shinyServer(function(input,output,session){
    
    
      data_update <- reactive({
    
        # Compose data frame
        data.frame(
          Name = c("Region", 
                   "Company",
                   "Status"),
          Value = as.character(c(input$region, 
                                 input$company,
                                 input$status)), 
          stringsAsFactors=FALSE)
      }) 
    
      # Show the values using an HTML table
      output$table <- renderTable({
        data_update()
      })
    })
    

    UI.R

    shinyUI(
      fluidPage(
        titlePanel("Summary"),
    
        sidebarLayout(
          sidebarPanel(
    
            selectInput("region", 
                        label = "Choose a Region",
                        choices = c("", unique(df$Region)), selected= '1'),
    
            selectInput("company", 
                        label = "Choose a company",
                        choices = c("", unique(df$Company)), selected= '1'),
    
            selectInput("status", 
                        label = "Choose Proj status",
                        choices = c("", unique(df$Status)), selected= '1'),
    
            dateRangeInput("projdate", 
                           label = "Date of interest:",
                           start = Sys.Date() - 60, 
                           end = Sys.Date())
          ),
    
          mainPanel(
            tableOutput("table")
          )
        )
      ))
    

    GLOBAL.R

    library(shiny)
    library(dplyr)
    library(DT)
    
    Region <- c("USA","UK","JPN","JPN","UK","USA","USA")
    Company <- c("A","B","C","A","B","C","A")
    Status <- c("Completed","In Production","In Design","New","In Production","In Design","New")
    date <- c("2015-05-01","2015-05-01","2015-06-04","2015-06-20","2015-07-15","2015-08-12","2015-08-12")
    date <- as.Date(date,"%Y-%m-%d")
    
    df <- data.frame(Region, Company, Status, date)
    

    I also recommend using DT package to plot your table.

    Hope it helps! Cheers

    0 讨论(0)
提交回复
热议问题