【问题标题】:Embed query to data table display in R Shiny App在 R Shiny App 中嵌入查询到数据表显示
【发布时间】:2026-01-24 06:00:01
【问题描述】:

我想根据用户输入的最小和最大范围在我的 Shiny 应用程序上显示表格。

下面是我的数据框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

用户首先输入Column AColumn Bmin valuemax value,然后点击Apply,然后唯一满足minmax两者的值的行Column A Column B 应该会显示出来。

举例

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

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    当我们推送actionButton 'Apply' 然后subset 时,我们可以使用eventReactive 来获取值

    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 = 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()
        })
      }
    

    -运行

    shinyApp(ui, server)  
    

    -输出

    数据

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

    【讨论】:

      【解决方案2】:

      以下是适合您的解决方案:

      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)
      

      您几乎没有错误,例如textInput 的两个小部件具有相同的 id --> BMin(我猜是复制问题)。

      这是我根据textInput 中的选择(范围最小值和最大值)对数据进行子集化的部分:

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

      【讨论】:

      • 感谢您的回复,但代码似乎无法正常工作。该应用程序显示所有四个文本框,即A maxAminBMaxBmin。但是在输入值时它不显示任何内容。另外我在您的代码中找不到submit button 的用法。
      • 代码运行良好,看看我使用的数据,我刚刚取了你的 5 行中的第一个......并且子集按钮有效,你需要记住必须有正确的选择要显示的数据范围,看看我在代码下方发布的屏幕截图...