Добавьте индикатор стрелки в 1-й строке информационного кадра вместо второго - PullRequest
0 голосов
/ 27 января 2020

У меня есть блестящее приложение ниже, которое сравнивает значения 1-го ряда со значениями 2-го и затем отображает стрелки во 2-м ряду. Как я могу применить это в 1-м ряду вместо 2-го? Я использовал эту логику c здесь

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)

1 Ответ

1 голос
/ 27 января 2020

Если я правильно понял ваш вопрос, вы можете просто изменить lag () на lead () в форматере.

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-lead(x) > 0, "green", 
                                                                        ifelse(x-lead(x) < 0, "red", "black"))), 
                                       x ~ icontext(ifelse(x-lead(x)>0, "arrow-up", 
                                                           ifelse(x-lead(x)<0, "arrow-down", "")), 
                                                    x)
    )
    a <- formattable(total, list(
      `PreviousThree` = improvement_formatter,
      `PreviousDay` = improvement_formatter)
    )

  })
}

shinyApp(ui, server)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...