基于多列的Formattable [英] Formattable based on multiple columns
问题描述
我使用包 formattable
生成一个格式化的表。我找到了一个很好的资源用R格式化表格。但是这里的箭头格式化的例子只是基于特定的列。我的要求是:假设我来自不同的城市,列A和B,其中A具有树数和污染两个等级的因子,而B是这些同比变化的百分比。因此,如果列A值增加为树数(因为它是正的),所以我想用正绿色箭头格式化B列,如果反之则减少,反之则是红色。
因此,从链接本身(例如: Markus Gesmann )的例子,即使改变为<0,
library(formattable)
DF < - data.frame (Ticker = c(,,,IBM,AAPL,MSFT),
名称= c(道琼斯,标普500 ,
IBM,Apple,Microsoft),
Value =记帐(c(15988.08,1880.33,NA,$ b $ 130.00,97.05,50.99)),
更改=百分比(c(-0.0239,-0.0216,0.021,
-0.0219,-0.0248,-0.0399)))
DF
##代码名称值更改
## 1道琼斯15,988.08 -2.39%
## 2 S& 500 500 1,880.33 -2.16%
## 3技术NA 2.10%
## 4 IBM IBM 130.00 -2.19%
## 5 AAPL Apple 97.05 -2.48%
## 6 MSFT Microsoft 50.99 -3.99%
formattable(DF,list(
Name = formatter(
span,
style = x〜ifelse(x ==Technology,
样式(font.weight =bold),NA)),
Value = color_tile(white,orange),
Change = formatter(
span,
style = x〜style(color = ifelse(x < 0,red,green)),
x〜icontext(ifelse(x <0,arrow-down,arrow-up),x)))
)
基于
:
formattable(DF,list(
Name = formatter(
span,
style = x〜ifelse(x ==Technology,
style(font.weight =bold),NA)),
Value = color_tile(white ,orange),
Change = formatter(
span,
style =〜style(color = ifelse(Change< 0&Ticker! (改变< 0& Name!=IBM,arrow-down,arrow-up),Change)))
$ b显然, x〜
公式风格限制您在右侧使用唯一的变量本身。你必须切换到〜
,然后在右边显示列名(而不是 x
)。
I am using the package formattable
to generate a formatted table. I found a nice resource Formatting tables in R. But here the example of arrow formatting is based only on the specific column.
My requirement is: Say I have from different cities, column A and B, where A has factors with two levels 'Number of Trees' and 'Pollution', while B is the percentage change of these YoY. So I want to format the Column B with a positive green arrow if there has an increase for Column A values being 'Number of Trees' (since it is positive) and red if there is a decrease while for pollution the other way round.
So taking the example (credit: Markus Gesmann) from the link itself, say just for IBM even if the change is <0, I want to show a positive green arrow against it.
library(formattable)
DF <- data.frame(Ticker=c("", "", "", "IBM", "AAPL", "MSFT"),
Name=c("Dow Jones", "S&P 500", "Technology",
"IBM", "Apple", "Microsoft"),
Value=accounting(c(15988.08, 1880.33, NA,
130.00, 97.05, 50.99)),
Change=percent(c(-0.0239, -0.0216, 0.021,
-0.0219, -0.0248, -0.0399)))
DF
## Ticker Name Value Change
## 1 Dow Jones 15,988.08 -2.39%
## 2 S&P 500 1,880.33 -2.16%
## 3 Technology NA 2.10%
## 4 IBM IBM 130.00 -2.19%
## 5 AAPL Apple 97.05 -2.48%
## 6 MSFT Microsoft 50.99 -3.99%
formattable(DF, list(
Name=formatter(
"span",
style = x ~ ifelse(x == "Technology",
style(font.weight = "bold"), NA)),
Value = color_tile("white", "orange"),
Change = formatter(
"span",
style = x ~ style(color = ifelse(x < 0 , "red", "green")),
x ~ icontext(ifelse(x < 0, "arrow-down", "arrow-up"), x)))
)
解决方案 Based on the doc at ?formatter
:
formattable(DF, list(
Name=formatter(
"span",
style = x ~ ifelse(x == "Technology",
style(font.weight = "bold"), NA)),
Value = color_tile("white", "orange"),
Change = formatter(
"span",
style = ~ style(color = ifelse(Change < 0 & Ticker != "IBM", "red", "green")),
~ icontext(ifelse(Change < 0 & Name != "IBM", "arrow-down", "arrow-up"), Change)))
)
Apparently, the x ~
formula style restricts you to use the only the variable itself on the right-hand side. You have to switch to ~
and then write the column name explicitly on the right-hand side (instead of as x
).
这篇关于基于多列的Formattable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!