【问题标题】:Shiny R- Why aren't formatStyle() changes visible in my output data table?Shiny R- 为什么我的输出数据表中不显示 formatStyle() 更改?
【发布时间】:2021-11-26 01:09:17
【问题描述】:

我正在尝试使用 formatStyle() 在 Shiny 中更改输出表的格式和样式,但更改不可见。可能有什么问题?例如,我希望“是”为粗体,但最终结果未显示这些更改。 这是我的最终输出:

这是我的代码:

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)






这是我的输入数据:

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

非常感谢。

【问题讨论】:

    标签: r shiny datatable


    【解决方案1】:

    formatStyle 中的 columns 使用来自 table 的列名,而不是 NULL

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

    【讨论】:

      猜你喜欢
      • 2018-02-27
      • 1970-01-01
      • 2018-09-30
      • 2021-12-05
      • 2019-07-28
      • 1970-01-01
      • 2020-04-28
      • 2018-07-08
      • 2016-05-10
      相关资源
      最近更新 更多