Skip to content

Instantly share code, notes, and snippets.

@aarong1
Last active October 9, 2021 20:18
Show Gist options
  • Save aarong1/03796aae5d3dd3b4a39b85f062851647 to your computer and use it in GitHub Desktop.
Save aarong1/03796aae5d3dd3b4a39b85f062851647 to your computer and use it in GitHub Desktop.
This is a solution that gets rid of the dot that is default were using selected rows in reactable. This code snippet demonstrates a coloured row like that of DataTables for selected elements that gets rid of the dot but runs a bit slower. Thanks #r #shiny #reactable
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(reactable)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("This displays highlighting \n rows in reactable without
that ugly dot thing"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
shiny::tags$button('custom button w/o bootstrap')
),
# Show a plot of the generated distribution
mainPanel(
shiny::tags$section(
reactableOutput('mytable')
)
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
# // Only handle click events on the 'mb' column
# if (colInfo.id !== 'mpg') {
# return
# }
onclick_js <- JS(
"function(rowInfo, colInfo) {
// Send the click event to Shiny, which will be available in input$show_details
// Note that the row index starts at 0 in JavaScript, so we add 1
if (window.Shiny) {
console.log(rowInfo)
console.log(colInfo)
Shiny.setInputValue('show_rows',rowInfo)
Shiny.setInputValue('show_details',
{ index: rowInfo.index + 1,
rnd: Math.random() })
}
}"
)
#footer <- function(values) format(sum(values, na.rm = TRUE), big.mark = ",")
output$mytable <- renderReactable({
reactable(
mtcars,
compact = TRUE,
defaultColDef = colDef(minWidth = 30,
footerStyle = "font-weight: bold",
style = function(value, index, name) {
list(background = ifelse(
index==
input$show_details$index,
"darksalmon",
"white"),
border='solid',
borderWidth='1px 0px 0px 0px',
margin = "0px")}
),
highlight = TRUE,
defaultPageSize = round((1200 - 345) / 31),
paginationType = "simple",
wrap = FALSE,
onClick = onclick_js)
})
observeEvent(input$show_details,{
print(input$show_details)})
observeEvent(input$show_rows,{
print(input$show_rows)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment