Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:excel文件的条件格式_R_Excel_Conditional Formatting_Openxlsx - Fatal编程技术网

R:excel文件的条件格式

R:excel文件的条件格式,r,excel,conditional-formatting,openxlsx,R,Excel,Conditional Formatting,Openxlsx,我试图根据单独excel文件中列的匹配来突出显示excel文件的行。基本上,我想突出显示file1中的一行,如果该行中的单元格与file2中的单元格匹配 我看到R包“conditionalFormatting”有一些这样的功能,但我不知道如何使用它 我认为伪代码应该是这样的: file1 <- read_excel("file1") file2 <- read_excel("file2") conditionalFormatting(file1, sheet = 1, cols =

我试图根据单独excel文件中列的匹配来突出显示excel文件的行。基本上,我想突出显示file1中的一行,如果该行中的单元格与file2中的单元格匹配

我看到R包“conditionalFormatting”有一些这样的功能,但我不知道如何使用它

我认为伪代码应该是这样的:

file1 <- read_excel("file1")
file2 <- read_excel("file2")

conditionalFormatting(file1, sheet = 1, cols = 1:end, rows = 1:22, 
rule = "number in file1 is found in a specific column of file 2")

file1函数
conditionalFormatting()
将活动的条件格式嵌入excel文档,但可能比一次性突出显示更复杂。我建议将每个文件加载到数据框中,确定哪些行包含匹配的单元格,创建突出显示样式(黄色背景),将文件加载为工作簿对象,将适当的行设置为突出显示样式,并保存更新的工作簿对象

以下函数用于确定哪些行具有匹配项。
magrittr
包提供了
%>%
管道和
数据。table
包提供了
transpose()
函数

find_matched_rows <- function(df1, df2) {
  require(magrittr)
  require(data.table)

  # the dataframe object treats each column as a list making it much easier and
  # faster to search via column than row. Transpose the original file1 dataframe
  # to treat the rows as columns.
  df1_transposed <- data.table::transpose(df1)

  # assuming that the location of the match in the second file is irrelevant,
  # unlist the file2 dataframe so that each value in file1 can be searched in a
  # vector
  df2_as_vector <- unlist(df2)

  # determine which columns contain a match. If one or more matches are found,
  # attribute the row as 'TRUE' in the output vector to be used to subset the 
  # row numbers
  match_map <- lapply(df1_transposed,FUN = `%in%`, df2_as_vector) %>%
    as.data.frame(stringsAsFactors = FALSE) %>%
    sapply(function(x) sum(x) > 0)

  # make a vector of row numbers using the logical match_map vector to subset
  matched_rows <- seq(1:nrow(df1))[match_map]
  return(matched_rows)
}

find_matched_rows这是否需要随着新数据添加到工作簿中而自动更新,或者您正在尝试使用一个脚本来读取两个文件并相应地更新其中一个?不,它不需要更新。我只需要一次精彩表演。谢谢非常感谢,这是非常全面的。
# used to ensure that the correct rows are highlighted. the dataframe does not
# include the header as an independent row unlike excel.
file1_header_row <- 1
file2_header_row <- 1

tst_df1 <- openxlsx::read.xlsx("./file1.xlsx",
                               startRow = file1_header_row)
tst_df2 <- openxlsx::read.xlsx("./file2.xlsx",
                               startRow = file2_header_row)

#example data for testing
tst_df1 <- data.frame(fname = c("John", "Bob", "Bill"), 
                  lname = c("Smith", "Johnson", "Samson"), 
                  wage = c(10, 15.23, 137.38), 
                  stringsAsFactors = FALSE)
tst_df2 <- data.frame(a = c(10, 34, 284.2), 
                   b = c("Billy", "Bill", "Billy-Bob"), 
                   c = c("Samson", "Johansson", NA), 
                   stringsAsFactors = FALSE)

df_matched_rows <- find_matched_rows(tst_df1, tst_df2)

# any color found in colours() can be used here or hex color beginning with "#"
highlight_style <- openxlsx::createStyle(fgFill = "yellow") 

file1_wb <- openxlsx::loadWorkbook(file = "./file1.xlsx")
openxlsx::addStyle(wb = file1_wb, 
                   sheet = 1, 
                   style = highlight_style,
                   rows = file1_header_row + df_matched_rows,
                   cols = 1:ncol(tst_df1),
                   stack = TRUE,
                   gridExpand = TRUE)
openxlsx::saveWorkbook(wb = file1_wb, 
                       file = "./file1.xlsx",
                       overwrite = TRUE)