简体   繁体   English

在 R 中的 Function 中使用 Flextable

[英]Using Flextable in a Function in R

I am trying to create a function in R markdown for conditional formatting while using flextable package.我正在尝试在 R markdown 中创建一个 function 用于条件格式,同时使用 flextable ZEFE90A8E604A7C84A7D86 I am trying to color format rows based on the condition which compares two columns.我正在尝试根据比较两列的条件对行进行颜色格式化。


library(flextable)
library(dplyr)
cndnl_form <- function(data,mv1,ov1)

{
ft <- flextable({{data}})

ft <- color(ft, i = ~ ({{mv1}}- {{ov1}}/ abs({{mv1}}) > 0.25),
j = {{ov1}},
color="blue")

}

cndnl_form(df,"ft$3","ft$10")


but i am getting an error Error during wrapup: object 'mv1' not found但我在总结过程中收到错误错误:未找到 object 'mv1'

Any idea, what should i change?任何想法,我应该改变什么?

I'm not sure whether tidy eval will work in this context.我不确定 tidy eval 在这种情况下是否有效。

However, as flextable allows for formula specifications you can set up the condition as a formula-string which can then be converted to a formula.但是,由于flextable允许公式规范,您可以将条件设置为公式字符串,然后可以将其转换为公式。

Using mtcars as example data with the simple condition mv1 > 20 try this:使用mtcars作为简单条件mv1 > 20的示例数据,试试这个:

library(flextable)

cndnl_form <- function(data, mv1, ov1)
{
  ft <- flextable({{data}})

  #cond <- paste("~", "(", mv1, "-", ov1, ")", "/", "abs(", mv1, ") > 0.25")

  cond <- paste("~", mv1, "> 20")
  color(ft, i = as.formula(cond), j = as.formula(paste("~", ov1)), color="blue")
}

ft <- cndnl_form(head(mtcars), "mpg", "gear")

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM