代码之家  ›  专栏  ›  技术社区  ›  Vector JX

基于所选日期的闪亮范围查询

  •  3
  • Vector JX  · 技术社区  · 6 年前

    Query<-paste0("select ID, Date, Value, Result
                   From Table1
                   where date(date)>='2018-07-01'
                   and date(date)<='2018-08-31');")
    
    Dev1<-dbgetquery(database,Query)
    

    开发版本1:

    ID        Date                   Value        Result
    KK-112    2018-07-01 15:37:45    ACR          Pending
    KK-113    2018-07-05 18:14:25    ACR          Pass
    KK-114    2018-07-07 13:21:55    ARR          Accepted
    KK-115    2018-07-12 07:47:05    ARR          Rejected
    KK-116    2018-07-04 11:31:12    RTR          Duplicate
    KK-117    2018-07-07 03:27:15    ACR          Pending
    KK-118    2018-07-18 08:16:32    ARR          Rejected
    KK-119    2018-07-21 18:19:14    ACR          Pending
    

    使用上面提到的dataframe,我在R中创建了下面提到的pivot dataframe。

    Value      Pending   Pass    Accepted   Rejected   Duplicate
    ACR          3        1         0          0          0
    ARR          0        0         1          2          0
    RTR          0        0         0          0          0
    

    我只需要一点帮助来触发基于日期范围的查询(例如,如果在shiny dashboard上选择某个日期范围,数据会自动更新)。

    ui 仪表板。请建议如何组织表格和颜色的标题。

    我使用下面提到的示例代码来传递数据帧。

    library(shiny)
    library(dplyr)
    library(shinydashboard)
    library(tableHTML)
    
    ui <- dashboardPage(
    dashboardHeader(),
    dashboardSidebar(),
    dashboardBody(
      tableHTML_output("mytable")
       )
    )
    server <- function(input, output) {
    
        Date<-Dev1$Date
        {
        output$mytable <- render_tableHTML( {
          Pivot<-data.table::dcast(Dev1, Value ~ Result, value.var="ID", 
                                   fun.aggregate=length)
    
          Pivot$Total<-rowSums(Pivot[2:3])
    
          Pivot %>% 
            tableHTML(rownames = FALSE,
                      widths = rep(80, 7))
          })
        }
    }
    shinyApp(ui, server)
    

    所需样品设计:

    enter image description here

    3 回复  |  直到 6 年前
        1
  •  1
  •   Shree    6 年前

    下面是你可以做的-

    library(shiny)
    library(dplyr)
    library(data.table)
    library(shinydashboard)
    library(tableHTML)
    library(DT)
    
    ui <- dashboardPage(
      dashboardHeader(),
      dashboardSidebar(),
      dashboardBody(
        dateRangeInput("dates", "Select Dates"),
        actionButton("run_query", "Run Query"),
        br(), br(),
        tags$strong("Query that will be run when user hits above button"),
        verbatimTextOutput("query"),
        br(),
        tableHTML_output("mytable"),
        br(),
        DTOutput("scrollable_table")
      )
    )
    server <- function(input, output) {
    
      Dev1 <- eventReactive(input$run_query, {
        # Query <- sprintf("select ID, Date, Value, Result From Table1 where date(date) >= '%s' and date(date) <= '%s');",
        #                  input$dates[1], input$dates[2])
        # dbgetquery(database, Query)
        structure(list(ID = c("KK-112", "KK-113", "KK-114", "KK-115", 
                                      "KK-116", "KK-117", "KK-118", "KK-119"),
                               Date = c("2018-07-01 15:37:45", "2018-07-05 18:14:25", "2018-07-07 13:21:55", "2018-07-12 07:47:05", 
                                        "2018-07-04 11:31:12", "2018-07-07 03:27:15", "2018-07-18 08:16:32", 
                                        "2018-07-21 18:19:14"),
                               Value = c("ACR", "ACR", "ARR", "ARR", "RTR", "ACR", "ARR", "ACR"),
                               Result = c("Pending", "Pass", "Accepted", "Rejected", "Duplicate", "Pending", "Rejected", "Pending")),
                          .Names = c("ID", "Date", "Value", "Result"),
                          row.names = c(NA, -8L), class = "data.frame")
      })
    
      output$mytable <- render_tableHTML({
        req(Dev1())
        Pivot <- data.table::dcast(Dev1(), Value ~ Result, value.var="ID",
                                 fun.aggregate=length)
        Pivot$Total <- rowSums(Pivot[, 2:6])
        Pivot %>%
          tableHTML(rownames = FALSE, widths = rep(80, 7)) %>%
          add_css_header(., css = list(c('background-color'), c('blue')), headers = 1:7)
      })
    
      output$query <- renderPrint({
        sprintf("select ID, Date, Value, Result From Table1 where date(date) >= '%s' and date(date) <= '%s');",
                input$dates[1], input$dates[2])
      })
    
      output$scrollable_table <- renderDT({
        data.frame(matrix("test", ncol = 30, nrow = 5), stringsAsFactors = F) %>%
          datatable(options = list(scrollX = TRUE, paginate = F))
      })
    }
    shinyApp(ui, server)
    

    您可以使用 dateRangeInput() Dev1 . Live query显示在 verbatimTextOutput("query") 开发人员1 eventReactive mytable 开发人员1 更新。

    对于可水平滚动的表,我建议 DT 包装如下所示 DTOutput("scrollable_table") .

    注: 一定要消毒 Query

        2
  •  0
  •   Vishesh Shrivastav Akitha_MJ    6 年前

    您可以添加 sliderInput Date .

    library(shiny)
    library(dplyr)
    library(shinydashboard)
    library(tableHTML)
    library(DT)
    
    structure(list(ID = structure(1:8, .Label = c("KK-112", "KK-113", "KK-114", "KK-115", "KK-116", "KK-117", "KK-118", "KK-119"), 
                                  class = "factor"), 
                   Date = structure(c(17713, 17717, 17719, 17724, 17716, 17719, 17730, 17733), 
                                    class = "Date"), 
                   Value = structure(c(1L, 1L, 2L, 2L, 3L, 1L, 2L, 1L), .Label = c("ACR", "ARR", "RTR"), class = "factor"), 
                   Result = structure(c(4L, 3L, 1L, 5L, 2L, 4L, 5L, 4L), .Label = c("Accepted", "Duplicate", "Pass", "Pending", "Rejected"), 
                                      class = "factor")), class = "data.frame", row.names = c(NA, -8L))
    
    ui <- dashboardPage(
      dashboardHeader(),
      dashboardSidebar(
        # Add sliderInput for date - lets the user select a range of dates
        sliderInput("dates.range",
                    "Dates:",
                    min = min(Dev1$Date),
                    max = max(Dev1$Date),
                    value = as.Date("2018-07-18"),
                    timeFormat="%Y-%m-%d")
      ),
      dashboardBody(
        tableHTML_output("mytable"),
        dataTableOutput("mytable2")
      )
    )
    
    server <- function(input, output) {
    
      data.subsetted.by.date <- reactive({
        # Subset data - select dates which are in the user selected range of dates
        subset(Dev1, Date > min(Dev1$Date) & Date < input$dates.range)
      })
      # Output subsetted data as a DataTable
      output$mytable2 <- renderDataTable(data.subsetted.by.date())
    
      Date <- Dev1$Date
    
      output$mytable <- render_tableHTML({
          Pivot <- data.table::dcast(Dev1, Value ~ Result, value.var = "ID", fun.aggregate=length)
          Pivot$Total <- rowSums(Pivot[2:3])
          Pivot %>% 
            tableHTML(rownames = FALSE, widths = rep(80, 7))
        })
    
    }
    
    shinyApp(ui, server)
    

    你看我用过 renderDataTable dataTableOutput 从DT软件包中。这些允许为你的闪亮应用创建可滚动的表格。

        3
  •  0
  •   Galina Polishchuk    6 年前

    对于从-到的数据,您可以使用 dateRangeInput() 然后使用其中的输入过滤数据。

    例如:

    在你的 UI :

    dateRangeInput("ID", "Date", min = as.Date(min(Dev1$Date)), max = as.Date(max(Dev1$Date))
    

    Server :

    Pivot <- Dev1 %>% filter(Date >= input$ID[1] & Date <= input$ID[2])
    

    你的问题我听懂了吗?