Search code examples
rshinyhtmlwidgetsexcelr

Hide index column (row numbers) in excelR widget of Shiny app


I make making a Shiny app in R that shows the user an Excel-like grid. The excelR package is a wrapper for a JS package JSpreadsheet. This package automatically puts row numbers in the left-most column. I do not want them.

By digging into the JavaScript, I was finally able to figure out how to use an actionButton to remove the row numbers by sending a JS command:

library(shiny)
library(excelR)
library(shinyjs)

jsCode <- "shinyjs.hideindex = function(params) {document.getElementById('table').jexcel.hideIndex();}"

ui <- fluidPage(
  useShinyjs(),
  extendShinyjs(text = jsCode, functions = "hideindex"),
  excelOutput("table", height = 175),
  actionButton('hide_button', 'Hide Index Column')
)

server <- function(input, output, session){
  output$table <- renderExcel({
    excelTable(data = head(iris),
               columns = data.frame(title = names(iris),
                                    type = c('numeric', 'numeric', 'numeric', 'numeric', 'text')))
    })

  onclick("hide_button", js$hideindex())

}

shinyApp(ui, server)

But I would really like to have the table render automatically without the index column (row numbers). I tried to use observeEvents (and many, many other things) that watched for changes in input$table, but the input does not seem to get created until the table is edited.


Solution

  • I modified your example to make it more discrete, however it will run every time someone modify your app (because of the observe() function).

    library(shiny)
    library(excelR)
    library(shinyjs)
    
    jsCode <- "shinyjs.hideindex = function(params) {document.getElementById('table').jexcel.hideIndex();}"
    
    ui <- fluidPage(
      useShinyjs(),
      extendShinyjs(text = jsCode, functions = "hideindex"),
      excelOutput("table", height = 175),
      hidden(actionButton('hide_button', 'Hide Index Column')) # Hide from start 
    )
    
    server <- function(input, output, session){
      output$table <- renderExcel({
        excelTable(data = head(iris),
                   columns = data.frame(title = names(iris),
                                        type = c('numeric', 'numeric', 'numeric', 'numeric', 'text')))
      })
      
      observe({ # Automatic click on it even if hidden
        click("hide_button")
      })
      onclick("hide_button", js$hideindex())  
    }
    
    shinyApp(ui, server)
    

    It could be better to run this only at app start but I didn't solve it yet.