skip to Main Content

I am currently working on a Shiny application where I am using the rhandsontable package to display a table. Here is the code I am using:

library(shiny)
library(rhandsontable)

ui <- fluidPage(
  mainPanel(
    rHandsontableOutput("myTable")
  )
)

server <- function(input, output) {
  output$myTable <- renderRHandsontable({
    df <- data.frame(matrix(1:(50**2), ncol = 50))
    rhandsontable(df, height = 10000, width = 10000)
  })
}

shinyApp(ui = ui, server = server)

I would like to have sticky column and row names, but I am not sure how to achieve this without using a box (this is why I have set a very large height and width). I have tried several methods but none of them seem to work. Any input would be appriciated.

2

Answers


  1. This example uses the hot_table function within the rhandsontable call to make row names sticky (stickyRows = TRUE) and include column headers (colHeaders = TRUE)

    library(shiny)
    library(rhandsontable)
    
    ui <- fluidPage(
      mainPanel(
        rHandsontableOutput("myTable")
      )
    )
    
    server <- function(input, output) {
      output$myTable <- renderRHandsontable({
        df <- data.frame(matrix(1:(50**2), ncol = 50))
        
        rhandsontable(df, height = 500, width = 800) %>%
        #rhandsontable(df) %>% #or use this
          hot_table(stickyRows = TRUE, 
                    colHeaders = TRUE)
      })
    }
    
    shinyApp(ui = ui, server = server)
    
    Login or Signup to reply.
  2. An approach based on hiding the scrollbars with CSS. You can change the width and height.

    library(shiny)
    library(rhandsontable)
    
    # Add CSS to hide scrollbars - may not work on all browsers.
    ui <- fluidPage(
      tags$head(
        tags$style(HTML(
          "::-webkit-scrollbar {
             display: none;
            }"
        ))
      ),
      mainPanel(
        rHandsontableOutput("myTable")
      )
    )
    
    # Set the width and height to reasonable values to get the hidden scroll bars working.
    server <- function(input, output) {
      output$myTable <- renderRHandsontable({
        df <- data.frame(matrix(1:(50**2), ncol = 50))
        rhandsontable(df, width = "100%", height = 500,
                      fixedRowsTop = 0, fixedColsLeft = 0)
      })
    }
    
    shinyApp(ui = ui, server = server)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search