如何在Reactable中合并一些表格单元格

lo8azlld  于 2023-07-31  发布在  React
关注(0)|答案(1)|浏览(159)

我正在尝试创建一个reactable,它将显示大量分类变量的统计数据。该表应显示以下列:变量名、变量类别、类别频率、百分比、有效值数和缺失值数。我想合并一些表格单元格,以便值不重复。我想合并:变量名、有效计数和缺失计数。我可以用java脚本来做这个,但我不懂这种语言。
这是我正在编写的代码:

library(dplyr)
library(reachable)


calculate_summary <- function(x) {
  data_summary <- data.frame(Category = x) %>%
    count(Category, name = "Frequency") %>%
    mutate(Percent = (Frequency / sum(Frequency)) * 100,
           Valid_Count = sum(!is.na(Category)),
           Missing_Count = sum(is.na(Category))) %>%
    select(Category, Frequency, Percent, Valid_Count, Missing_Count)
  
  return(data_summary)
}


# Function to create the reactable
create_reactable <- function(data) {
  data_summary <- lapply(data, calculate_summary)
  combined_data <- do.call(rbind, data_summary)
  reactable(
    combined_data,striped = TRUE, resizable = TRUE, wrap = FALSE,
    style = list(
      fontFamily = "Helvetica"),
    
    columns = list(
      Category = colDef(name = "Category"),
      Frequency = colDef(name = "Count"),
      Percent = colDef(name = "Percent %"),
      Valid_Count = colDef(name = "Valid Count"),
      Missing_Count = colDef(name = "Missing Count")
    ),
    showPagination = TRUE
  )
}

# Example data
set.seed(123)
data <- data.frame(
  Variable = c("A", "B", "C", "D", "E", NA),
  Proba = c("A", "B", "C", "D", "E", NA)
  )
  

# Create the reactable using the function
reactable_data <- create_reactable(data)

# Display the reactable
reactable_data

字符串
通过该代码,我得到了以下表格设计:
x1c 0d1x的数据
但是,我想合并一些单元格。



谢谢!

w6lpcovy

w6lpcovy1#

这里有一种可能性。


的数据
你可以通过使用以下命令来获得:

library(dplyr)
library(reactable)

calculate_summary <- function(x) {
    data_summary <- data.frame(Category = x) %>%
        count(Category, name = "Frequency") %>%
        mutate(Percent = (Frequency / sum(Frequency)) * 100,
               Valid_Count = ifelse(Category == "C", sum(!is.na(Category)), NA),
               Missing_Count = ifelse(Category == "C", sum(is.na(Category)), NA)) %>%
        select(Category, Frequency, Percent, Valid_Count, Missing_Count) 
    
    return(data_summary)
}

# Function to create the reactable
create_reactable <- function(data) {
    data_summary <- lapply(data, calculate_summary)
    combined_data <- do.call(rbind, data_summary) |> 
        tibble::rownames_to_column("Name") |> 
        mutate(Name = ifelse(Category != "C", "", Name)) |> 
        mutate(Name = gsub("\\..*","",Name)) 
    
    reactable(
        combined_data,striped = TRUE, resizable = TRUE, wrap = FALSE,
        style = list(
            fontFamily = "Helvetica"),
        
        columns = list(
            Category = colDef(name = "Category"),
            Frequency = colDef(name = "Count"),
            Percent = colDef(name = "Percent %"),
            Valid_Count = colDef(name = "Valid Count"),
            Missing_Count = colDef(name = "Missing Count")
        ),
        showPagination = TRUE
    )
}

# Example data
set.seed(123)
data <- data.frame(
    Variable = c("A", "B", "C", "D", "E", NA),
    Proba = c("A", "B", "C", "D", "E", NA)
)

# Create the reactable using the function
reactable_data <- create_reactable(data)

# Display the reactable
reactable_data

字符串

相关问题