In R Shiny, I have a data table in wide form, let's all this Representation A of the data:
| Rank | Person | School |
| ------------ | ------------ | ------------ |
| 1 | Sean | Boston |
| 2 | Alicia | |
I'm using DT::dataTableOutput
to present this table. I want to instead present this table in long form (Rank identifies the observations):
| Rank | Variable | Value of Variable |
| ------------ | ------------ | ------------ |
| 1 | Person | Sean |
| 1 | School | Boston |
| 2 | Person | Alicia |
| 2 | School | |
I will then also style this table slightly differently, I will:
- Only print the first occurrence of a value of rank
The table then becomes:
| Rank | Variable | Value of Variable |
| ------------ | ------------ | ------------ |
| 1 | Person | Sean |
| | School | Boston |
| 2 | Person | Alicia |
| | School | |
I will also:
- Drop empty rows
So the final table, which we will call Representation B of the data, becomes:
| Rank | Variable | Value of Variable |
| ------------ | ------------ | ------------ |
| 1 | Person | Sean |
| | School | Boston |
| 2 | Person | Alicia |
When presenting the table in the format B, I still want to keep as much of the functionality as possible that DT::dataTableOutput
supplies for form A, e.g. the ability to search and sort (without separating rows belonging together) etc. Here, by sorting I don't mean the order variables are presented in within a given rank, but the order the "rank groups" are presented. For example, sorting on Person should yield the following since Alicia comes before Sean lexicographically:
| Rank | Variable | Value of Variable |
| ------------ | ------------ | ------------ |
| 2 | Person | Alicia |
| 1 | Person | Sean |
| | School | Boston |
What do you think is the easiest way to implement this?
Currently, I consider two different ways. In both plans, instead of having the standard sorting buttons provided by DT::dataTableOutput
, I will link e.g. a radio button which will allow the user to choose which variable the table should be sorted on (i.e. Rank, Person, or School).
My first option for implementation: When the radio button is pressed, I will (without the user seeing it) transform the table to representation A, sort it there, then transform it to the properly sorted version of representation B.
My second option for implementation: I could to representation B "attach" representation A (without showing the latter to the user) so that each row of the underlying data contains the full information for that rank:
| Rank | Variable | Value of Variable | Rank_Long | Person_Long | School_Long |
| ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
| 1 | Person | Sean | 1 | Sean | Boston |
| | School | Boston | 1 | Sean | Boston |
| 2 | Person | Alicia | 2 | Alicia | |
If I want to obtain the lexicographically sorted B representation, I order the table above by (Person_Long, Rank_Long, Variable). The extra Variable in the ordering is added to get the rows presented to the user (Rank and Variable) in the right order (Person above School).
In practice, I will have around five to ten variables rather than only two (and each of these ten should be allowed to sort on), and I will be running Shiny Server on an AWS server which will be reached through an iframe on a website.
Pros and cons of the first and second options for implementation:
- First option: Fast if the
dcast
andmelt
functions are fast enough and ifmelt
can be set to preserve the sorting when transforming to long. Should provide faster initial load time of the table for the user since the table won't have as many columns as the second option. - Second option: No reshape needed, but more data sent to the user at initial load.
Which of my two options do you think is the best, and do you have suggestions about other implementations I haven't considered?
1条答案
按热度按时间eaf3rand1#
我使用
paste
连接了感兴趣的列,并正确放置了<br>
标记作为换行符,然后使用DT::datatable(..., escape = FALSE)
可以获得所需格式的表。我定义了一个
sliderInput
(在表外),我将其输入设置为触发排序事件,当滑块获得新的输入时,表将重新加载(从而重置搜索),我将尝试修复这个问题(使用一些过滤器或代理解决方案,或者希望我找到一个简单的方法)。