【问题标题】:Print Data Frame with long string variables打印带有长字符串变量的数据框
【发布时间】:2019-02-26 21:34:24
【问题描述】:

我有一个数据框,其中包含可能很长的调查问题的描述。我试图找出一种方法来整齐地打印它们。这是一个例子:

foo <- data.frame(v1 = 1:5, 
                  v2 = rep(c("This is a really long description of a survey question that gives a bunch of information about the question and can be very long blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah"), 5), 
                  v3 = 6:10)

我希望打印输出看起来像这样:

  v1   v2                                       v3
1  1   This is a really long description of     6
       a survey question that gives a bunch 
       of information about the question and
       can be very long blah blah blah blah 
       blah blah blah blah blah blah blah 
       blah blah blah blah blah blah blah
2  2   This is a really long description of     7
       a survey question that gives a bunch 
       of information about the question and
       can be very long blah blah blah blah 
       blah blah blah blah blah blah blah 
       blah blah blah blah blah blah blah
3  3   This is a really long description of     8
       a survey question that gives a bunch 
       of information about the question and
       can be very long blah blah blah blah 
       blah blah blah blah blah blah blah 
       blah blah blah blah blah blah blah
4  4   This is a really long description of     9
       a survey question that gives a bunch 
       of information about the question and
       can be very long blah blah blah blah 
       blah blah blah blah blah blah blah 
       blah blah blah blah blah blah blah
5  5   This is a really long description of     10
       a survey question that gives a bunch 
       of information about the question and
       can be very long blah blah blah blah 
       blah blah blah blah blah blah blah 
       blah blah blah blah blah blah blah

【问题讨论】:

  • 我还没有遇到在控制台本身中以这种方式打印的方法。但是有几种方法可以将 HTML 表格输出到执行此操作的查看器,例如flextable::flextable(foo)

标签: r dataframe rstudio


【解决方案1】:
flextable::flextable(foo, cwidth = c(0.5,7,0.5))

【讨论】:

  • 这是一个很好的解决方案。谢谢你。我没有意识到您可以以这种方式制作表格并将它们显示在查看器中。很高兴知道。
【解决方案2】:

您可以使用abbreviate()DT::datatable()。这样您就可以自动缩短文本并在点击时查看全文:

library(DT)
texts <- paste0(1:5, "is is a really long description of a survey question that gives a bunch of information about the question and can be very long blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah")

foo <- data.frame(v1 = 1:5, 
                  v2 = paste0('<a href="#" onclick="alert(\'', texts, '\');">', 
                              abbreviate(texts, named = FALSE), '</a>'), 
                  v3 = 6:10)
datatable(foo, escape = FALSE)

【讨论】:

  • 这是一个很好的解决方案,将来可能会帮助我做一些其他事情。但是,我接受了另一个答案,因为它不需要更改数据框。仅就我的一般知识而言,您是否知道是否可以使用此表格制作用户可以与之交互并更改数据框的表格?例如,您能否与数据框中相同观察值的可能值进行比较,然后选择两者之一并在数据框中对其进行修改?
  • 您可以更改数据表中的值,...它是可编辑的,...可以通过闪亮实现交互。如果您想更深入地讨论它,我认为一个新问题将是一个不错的选择。
  • 很高兴知道这一点。我的项目还没有到需要这个的地步。一旦我到达那里并有机会玩一些数据表并制定我的实际问题是什么,我会在需要时发布一个新问题。谢谢
猜你喜欢
  • 2016-01-02
  • 2012-01-10
  • 2021-08-05
  • 2018-09-30
  • 1970-01-01
  • 2016-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多