R emmeans package output in Shiny

Nanco_NRC
Nanco_NRC used Ask the Experts™
on
Q: How to unite interaction columns from R emmeans package dynamically generated in R-Shiny?

I am building an R-Shiny app where I need to wrangle the output from the 'emmeans' package. However, in this interactive environment where many factors may be entered by the user, the single-tibble 'emmeans' output structure will vary with each run depending on the selections made. It could go from having only a single main effect to having multiple 3-way interactions (mixed with main effects and 2-way interactions) arranged in a wide format way.

For instance, assuming the user selects FctrA (with levels A and B) and FctrB (with levels C, D, and E), the interaction FctrA_FctrB will be automatically considered as well. When (~FctrA, ~FctrB, ~FctrA+FctrB) are submitted to 'emmeans', the output tibble is structured as follows:

- the leftmost side of the tibble contains FctrA results (levels, estimates, SE, df, CLs);
- the certermost block contains FctrB results;
- the rightmost side of the tibble contains the interaction results

So far so good except that FctrA levels columns is a single column, FctrB levels column is also a single column, but the interaction portion has its levels split into two columns, one with FctrA and one with FctrB.

The above issue impairs gathering, spreading, stacking of the separate blocks owing to the dimensional discrepancy.

My question is: How can I tell Shiny ('tidyr') to find those split interaction columns and concatenate them appropriately in a smart way (column names, number of interaction columns, overall output tibble structure varies according to input data and selections made)?

I am attaching an image to illustrate the process exemplified above:

- Figure 1 is what I currently have from 'emmeans';
- Figure 2 is what I need to do with any interactions I may have in the tibble (see column 13_14 related to my question above);
- Figure 3 is the final table I need which I believe I may get with the function 'coalesce' as long all blocks in my tibble have the same number of columns

Thank you all in advance for any advice you may provide.Emmeans_Question_ExpertsExchange.png
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Just wanted to share with the community that I have found a solution.

First, it required an extensive pre-work on headers names so that R could identify which ones needed just grouped and which ones needed united/combined/concatenated to have a table as shown in Figure 1 (new attachment).

Then, the key command line to unite those target columns and output something like the table shown in Figure 2 was:
lapply(split.default(df, names(df)), function(x) Reduce(`:`, x))

With some additional wrangling after that, it was possible to achieve a table like the one shown in Figure 3.

I hope this idea around how to prep columns names and combine that with the lapply statement above may be of help to someone else in the community.

Best Regards

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial