微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何格式化R Shiny DataTable像Microsoft Excel表格

我在Microsoft Excel中有一些表需要在R Shiny App中重新创建. R中的格式必须至少与原始上下文保持一致.

这是原始表的图像:

表格1

Excel Table 1

表2

Excel Table 2

请注意格式:表格标题下面有行和上面的总计,标题和总数都是粗体,“每月帐单”列中的数字有数千个用逗号分隔并有美元符号,表2中的最终数字是用框输入的.

如果这些行不可再生,那就没问题了,但我至少需要能够加粗选定的主题,标题和总数,并能够获得每月帐单列的正确数字格式.

我已经尝试使用DT包但我无法弄清楚如何格式化行而不是列.我注意到DT使用JavaScript函数的包装器,但我自己并不亲自了解JavaScript.有没有办法通过R包或Javascript以我需要的方式格式化这个?

编辑:

虽然这很简单,但我不能只包括表格的图像,因为有些数字会链接用户输入,并且必须能够更新.

解决方法

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.09994),Transition = c(0.00303,0.05585,0.00459,0.07611,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,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,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()

        })

    })
)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐