inst/shiny-examples/medidor/server.R

library(shiny)
library(stj)
library(stf)
library(medidor)
library(DT)
library(dplyr)
library(lubridate)
library(shinydashboard)
library(medidor)
library(shinyjs)

shinyServer(function(session, input, output) {
  
  observe({
    if (input$origem == 'stf') {
      updateSelectInput(session, 'select', 'Selecione um processo', 
                        choices = c('', d_stf$numero_cnj), selected = '')
    } else {
      updateSelectInput(session, 'select', 'Selecione um processo', 
                        choices = c('', d_stj$numero_cnj), selected = '')
    }
  })
  
  andamentos <- reactive({
    input$busca_side_btn
    input$select
    isolate({
      n <- ifelse(input$tipo_busca == 'busca', input$busca_side, input$select)
      if (!is.null(n) && stringr::str_length(n) == 20) {
        progress <- shiny::Progress$new(session, min = 0, max = 1)
        on.exit(progress$close())
        progress$set(message = 'Baixando os dados do Tribunal', detail = '')
        if (input$origem == 'stf') {
          d <- download_parse_stf(n)
        } else {
          d <- download_parse_stj(n)
        }
        progress$set(value = 1)
        d
      } else {
        dplyr::data_frame()
      }
    })
  })
  
  
  output$infos <- DT::renderDataTable({
    d <- andamentos()
    validate(
      need(ncol(d) > 0, "Selecione ou busque um processo.")
    )
    validate(
      need(nrow(d) > 0, "Não foi possível obter informações desse processo.")
    )
    if (nrow(d) > 0) {
      x <- d %>% mutate(data = as.Date(dmy(substr(data, 1L, 10L))))
      cores <- sapply(1:nrow(x), function(xx) verifica_andamento(x, xx)[2])
      
      x %>% 
        datatable(filter = 'none', class = 'display', 
                  selection = 'single',
                  options = list(searching = FALSE, paging = FALSE),
                  style = 'bootstrap') %>% 
        formatDate('data', 'toDateString') %>% 
        formatStyle(names(d), cursor = 'pointer')
    }
  })
  
  
  # Tirar a legenda
  # cores no dataframe
  # fazer funcionar clique no stj
  output$alertas <- renderUI({
    ind <- input$infos_cell_clicked
    # print(ind)
    if (!is.null(ind) && length(ind) > 0) {
      d <- andamentos()
      x <- d %>% mutate(data = as.Date(dmy(substr(data, 1L, 10L))))
      cores <- lapply(1:nrow(x), function(xx) verifica_andamento(x, xx))
      tempo <- cores[[ind$row]][1]
      corr <- cores[[ind$row]][2]
      dia <- ifelse(tempo == '1', 'dia', 'dias')
      tags$div(
        valueBox('', width = 12,
                 value = sprintf('%s %s até o andamento seguinte', tempo, dia), 
                 color = ifelse(corr == 'vermelho', 'red', 
                                ifelse(corr == 'verde', 'green', 
                                       'yellow')))
      )
    }
  })
  
})
jtrecenti/medidor documentation built on May 20, 2019, 3:17 a.m.