Home > database >  How to display data frame for calculated difference between times?
How to display data frame for calculated difference between times?

Time:10-20

I'm a beginner, which is worth mentioning at the beginning. I wanted to create an application in which the user enters an arbitrary date and, based on it, calculates the time between that date and the last activity of a person from the data table. if that time is greater than the value of input$disabled, which is the expected time of being offline, I would like to display all the information about those people. currently, I'm getting the following problem: Error in UseMethod: no applicable method for 'filter' applied to an object of class "c('reactiveExpr', 'reactive', 'function')" and for the second one: Error in UseMethod: no applicable method for 'mutate' applied to an object of class "difftime".

last_event has a character type and looks like this: "2019-12-22 00:00:0", which is why I overlay as.Date() on it. I am not sure if I used reactive() correctly, because this application is a work of trial and error. Can someone help me? I would be very grateful, because it is kinda frustrating for me right now.

last_event <- c("2019-12-26 00:00:00","2020-10-21 00:00:00","2020-05-27 00:00:01","2020-02-25 00:00:00","2020-10-09 00:00:00","2020-10-16 00:00:00","2019-12-01 00:00:01")
id <- c(1:7)
users_name <- c("Krox", "Minit", "Brulon", "Loc", "Mese02", "Robu78", "CoffeeMan")
data <- data.frame(id, users_name, last_event)

ui <- dashboardPage(
  dashboardHeader(title = "X"),
  dashboardSidebar(
    dateInput(inputId = "date", label = "Podaj date : "),
    sliderInput(inputId = "disabled", label = "Czas nieaktywnosci (msc): ", min = 6, max = 24, value = 12)),
  dashboardBody(
    dataTableOutput(outputId = "table")
  ))

server <- function(input, output){
  reactive_data <- eventReactive(input$date, {
    mutate(diff = (difftime(as.Date(input$date), as.Date(data$last_event)))/30)
    data
  })
  
  output$table <- renderDataTable({
    data <- reactive_data()
    if (input$disabled){
      data %>%
        filter(diff > input$disabled)
    }})
}

shinyApp(ui, server)

Also I try to write this code in another way:

library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(readxl)
library(dplyr)

data <- read_excel(file.choose()) 

ui <- dashboardPage(
  dashboardHeader(title = "X"),
  dashboardSidebar(
    dateInput(inputId = "date", label = "Podaj date : "),
    sliderInput(inputId = "disabled", label = "Czas nieaktywnosci (msc): ", min = 6, max = 24, value = 12)),
  dashboardBody(
    dataTableOutput(outputId = "table")
  ))

server <- function(input, output){
  data1 <- reactive(data %>%
                      mutate(diff = (difftime(as.Date(input$date)) - as.Date(data1$last_event))/30))
  
  
  output$table <- renderDataTable(data1 %>%
                                    filter(diff > input$disabled))
}

shinyApp(ui, server)

CodePudding user response:

Please try the below:

last_event <- c("2019-12-26 00:00:00","2020-10-21 00:00:00","2020-05-27 00:00:01","2020-02-25 00:00:00","2020-10-09 00:00:00","2020-10-16 00:00:00","2019-12-01 00:00:01")
id <- c(1:7)
users_name <- c("Krox", "Minit", "Brulon", "Loc", "Mese02", "Robu78", "CoffeeMan")
data <- data.frame(id, users_name, last_event)

ui <- dashboardPage(
    dashboardHeader(title = "X"),
    dashboardSidebar(
        dateInput(inputId = "date", label = "Podaj date : "),
        sliderInput(inputId = "disabled", label = "Czas nieaktywnosci (msc): ", min = 6, max = 24, value = 12)),
    dashboardBody(
        dataTableOutput(outputId = "table")
    ))

server <- function(input, output){
    reactive_data <- eventReactive(input$date, {
        mutate(data, diff = (difftime(as.Date(input$date), as.Date(data$last_event)))/30)
    })
    
    output$table <- renderDataTable({
        data <- reactive_data()
        
        if (input$disabled){
            data %>%
                filter(diff > input$disabled)
        }})
}

shinyApp(ui, server)

This line needed work:

mutate(data, diff = (difftime(as.Date(input$date), as.Date(data$last_event)))/30)

Alternatively:

data %>% mutate(diff = (difftime(as.Date(input$date), as.Date(data$last_event)))/30)

Update

for your second code example, see here:

library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(readxl)
library(dplyr)

data <- read_excel(file.choose()) 

ui <- dashboardPage(
    dashboardHeader(title = "X"),
    dashboardSidebar(
        dateInput(inputId = "date", label = "Podaj date : "),
        sliderInput(inputId = "disabled", label = "Czas nieaktywnosci (msc): ", min = 6, max = 24, value = 12)),
    dashboardBody(
        dataTableOutput(outputId = "table")
    ))

server <- function(input, output){
    data1 <- reactive(data %>%
                          mutate(diff = (difftime(as.Date(input$date), as.Date(data$last_event))/30)))
    
    
    output$table <- renderDataTable(data1() %>%
                                        filter(diff > input$disabled))
}

shinyApp(ui, server)

CodePudding user response:

Actually I solved this problem by myself, but thank you all for answers. The final code looks like that:

library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(readxl)
library(dplyr)
library(DT)

ui <- dashboardPage(
  dashboardHeader(title = "X"),
  dashboardSidebar(
    dateInput(inputId = "date", label = "Choose date : "),
    sliderInput(inputId = "disabled", label = "Choose time of inactivity: ", min = 6, max = 24, value = 12)),
  dashboardBody(
    dataTableOutput(outputId = "table")
  ))

server <- function(input, output){
  data1 <- reactive(read_excel(file.choose()))

  output$table <- renderDT({
    data1() %>%
      mutate(diff = (difftime(input$date, as.Date(last_event))/30)) %>% 
      filter(diff > input$disabled) %>%
      select(-diff) %>% 
      DT::datatable(options(list(scrollX=TRUE)))
  })
}

shinyApp(ui, server)
  • Related