Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 204824

Add arrow indicator in the 1st row of a dataframe instead of the second

$
0
0

I have the shiny app below which compares the values of the 1st row with the values of the 2nd and then displays arrows in the 2nd row. How can I apply this in the 1st row instead of the 2nd? I used this logic here

library(shiny)
library(shinydashboard)
library(dplyr)
library(formattable)

name<-c("John","John","John","John","John","John","John")
Dealer<-c("ASD","ASD","ASD","ASD","ASD","ASD","ASD")
Date<-c("2020-01-03","2020-01-04","2020-01-05","2020-01-06","2020-01-07","2020-01-08","2020-01-09")
dataset<-data.frame(name,Dealer,Date)

new<-dataset %>%
  mutate(Date = as.Date(Date)) %>%
  arrange_all %>%
  group_by(Dealer) %>%
  summarise(PreviousDay = sum(Date == last(Date) - 1), 
            PreviousThree = sum(Date %in% (last(Date) - 3) : last(Date))) %>%
  mutate(period = 2)

new2<-dataset %>%
  mutate(Date = as.Date(Date)) %>%
  arrange_all %>%
  group_by(Dealer) %>%
  summarise(PreviousDay = sum(Date == last(Date) - 2), 
            PreviousThree = sum(Date %in% (last(Date) - 6) : last(Date))) %>%
  mutate(period = 1)

total<-rbind(new,new2) %>%
  arrange(period)

sidebar <- dashboardSidebar()

body <- dashboardBody(
  fluidRow(box(width = 12, solidHeader = TRUE,
               formattableOutput("example_table"))
  )
)

ui <- dashboardPage(dashboardHeader(title = "Example"),
                    sidebar,
                    body
)


server <- function(input, output) {

  output$example_table <- renderFormattable({
    improvement_formatter <- formatter("span", 
                                       style = x ~ style(font.weight = "bold", 
                                                         color = ifelse(x-lag(x) > 0, "green", 
                                                                        ifelse(x-lag(x) < 0, "red", "black"))), 
                                       x ~ icontext(ifelse(x-lag(x)>0, "arrow-up", 
                                                           ifelse(x-lag(x)<0, "arrow-down", "")), 
                                                    x)
    )
    formattable(total, list(
                `PreviousThree` = improvement_formatter,
                `PreviousDay` = improvement_formatter)
    )
  })
}

shinyApp(ui, server)

Viewing all articles
Browse latest Browse all 204824

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>