2
votes

I would like to display table on my Shiny app based on the Min and Max Range that is take as input from the user.

Below is my data frame df1

    A   B    C            D
    12  23  Taken       Delivered
    23  32  Taken       Delivered
    32  1   Not Taken   Processing
    21  43  Not Taken   Processing
    12  76  Taken       Delivered
   124  49  Taken       Delivered
    15  14  Not Taken   Processing
    12  15  Taken       Delivered

The user first inputs the min value and max value of Column A and Column B and Clicks Apply, After that the only rows which satisfies the of both min and max value of both the Column A and Column B should get displayed.

For Example

Min Value of A = 5,  
Max Value of A = 15,  
Min value of B = 6,
Max Value of B = 15 then, only last two rows should get displayed.  
If none of the input rows satisfies the input values error message **Please 
Select Appropriate Rage** should display.   

UI.R

library(shiny)
shinyUI(fluidPage(
fluidRow(

      column(4,textInput("A.Mmin","A min")),

      column(4,textInput("AMmax", "A max"))),
fluidRow(        
      column(4,textInput("BMin","BMin")),

      column(4,textInput("BMin", "BMin")),

      column(4, submitButton(text = "Apply"))),

 fluidRow(        
      dataTableOutput('table')

 )))

Server.R

 library(shiny)
    shinyServer(function(input, output) {

    output$table <- renderDataTable(df1) })
2

2 Answers

2
votes

We could use eventReactive to get the values when we push the actionButton 'Apply' and then subset

library(shiny)
library(DT)
  

-ui

ui <- fluidPage(
  fluidRow(
    column(4,
           numericInput("Amin", "Amin", 5)),
    column(4,
           numericInput("Amax", "Amax", 15))),
                    
           
  fluidRow(        
    column(4,
           numericInput("Bmin", "Bmin", 5)),
    
    column(4, 
           
           numericInput("Bmax", "Bmax", 15)),
          
    column(4, actionButton("button", "Apply"))),
  
  fluidRow(        
    dataTableOutput('table')
    
  ))

-server

  server = function(input, output) {
    
    observeEvent(input$button, {
      cat("Showing", input$Amin, "as min value\n")
      cat("Showing", input$Amax, "as max value\n")
      cat("Showing", input$Bmin, "as min value\n")
      cat("Showing", input$Bmax, "as max value\n")
      
    })
   
    res <- eventReactive(input$button, {
      subset(df1, (A > input$Amin & A < input$Amax) & 
                      (B > input$Bmin & B < input$Bmax))
      
      
    })
    output$table <- renderDataTable({
      res()
    })
  }

-run

shinyApp(ui, server)  

-output

enter image description here

data

df1 <- structure(list(A = c(12L, 23L, 32L, 21L, 12L, 124L, 15L, 12L), 
B = c(23L, 32L, 1L, 43L, 76L, 49L, 14L, 15L), C = c("Taken", 
"Taken", "Not Taken", "Not Taken", "Taken", "Taken", "Not Taken", 
"Taken"), D = c("Delivered", "Delivered", "Processing", "Processing", 
"Delivered", "Delivered", "Processing", "Delivered")), .Names = c("A", 
"B", "C", "D"), class = "data.frame", row.names = c(NA, -8L))
0
votes

Here is the solution for you:

library(shiny)
library(gridExtra)
A <- c(12,23,32,21,12)
B <- c(23,32,1,43,76)
C <-c("Taken","Taken","Not Taken","Not Taken","Taken")
data <- data.frame(A,B,C)

ui <- fluidPage(
  fluidRow(

    column(4,textInput("AMmin","A min")),

    column(4,textInput("AMmax", "A max"))),
  fluidRow(        
    column(4,textInput("BMin","BMin")),

    column(4,textInput("BMax", "BMax")),

    column(4, submitButton(text = "Apply"))),

  fluidRow(        
    dataTableOutput('table')

  ))

server <- function(input,output){

  df1 <- reactive({
    df1 <- data[data$A > input$AMmin & data$A < input$AMmax & data$B > input$BMin & data$B < input$BMax , ]

  })
  output$table <- renderDataTable(df1())
}

app = shinyApp(ui,server)
runApp(app)

enter image description here You had few mistakes such as two widgets for textInput had the same id --> BMin (i guess copying problem).

Here is the part where i subset the data based on the choice (range min and max) from textInput:

df1 <- data[data$A > input$AMmin & data$A < input$AMmax & data$B > input$BMin & data$B < input$BMax , ]