加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-14 01:50:52 所属栏目:Windows 来源:网络整理
导读:我在Microsoft Excel中有一些表需要在R Shiny App中重新创建. R中的格式必须至少与原始上下文保持一致. 这是原始表的图像: 表格1 表2 请注意格式:表格标题下面有行和上面的总计,标题和总数都是粗体,“每月帐单”列中的数字有数千个用逗号分隔并有美元符号,
我在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()

        })

    })
)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读