将长标题转换为多列HTML表

时间:2019-02-21 03:39:27

标签: r

我有一个两行的小标题,其中有很多行,我想在HTML表中显示小标题的内容,同时将小标题的内容流到多列。这是我正在使用的典型数据示例。

structure(list(scores = 328:360, points = c(1.44976648822324, 
2.39850620178477, 3.54432361637504, 4.87641377160755, 6.38641933285773, 
8.06758106817846, 9.91425882252425, 11.9216360940354, 14.0855256867808, 
16.4022354393545, 18.8684718158155, 21.4812684932283, 24.2379320869751, 
27.136, 30.1732070790481, 33.3474588150326, 36.6568095024477, 
40.0994442217347, 43.6736638137649, 47.3778722279125, 51.2105657758874, 
55.1703239324027, 59.2558014037444, 63.46572124494, 67.7988688512606, 
72.2540866842331, 76.8302696189673, 81.5263608204015, 86.3413480724772, 
91.2742604972992, 96.3241656118041, 101.490166677918, 106.771400309065
)), row.names = c(NA, -33L), class = c("tbl_df", "tbl", "data.frame"
))

我想在HTML表中显示这些数据对,如下所示:

| Score | Points | Score | Points | Score | Points | Score | Points |
|------:|-------:|------:|-------:|------:|-------:|------:|-------:|
|   328 |    1.4 |   337 |   16.4 |   346 |   43.7 |   355 |   81.5 |
|   329 |    2.4 |   338 |   18.9 |   347 |   47.4 |   356 |   86.3 |
|   330 |    3.5 |   339 |   21.5 |   348 |   51.2 |   357 |   91.3 |
|   331 |    4.9 |   340 |   24.2 |   349 |   55.2 |   358 |   96.3 |
|   332 |    6.4 |   341 |   27.1 |   350 |   59.3 |   359 |  101.5 |
|   333 |    8.1 |   342 |   30.2 |   351 |   63.5 |   360 |  106.8 |
|   334 |    9.9 |   343 |   33.3 |   352 |   67.8 |       |        |
|   335 |   11.9 |   344 |   36.7 |   353 |   72.3 |       |        |
|   336 |   14.1 |   345 |   40.1 |   354 |   76.8 |       |        |

我希望有一个解决方案,无论原始小标题中有多少行,它都将生成四双列布局。

我首先将小标题切成四个部分,但由于第四个元素没有前三个元素那么多而陷入困境。

对实现此目标的方法有何建议?

1 个答案:

答案 0 :(得分:0)

您可以编写一个小函数,它将接收数据和所需的列数。默认值为4列

reshaping = function(dat, cols = 4){
  n = nrow(dat)
  m = ceiling(n/cols)
  time=rep(1:cols, each = m, len = n)
  id = rep(1:m, times = cols, len = n)
  reshape(cbind(id, time, dat), idvar = 'id', dir='wide')[-1]
}

reshaping(dat)
  scores.1  points.1 scores.2 points.2 scores.3 points.3 scores.4  points.4
1      328  1.449766      337 16.40224      346 43.67366      355  81.52636
2      329  2.398506      338 18.86847      347 47.37787      356  86.34135
3      330  3.544324      339 21.48127      348 51.21057      357  91.27426
4      331  4.876414      340 24.23793      349 55.17032      358  96.32417
5      332  6.386419      341 27.13600      350 59.25580      359 101.49017
6      333  8.067581      342 30.17321      351 63.46572      360 106.77140
7      334  9.914259      343 33.34746      352 67.79887       NA        NA
8      335 11.921636      344 36.65681      353 72.25409       NA        NA
9      336 14.085526      345 40.09944      354 76.83027       NA        NA

reshaping(dat,8)
  scores.1 points.1 scores.2  points.2 scores.3 points.3 scores.4 points.4 scores.5 points.5 scores.6 points.6 scores.7  points.7
1      328 1.449766      333  8.067581      338 18.86847      343 33.34746      348 51.21057      353 72.25409      358  96.32417
2      329 2.398506      334  9.914259      339 21.48127      344 36.65681      349 55.17032      354 76.83027      359 101.49017
3      330 3.544324      335 11.921636      340 24.23793      345 40.09944      350 59.25580      355 81.52636      360 106.77140
4      331 4.876414      336 14.085526      341 27.13600      346 43.67366      351 63.46572      356 86.34135       NA        NA
5      332 6.386419      337 16.402235      342 30.17321      347 47.37787      352 67.79887      357 91.27426       NA        NA