【问题标题】:How to rename columns in a Shiny renderTable?如何重命名闪亮渲染表中的列?
【发布时间】:2017-05-03 22:41:48
【问题描述】:

我在 Shiny 中使用 renderTable 来显示 table。我在重命名输出中的列名时遇到问题。

服务器.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

任何建议将不胜感激。

【问题讨论】:

  • output$annual.sum 最初定义在哪里?是output$lump.sum的拼写错误吗?
  • 我认为这并不重要,因为我怀疑是否可以设置 colnames 来引用这样的输出。但我同意我们需要一个可重现的例子,...

标签: r formatting shiny


【解决方案1】:

我想通了。您可以简单地使用 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.

【讨论】:

  • 感谢您发布此答案 - 使用“%>%”是关键,对我有很大帮助
猜你喜欢
  • 2016-06-12
  • 1970-01-01
  • 2016-01-11
  • 2016-03-04
  • 1970-01-01
  • 2016-04-07
  • 2014-12-12
  • 1970-01-01
  • 2015-01-11
相关资源
最近更新 更多