【问题标题】:How to Format R Shiny DataTable Like Microsoft Excel Table如何像 Microsoft Excel 表一样格式化 R Shiny DataTable
【发布时间】:2018-02-23 15:12:04
【问题描述】:

我在 Microsoft Excel 中有一些表格,需要在 R Shiny App 中重新创建。 R 中的格式必须至少与原始上下文基本相同。

以下是原始表格的图片:

表 1

表 2

注意格式:表格标题下方和总计上方有行,标题和总计以粗体显示,月度账单列中的数字以逗号分隔并带有美元符号,表 2 中的最终数字被框内。

如果这些行不可重新创建,那很好,但我至少需要能够将所选主题、标题和总计加粗,并且能够为每月账单列获取正确的数字格式。

我尝试过使用 DT 包,但我不知道如何格式化行而不是列。我注意到 DT 使用 JavaScript 函数的包装器,但我个人并不了解 JavaScript。有没有办法通过 R 包或 Javascript 以我需要的方式格式化?

编辑:

虽然很简单,但我不能只包含表格的图像,因为某些数字将与用户输入相关联,并且必须能够更新。

【问题讨论】:

标签: r shiny dt


【解决方案1】:

pixiedust 可以轻松进行特定于单元格的自定义。

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】:

    如果您提供数据示例,这会更容易,但坚持使用DT,您应该能够利用formatStyle 更改行和列的格式。有关将第一行加粗的示例,请参见以下内容(假设您的数据框名为 df):

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

    rstudio DT 页面提供了更多示例:http://rstudio.github.io/DT/010-style.html

    或者,我认为您最好使用stargazer 包。

    基本图看起来与您想要的结果非常相似。

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

    这将使您入门,但请参阅此处以获得更多灵活性:https://www.jakeruss.com/cheatsheets/stargazer/

    【讨论】:

      猜你喜欢
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-04
      • 2021-11-17
      • 2022-10-30
      • 1970-01-01
      • 2021-01-19
      相关资源
      最近更新 更多