How to Format R Shiny DataTable Like a Microsoft Excel Spreadsheet

I have several tables in Microsoft Excel that I need to recreate in an R Shiny application. Formatting in R should remain at least basically the same as the original context.

Here are the images of the source tables:

Table 1

Excel spreadsheet 1

table 2

Excel spreadsheet 2

Pay attention to formatting. There are rows under the headings of the tables, and the totals, headers and totals are shown in bold, the numbers in the Monthly Account column have thousands separated by commas and have dollar characters, and the final number in table 2 is put in a square.

If the rows were not corrected, that would be nice, but I need to at least be able to highlight the highlighted topics, headers and totals and be able to get the correct number format for the Monthly Bill column.

I tried using the DT package, but cannot figure out how to format rows instead of columns. I noticed that DT uses wrappers for JavaScript functions, but I personally don't know JavaScript. Is there a way to format this method that I need through R packages or Javascript?

Edit:

Although this would be simple, I cannot just include the image of the tables, because some of the numbers will be associated with user input and should be able to update.

+4
source share
2 answers

pixiedust simplifies cellular network setup.

T1 <- data.frame(Charge = c("Environmental", "Base Power Cost",
                            "Base Adjustment Cost", "Distribution Adder",
                            "Retail Rate Without Fuel", "Fuel Charge Adjustment",
                            "Retail Rate With Fuel"),
                 Summer = c(0.00303, 0.06018, 0.00492, 0.00501, 0.07314,
                            0.02252, 0.09566),
                 Winter = c(0.00303, 0.05707, 0.00468, 0.01264, 0.07742, 
                            0.02252, 0.09994),
                 Transition = c(0.00303, 0.05585, 0.00459, 0.01264,
                                0.07611, 0.02252, 0.09863),
                 stringsAsFactors = FALSE)

T2 <- data.frame(Period = c("Summer", "Winter", "Transition", "Yearly Bill"),
                 Rate = c(0.09566, 0.09994, 0.09863, NA),
                 Monthly = c(118.16, 122.44, 121.13, 1446.92),
                 stringsAsFactors = FALSE)

library(shiny)
library(pixiedust)
library(dplyr)
options(pixiedust_print_method = "html")

shinyApp(
  ui = 
    fluidPage(
      uiOutput("table1"),
      uiOutput("table2")
    ),

  server = 
    shinyServer(function(input, output, session){

      output$table1 <-
        renderUI({
          dust(T1) %>% 
            sprinkle(rows = 1, 
                     border = "bottom", 
                     part = "head") %>% 
            sprinkle(rows = c(5, 7),
                     cols = 2:4,
                     border = "top") %>% 
            sprinkle(rows = c(5, 7),
                     bold = TRUE) %>% 
            sprinkle(pad = 4) %>% 
            sprinkle_colnames(Charge = "") %>% 
            print(asis = FALSE) %>% 
            HTML()
        })

      output$table2 <- 
        renderUI({
          T2 %>% 
            mutate(Monthly = paste0("$", trimws(format(Monthly, big.mark = ",")))) %>% 
            dust() %>% 
            sprinkle(rows = 1,
                     border = "bottom",
                     part = "head") %>% 
            sprinkle(rows = 4,
                     cols = 1,
                     bold = TRUE) %>% 
            sprinkle(rows = 4,
                     cols = 3,
                     border = "all") %>% 
            sprinkle(na_string = "", 
                     pad = 4) %>% 
            sprinkle_colnames(Period = "",
                              Monthly = "Monthly Bill") %>% 
            print(asis = FALSE) %>% 
            HTML()

        })

    })
)
+2
source

, , DT, formatStyle . , , . ( df):

df %>%
  datatable() %>%
  formatStyle(
    0,
    target = "row",
    fontWeight = styleEqual(1, "bold")
  )

rstudio DT : http://rstudio.imtqy.com/DT/010-style.html

, , stargazer.

.

stargazer::stargazer(df, type = "html", title = "Table 1")

, . : https://www.jakeruss.com/cheatsheets/stargazer/

+1

Source: https://habr.com/ru/post/1685686/


All Articles