0

I have below code to use excelR to output a table of mtcars. I want to have the column names in vertical position instead of horizontally, how can I do that? It needs to be in excelR package as the user will need to update some of the information. Thanks!

library(shiny)
library(excelR)

ui <- fluidPage(
  excelOutput("v1")
)

server <- function(input, output, session) {
  df <- reactive({mtcars})

  output$v1 = renderExcel({
    excelTable(df())
  })
}

shinyApp(ui, server)

Edit:

The below answer from gdevaux is greatly appreciated and he has shown me it works on his pc. But somehow we couldn't figure out what went wrong that it didn't work on my Mac or Windows, tried both safari and chrome, R version are updated and tried opening from external browser.

Can someone else run the code below to see if works and if anyone knows what might be the reasons that it didn't work on mine?

Subaru Spirit
  • 394
  • 3
  • 19

1 Answers1

0

You can do it with CSS. Check out CSS documentation on text-orientation property to see the various possibilities of orientation.

library(shiny)
library(excelR)

ui <- fluidPage(
  
  # add CSS
  tags$head(
    tags$style(HTML("
    #your_table .resizable {
      writing-mode: vertical-rl;
      -webkit-text-orientation: sideways;
      text-orientation: sideways;
      }
    "))
  ),
  
  div(
    id = "your_table",
    excelOutput("v1")
  )
  
)

server <- function(input, output, session) {
  df <- reactive({mtcars})
  
  output$v1 = renderExcel({
    excelTable(df())
  })
}

shinyApp(ui, server)

EDIT : not working in RStudio viewer pane or window, you must use "run external"

gdevaux
  • 2,308
  • 2
  • 10
  • 19
  • I've tried the above code but the table's column names are still horizontal? Or do you mean I need to check out the CSS documentation to adjust the above code to rotate vertically? Thanks. – Subaru Spirit Jul 12 '21 at 16:08
  • they are vertical in my browser. Are you running your app in external browser or using Rstudio viewer ? in Rstudio viewer, it won't work correctly – gdevaux Jul 12 '21 at 16:10
  • I did try to open in browser. I'm using Mac, and I've tried the safari and chrome, but both look still the same horizontally. – Subaru Spirit Jul 12 '21 at 16:16
  • I've included an example that I found online that does work in my RStudio view and browser, but not sure how to transform that to fit my scenario using excelR, can you have a look please? thanks! – Subaru Spirit Jul 12 '21 at 16:20
  • I edited my post, can you copy past the whole code and try if it is working ? – gdevaux Jul 12 '21 at 16:20
  • Sure, thanks for trying to help! And I have just rerun the code from the edited version, still look the same as before on RStudio view, safari, chrome (Mac). – Subaru Spirit Jul 12 '21 at 16:22
  • Sure can you send me a link of what you found ? – gdevaux Jul 12 '21 at 16:23
  • Ok, also the example that works is in my edited post now as well. The link: https://community.rstudio.com/t/rendertable-rotate-colnames/20790/5 – Subaru Spirit Jul 12 '21 at 16:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/234784/discussion-between-gdevaux-and-subaru-spirit). – gdevaux Jul 12 '21 at 16:31
  • Not sure if this warning message has anything to do with why this isn't working on mine? Warning in excelTable(df()) : Since both column title and colHeaders are not specified 'data' column name will be used as column headers Since 'type' attribute is not specified and autoColTypes is true, detecting type from 'data' – Subaru Spirit Jul 12 '21 at 19:25
  • I have the same warning on windows, it's not a problem – gdevaux Jul 12 '21 at 20:26