对齐html RMarkdown表中的单元格(电缆)

时间:2018-11-22 05:58:56

标签: r r-markdown kable

我正在尝试对齐html Rmarkdown文档中包含多行文本的表(电缆)的内容。

呈现默认表时,文本左对齐,但水平居中对齐。见下文:

enter image description here

我希望的文本在左边对齐(不变),但在顶部对齐。是否可以在Rmarkdown中使用kable?

下面的代码重新创建了上表:

```{r, cache=TRUE, echo=FALSE}
column_1 <- c( "Lorem ipsum dolor sit amet, consectetur adipiscing 
elit. Vivamus dolor risus, aliquet at dolor et, tincidunt egestas 
urna.")

column_2 <- c("Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Vivamus dolor risus, aliquet at dolor et, tincidunt egestas urna. Nam 
nec elit vel eros accumsan vulputate. Donec et congue sapien. Morbi 
condimentum fringilla lobortis. Cras eget justo non turpis elementum 
facilisis. Fusce dignissim orci id pretium sagittis. Pellentesque 
dignissim nisi sapien, sit amet maximus nisl ullamcorper ac. Donec elit 
ex, hendrerit sed leo convallis, efficitur bibendum ligula. 
Pellentesque sagittis arcu et condimentum viverra. Mauris at lectus at 
massa ullamcorper ullamcorper. Aliquam quis leo ac eros cursus mattis. 
Praesent pellentesque nunc et diam lacinia, quis cursus purus lacinia. 
Aenean ipsum libero, bibendum non ante 
vel, vehicula consectetur ex. Fusce lobortis odio nec urna dignissim, 
sed elementum ipsum sagittis. Sed mollis, magna in blandit imperdiet, 
dolor quam posuere quam, id iaculis nisl diam sed lorem.")

column_3 <- c("Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Vivamus dolor risus, aliquet at dolor et, tincidunt egestas urna. Nam 
nec elit vel eros accumsan vulputate. Donec et congue sapien. Morbi 
condimentum fringilla lobortis. Cras eget justo non turpis elementum 
facilisis. Fusce dignissim orci id pretium sagittis. Pellentesque 
dignissim nisi sapien, sit amet maximus nisl ullamcorper ac. Donec elit 
ex, hendrerit sed leo convallis, efficitur bibendum ligula. 
Pellentesque sagittis arcu et condimentum viverra. Mauris at lectus at 
massa ullamcorper ullamcorper. Aliquam quis leo ac eros cursus 
mattis.")

test_table <- data_frame(column_1, column_2, column_3) %>%
  kable(caption = "Test table", align = "l") %>% 
  kable_styling(full_width = T) %>%
  column_spec(1, width = "30em") %>%
  column_spec(2, width = "40em") %>%
  column_spec(3, width = "30em")
```

question似乎试图解决类似的问题,并且该解决方案涉及使用Latex。这里不是一个选择...

1 个答案:

答案 0 :(得分:0)

使用extra-css参数:

data_frame(column_1, column_2, column_3) %>%
  kable(caption = "Test table", align = "l", format = "html") %>% 
  kable_styling(full_width = T) %>%
  column_spec(1, width = "30em", extra_css = "vertical-align:top;") %>%
  column_spec(2, width = "40em", extra_css = "vertical-align:top;") %>%
  column_spec(3, width = "30em", extra_css = "vertical-align:top;")

enter image description here