You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R中居中显示R代码块的输出内容?

解决R代码输出居中的办法
  • R Markdown场景

    1. 全局设置:在文档开头添加CSS样式,让所有代码输出自动居中:
      <style>
      .output {
        text-align: center;
      }
      </style>
      
    2. 单独代码块设置:给指定代码块添加results='asis'选项,用HTML标签包裹输出:
      ```{r, results='asis'}
      cat("<div style='text-align: center;'>")
      # 这里放你的代码,示例:
      print(head(mtcars))
      cat("</div>")
      
  • 普通R脚本输出到HTML场景
    用cat()函数结合HTML居中标签包裹输出内容,导出为HTML后即可看到居中效果:

    cat("<div style='text-align: center;'>")
    # 示例输出
    summary(iris)
    cat("</div>")
    
  • RStudio原生查看器
    RStudio自带的查看器没有直接让输出居中的设置,建议将输出转为HTML格式后用上述方法展示。

内容的提问来源于stack exchange,提问作者Beagle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 00:05:03