Home > Software engineering >  Shiny R- Why aren't formatStyle() changes visible in my output data table?
Shiny R- Why aren't formatStyle() changes visible in my output data table?

Time:10-06

I am trying to change the format and style of my output table in Shiny using formatStyle() but the changes are not visible. What might be wrong? For example, I want the "Yes" to be bold but the final results do not show these changes. This is my final output:

enter image description here

This is my code:

library(shiny)
library(dplyr)
library(DT)
library(readxl)
var_version_tables <- read_excel("test_2_filtros_2.xlsx")
df<-var_version_tables

ui <- fluidPage(
  selectInput("var_1", "Categories", choices = unique(df$var_1)),
  selectInput("var_2", "Tables", choices = NULL, multiple = T),
  DT::dataTableOutput("data")
)





server <- function(input, output, session) {
  categories <- reactive({
    filter(df, var_1 == input$var_1)
  })
  observeEvent(categories(), {
    choices <- unique(categories()$var_2)
    updateSelectInput(inputId = "var_2", choices = choices) 
  })
  
  
  output$data <- DT::renderDataTable({
    req(input$var_2)
    
    table<-categories() %>% 
      filter(var_2 %in% input$var_2) %>% 
      select(var_2, var_3)%>%
      mutate(result="Yes") %>%
      tidyr::pivot_wider(names_from = var_2, values_from = result, values_fill = "No")%>%
      rename(Columns=var_3)
    
    table_1<-DT::datatable(table, filter= 'top',options = list(order=list(0,'asc'), dom='t', pageLength= 100, autoWidth = TRUE),rownames = FALSE)
    table_2<-DT::formatStyle(table_1, columns = NULL, fontWeight = styleEqual(c('No', 'Yes'), c('normal', 'bold')))
    
    
    
    
    
    
    return(table_2)
    
    
    
    
    
  })
}

shinyApp(ui, server)






And this is my input data:

var_1 var_2 var_3
red table1 column1
red table1 column4
red table1 column3
blue table2 column1
blue table2 column2
blue table2 column3
blue table2 column4
blue table3 column3
blue table3 column10
blue table3 column15
blue table3 column4
blue table3 column5
pink table4 column1
pink table4 column2
pink table4 column11
pink table4 column10
pink table4 column5
pink table4 column6
pink table4 column7
blue table5 column1
blue table5 column2
blue table5 column3
yellow table6 column9
yellow table6 column10
pink table7 column6
pink table7 column7
pink table7 column8

Thanks a lot.

CodePudding user response:

Instead of NULL for columns in formatStyle use the column names from table.

    table_2 <-
      DT::formatStyle(table_1,
                      columns = colnames(table),
                      fontWeight = styleEqual(c('No', 'Yes'), c('normal', 'bold')))

enter image description here

  • Related