0
votes

I have an output to be rendered in a shiny app using DT::datatable.

The output which currently looks like this and I want to use the second row as the column headers:

tab1:
          V1      V2      V3      V4      V5      V6      V7      V8    
Month   Apr-17  May-17  Jun-17  Jul-17  Aug-17  Sep-17  Oct-17  Nov-17  
aaa     116.719 120.404 120.26  123.431 117.327 110.742 114.811 117.34  
bbb     76.118  75.976  76.215  76.134  77.19   78.519  78.258  74.522  

So when I want to use the row month as column headers I do the following:

   app1 <-reactive({ tab1()%>% .[-1,]})

And then I render it using datatable

 output$op1 <-renderDataTable({
    app1()
  })

I get the following output :

        V1    V2      V3      V4      V5      V6      V7      V8    
aaa     116.719 120.404 120.26  123.431 117.327 110.742 114.811 117.34  
bbb     76.118  75.976  76.215  76.134  77.19   78.519  78.258  74.522  

How do I replace the v1,v2 by month names .Thank you.

1

1 Answers

1
votes

First set_names on the data frame with the first row, and then remove the first row:

df %>% set_names(unlist(.[1,])) %>% tail(-1) # or .[-1,]
#     Apr-17  May-17 Jun-17  Jul-17  Aug-17  Sep-17  Oct-17 Nov-17
#aaa 116.719 120.404 120.26 123.431 117.327 110.742 114.811 117.34
#bbb  76.118  75.976 76.215  76.134   77.19  78.519  78.258 74.522

And in your case: reactive({ tab1() %>% set_names(unlist(.[1,])) %>% tail(-1) })