如何在Shiny renderTable中重命名列?

时间:2017-05-03 22:41:49

标签: r formatting shiny

我在Shiny中使用renderTable来显示table。我在输出中重命名列名时遇到了麻烦。

server.R:

reactive.tables <- reactiveValues()

output$lump.sum <- renderTable(
  if(input$my.password != am.password$password){
    data.frame(`There is no` = "report")
  } else {
    print(1)
    reactive.tables$occupancies %>%  # sum amounts by company
      group_by(company) %>%
      summarise(lump.sum.2 = sum(lump.sum.2), n = n()) %>%
      na.omit
  },
  colnames(output$lump.sum.2) = c("company", "lump sum", "occupancies")
)

重命名列似乎在反应性上下文之外正常工作。但是,每次在此反应式数据框中指定colnames参数时,都会出现以下错误:

ERROR: Error sourcing C:\Users\Carlos\AppData\Local\Temp\RtmpmmVUym\fileb803ae92d13

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:4)

我明白了。您可以使用rename():

简单地重命名列,而不是使用renderTable选项
output$lump.sum <- renderTable(
  if(input$my.password != am.password$password){
    data.frame(`There is no` = "report")
  } else {
    print(1)
  reactive.tables$lump.sum <- reactive.tables$occupancies %>%  # sum amounts by company
    group_by(company.unduplicated) %>%
    summarise(lump.sum.2 = sum(lump.sum.2), n = n()) %>%
    na.omit %>%
    rename(Company = company.unduplicated, Sum = lump.sum.2, Count = n)
},
colnames = TRUE
)

Example.