打印列表或data.frame时,r markdown无法保持行结构
我只需要显示一个列表或一个数据框,就像在R控制台中的一个降价latex文档中打印的那样,但我真的很难。我尝试过不同的解决方案,但没有成功 更具体地说,我有一个包含两列的dataframe,我想根据ifelse函数中定义的条件打印一列 数据结构:打印列表或data.frame时,r markdown无法保持行结构,r,r-markdown,R,R Markdown,我只需要显示一个列表或一个数据框,就像在R控制台中的一个降价latex文档中打印的那样,但我真的很难。我尝试过不同的解决方案,但没有成功 更具体地说,我有一个包含两列的dataframe,我想根据ifelse函数中定义的条件打印一列 数据结构: A tibble: 5 x 2 `open_pre_What do you think are the positive points o… `open_pre_What do you think are the negative points of
A tibble: 5 x 2
`open_pre_What do you think are the positive points o… `open_pre_What do you think are the negative points of this cour…
<chr> <chr>
1 Very sympathetic and good instructors, very motivated. It was too short and therefore could not unfold it's entirely po…
2 The instructor and TA both were really enthusiastic t… The contents were really compact and intensive - sometimes too m…
3 Provided interesting tools. Since the course provided interesting and useful tools, the time…
4 Very interactive and inspiring instructor and accommo… The time is too limited so that we had to be in a rush to cover …
5 I learnt a lot and got confident that I can do comput… The pace of the exercises on the PC was too fast and hard to fol…
如果列表为空,ifelse函数返回字符串“no comments”,否则返回数据帧的第一列
positive_comments = ifelse(identical(my_data[[1]], character(0))==TRUE,"No Comments",(my_data[1]))
print(positive_comments[[1]])
输出:
[1] Very sympathetic and good instructors, very motivated.
[2] The instructor and TA both were really enthusiastic to accomplish the goal of the module without making any stragglers. [3] Provided interesting tools.
[4] Very interactive and inspiring instructor and accommodating TA
[5] I learnt a lot and got confident that I can do computer simulations and modelling.
据我所知,这是因为print()命令insert line仅在需要时才中断,并且由于某些原因,在本例中不需要中断。请注意,直接从控制台运行命令将返回所需的结果
然后我尝试在数据帧中转换列表,但结果相同
positive_comments = ifelse(identical(my_data[[1]], character(0))==TRUE,"No Comments",(my_data[1]))
positive_comments = as.data.frame(positive)
colnames(positive_comments) <- ""
print(positive_comments)
print(positive_comments)
1非常有同情心和优秀的导师,非常积极。2讲师和助教都非常热情地完成了模块的目标,而没有掉队。3提供了有趣的工具。4非常互动、鼓舞人心的讲师和随和的助教5我学到了很多,并对自己能够进行计算机模拟和建模充满信心
print(positive_comments[1])
print(positive_comments[[1]])
[1] 非常富有同情心,是很好的导师,非常积极。
[2] 讲师和助教都非常热情地完成了模块的目标,而没有掉队。[3] 提供了有趣的工具。
[4] 非常互动、鼓舞人心的讲师和乐于助人的助教
[5] 我学到了很多,并对自己能做计算机模拟和建模充满信心
print(positive_comments[1])
print(positive_comments[[1]])
最后,我尝试删除markdown代码块中的results=“asis”
选项,但它仍然没有返回所需的结果。在这种情况下,允许print命令越过标记边界,导致输出不可读。使用打印.data.frame
(或其他打印方法)无法解决问题
所需输出:
[1] 非常富有同情心,是很好的导师,非常积极
[2] 讲师和助教都非常热情地完成了模块的目标,而没有掉队
[3] 提供了有趣的工具。
[4] 非常互动、鼓舞人心的讲师和乐于助人的助教
[5] 我学到了很多,并对自己能做计算机模拟和建模充满信心 所以第一个输出是正确的,除了它没有断线?打印(strwrap(正面注释[[1]],60])怎么样??它创建的行数比预期的要多。好,请尝试
cat(正面注释[[1]],填充=60)
。这不会给你编号;为此,应首先明确插入数字。通常,cat
的级别低于print
,并提供更多的控制。另一种方法是使用(例如)myhuxtable
package.cat在这种情况下完全抵消。不走运。我正在研究huxtablehuxtable不插入换行符,所以对于较长的注释,它只是超出了边界。这不是我要找的。我需要换行符