1
votes

I am using renderTable in Shiny to display a table. I am having trouble renaming the column names in the output.

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")
)

Renaming columns seems to work fine outside of a reactive context. However, every time I specify the colnames argument within this reactive dataframe, I get the following error:

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

Any suggestions would be greatly appreciated.

1
Where is output$annual.sum initially defined? Is it a misspelling of output$lump.sum? - Parfait
i dont think it matters, as i doubt one could set colnames referencing an output like that anyway. But i do agree we would need a reproducible example,... - Tonio Liebrand

1 Answers

6
votes

I figured it out. Instead of messing around with renderTable options, you can simply rename the columns using rename():

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.