ui自动化定位 表格
Last week, I looked at estimating systematic differences between UK polling companies.
上周 ,我研究了估计英国投票公司之间的系统差异。
This article looks at remaking the house effects table in the gt package in R.
本文着眼于在R中的gt包中重新制作房屋效果表。
For regular reporting, we often need to make reproducible graphs and tables.
对于常规报告,我们经常需要制作可重现的图形和表格。
The outputs were the estimated constants in generalised additive models. That model has the components:
输出是广义加性模型中的估计常数。 该模型具有以下组成部分:
Company-weighted average: a constant representing the average reading. Each company has the same weight.
公司加权平均值:代表平均值的常数。 每个公司的权重相同。
Smooth function of time: this is a smooth function of the number of days since the last election. That smooth function must average to zero.
时间的平滑函数:这是自上次选举以来的天数的平滑函数。 该平滑函数必须平均为零。
The house effect: for each company, there is an change to fit to the ‘average company’ vote intention over time.
内部影响:随着时间的流逝,每家公司都会发生变化,以适应“普通公司”的投票意图。
Here, the models assume house effects are constant, and do not vary in time. House effects are relative to the industry average. Centrality does not mean accuracy. The table shows:
在此,模型假设房屋影响是恒定的,并且不会随时间变化。 房屋影响与行业平均值有关。 中心性并不意味着准确性。 该表显示:
The goal is to show, for each company: count, plus ‘house effect’ estimates and intervals by party.
目标是为每个公司显示:数量,加上“房屋效应”估计和按方划分的间隔。
I made this table in LaTeX. It has a professional look, but there are problems:
我在LaTeX中制作了这张桌子。 它具有专业外观,但存在一些问题:
No caption: The caption is necessary, but it is not on the table itself.
无标题:标题是必需的,但它不在表格本身上。
Alignment: The columns are left-aligned. The estimates and intervals are all over the place due to different lengths.
对齐:列左对齐。 由于长度不同,估计值和时间间隔遍布各地。
Manual: If I wanted to look at a different period, I would need to change values. That could also lead to errors in publication.
手册:如果我想换一个时期,则需要更改值。 这也可能导致出版错误。
Numbers should be right-aligned. The House of Commons Library guide says:
数字应右对齐。 下议院图书馆指南说:
We are used to seeing numbers in this way, think back to hundred, tens and units at school. It means all digits aligned each other are of the same order of magnitude.
我们习惯于以这种方式看到数字,回想起学校中的百,十和单位。 这意味着彼此对齐的所有数字都具有相同的数量级。
For this task, I use the gt package in R. There are other packages, such as kable and formattable.
对于这个任务,我用的是GT 包在R.有其他软件包 ,如kable和formattable。
For the gt package, the goal is to arrange a table in R. With modifications, that table becomes an object. The output will be a rendered HTML table.
对于gt包,目标是在R中安排一个表。经过修改,该表将成为对象。 输出将是呈现HTML表。
R Studio/gt) R Studio / gt )First, I take the separate model outputs and the poll count, and join:
首先,我采用单独的模型输出和轮询计数,然后加入:
house_effects_tbl <- con_exc_table %>% inner_join(lab_exc_table, by = "term", suffix = c("_con", "_lab")) %>% inner_join(ldem_exc_table, by = "term") %>% inner_join(snp_exc_table, by = "term", suffix = c("_ldem", "_snp")) %>% inner_join(grn_exc_table, by = "term") %>% inner_join(con_lead_exc_table, by = "term", suffix = c("_grn", "_lead")) %>% dplyr::mutate(term = str_remove(term, "company")) %>% dplyr::rename(company = term) %>% inner_join(uk_poll_count_df, by = "company")I write dynamic labels for the output table, so it shows relevant dates:
我为输出表编写了动态标签,因此它显示了相关的日期:
house_effects_source_note <- paste0("Data: Wikipedia; British Polling Council member company archives [with fieldwork ending ", max(uk_poll_tidy_df$fw_end), "].")house_effects_subtitle <- paste0("Estimates are in percentage points relative to industry average, with 95% confidence intervals. Fieldwork between: ", min(uk_poll_tidy_df$fw_start), " and ", max(uk_poll_tidy_df$fw_end), ".")There are 20 columns in this R table. Alongside the company name, there is a count of polls for each company. There are three columns each for five parties plus the Conservative lead.
该R表中有20列。 在公司名称旁边,每个公司都有一定数量的民意调查。 共有三列,分别代表五个政党和保守党领袖。
The three columns in each set should merge to give the estimate and confidence interval. We can do that in the gt package:
每组中的三列应合并以给出估计值和置信区间。 我们可以在gt包中做到这一点:
house_effects_gt <- house_effects_tbl %>% gt() %>% cols_merge(columns = vars(est_con, low_con, upp_con), hide_columns = vars(low_con, upp_con), pattern = "<b>{1}</b><br>({2}, {3})") %>% cols_merge(columns = vars(est_lab, low_lab, upp_lab), hide_columns = vars(low_lab, upp_lab), pattern = "<b>{1}</b><br>({2}, {3})") %>% cols_merge(columns = vars(est_ldem, low_ldem, upp_ldem), hide_columns = vars(low_ldem, upp_ldem), pattern = "<b>{1}</b><br>({2}, {3})") %>% cols_merge(columns = vars(est_snp, low_snp, upp_snp), hide_columns = vars(low_snp, upp_snp), pattern = "<b>{1}</b><br>({2}, {3})") %>% cols_merge(columns = vars(est_grn, low_grn, upp_grn), hide_columns = vars(low_grn, upp_grn), pattern = "<b>{1}</b><br>({2}, {3})") %>% cols_merge(columns = vars(est_lead, low_lead, upp_lead), hide_columns = vars(low_lead, upp_lead), pattern = "<b>{1}</b><br>({2}, {3})") %>%Given the order in the underlying table, we need to reorder the columns:
给定基础表中的顺序,我们需要重新排列各列:
cols_move_to_start(columns = vars(company, count)) %>%For readability, we can then replace the column labels in the output:
为了提高可读性,我们可以替换输出中的列标签:
cols_label(company = "Polling Company", count = "Count", est_con = "CON", est_lab = "LAB", est_ldem = "LDEM", est_snp = "SNP", est_grn = "GRN", est_lead = "CON Lead") %>%Finally, we add more labels, striping, and fix alignment of the count column:
最后,我们添加更多标签,条带化并修复count列的对齐方式:
tab_header(title = md("**Estimated House Effects in UK/GB Vote Intention Polls.**"), subtitle = house_effects_subtitle) %>% tab_source_note(source_note = house_effects_source_note) %>% opt_row_striping(row_striping = TRUE) %>% cols_align(align = "right", columns = vars(count))The output table looks like:
输出表如下所示:
This table is effective and repeatable. The table highlights central estimates, but gives intervals showing uncertainty too.
该表是有效且可重复的。 该表突出显示了中心估计,但也给出了显示不确定性的间隔。
Other people can run the same code and make it again. Extra columns could include survey modes and target population.
其他人可以运行相同的代码,然后重新编写。 额外的列可能包括调查模式和目标人群。
The list of published polls were from Wikipedia and polling company archives. I used Prof Simon Wood’s mgcv package for generalised additive models. The data file and R code are on GitHub and R Pubs.
已发布的民意调查清单来自Wikipedia和民意调查公司档案 。 我将Simon Wood教授的mgcv 软件包用于广义加性模型 。 数据文件和R代码位于GitHub和R Pubs上 。
翻译自: https://medium.com/swlh/automating-tables-in-r-ea793c987d03
ui自动化定位 表格
相关资源:Layer UI表格列日期格式化及取消自动填充日期的实现方法