【问题标题】:R shiny: Edit the format of a table outputR闪亮:编辑表格输出的格式
【发布时间】:2015-03-05 10:37:55
【问题描述】:

我正在使用闪亮(使用 renderTable 和 tableOutput)输出表格,有没有办法编辑表格的格式和表格中的值。

我特别想

  • 使用逗号作为 1000 超级标记(即将 1234567 更改为 1,234,567)
  • one 列中的每个值之前放置一个 £ 符号
  • 将最后一行加粗
  • 删除行名

因此,如果您采用闪亮的反应性,例如举个例子

runExample('03_reactivity')

这会输出一个表'view',它的server.R代码是

  output$view <- renderTable({
    head(datasetInput(), n = input$obs)
  })

它的ui.R代码是

tableOutput("view")

在这个例子中我想输出

area    peri        shape   perm
£4,990  2,791.90    0.09    6.30
£7,002  3,892.60    0.15    6.30
£7,558  3,930.66    0.18    6.30
£7,352  3,869.32    0.12    6.30
£7,943  3,948.54    0.12    17.10
£7,979  4,010.15    0.17    17.10
£9,333  4,345.75    0.19    17.10
£8,209  4,344.75    0.16    17.10
£8,393  3,682.04    0.20    119.00
£6,425  3,098.65    0.16    119.00 

(标题保持粗体,底行也是粗体,结果stackoverflow同样难以获得我想要的格式;))

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您好,我在回答中添加了 cmets,希望这会有所帮助。在此处查看Datatables,了解如何自定义表格的更多信息。

    rm(list = ls())
    library(shiny)
    library(scales)
    
    # Sample Data
    area <- seq(from=10,to=100, by=10)
    peri <- seq(from=2710.1,to=2800.1, by=10)
    shape <- seq(from=0.1,to=1, by=0.1)
    perm <- seq(from=1,to=100, by=10)
    my_data <- as.data.frame(cbind(area,peri,shape,perm))
    
    ui = fluidPage(
      sidebarLayout(
        sidebarPanel(),
        mainPanel(
          tableOutput("view"),
          #4 Make the final row bold using tags$style
          tags$style(type="text/css", "#view tr:last-child {font-weight:bold;}")
        ),
      )
    )
    
    server = function(input, output) {
      output$view <- renderTable({
        test <- my_data
        #1 Comma Seperator as 1000: you can use the library(scales) and the comma function
        test$peri<-comma(test$peri)
        #2 Out the "£" sign before every value in column 1 (or any column): you can use paste0
        test$area <- paste0("£",test$area)
        test
        #3 Remove row names : use inlcude.rownames=FALSE
      },include.rownames=FALSE)
    
    }
    runApp(list(ui = ui, server = server))
    

    【讨论】:

    • 如果您想使用 DataTables,可以查看 DT 包:github.com/rstudio/DT
    • 哦,太好了,我不知道这个包,我在数据表上做的大部分事情都是用 tags$style 或 tags$scrip,干杯
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 2014-05-15
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 2023-03-21
    • 2021-10-20
    相关资源
    最近更新 更多