12

我正在尝试将我所有的数据处理移动到 Rmarkdown,而不是 SPSS+Excel,但无法弄清楚如何创建带有附加图的表。

理想的桌子

在 Excel 中,这可以通过 Sparklines 功能来完成,或者像我一样,简单地创建一个图表并非常准确地放置它。

上面的表格是使用 Tables 包(和 Pander)中的表格函数创建的。并粘贴到 Excel 中以创建图表(以及图表的标题)。

library(tables)
library(pander)
pander( #convert table to rmarkdown table
  tabular( 
  (Species + 1) ~ (n=1) + Format(digits=2) *
         (Sepal.Length + Sepal.Width) * (mean + sd), 
  data = iris),
  caption = "Table 1. Iris") #Heading for table

有没有人创造过这样的东西?也许是 gridExtra 包的解决方法,尽管我怀疑该包是否可以将表格和图表匹配在一起。


编辑 - 到目前为止我的解决方案。
HTML 已完成。pdf的一半。对于doc,我认为这是不可能的(复制粘贴到Excel)。

HTML 表
首先,R 知道我是在渲染 html、pdf 还是 doc。out_type 取值:“html”、“pdf”和“docx”。我可以在 if 语句中使用这个对象。

out_type <- knitr::opts_knit$get("rmarkdown.pandoc.to")

现在的酒吧:

    if(out_type == "html"){ #if output is html then:
        my_table$Mean_Sepal_Length <- paste0( #I have a table saved as a dataframe. I add a column to it called Mean_Sepal_Length
"<span style=' width: 100%; display: flex;'><span style='display: inline-block; border-radius: 3px; padding-right: 0; background-color: #00457C; width:", #create the bar
        MEAN_SEPAL_L_OBJECT, #set the with of the bar. I have an object with these proportions
"%; margin-right: 5px;'>&nbsp;</span><span>", MEAN_SEPAL_L_VARIABLE, "%</span></span>") #finally add the value behind the bar.
    }

看起来像这样

也可以有两列。

另一种可能

在这里,我再次有一个比例表,我有两个具有男性和女性比例的对象。

    my_table$male_female <- paste0(
"<span style=' width: 100%; display: flex;'><span>", MALE_BAR, #the proportion of males
 "%</span><span style='display: inline-block;border-top-left-radius: 3px; border-bottom-left-radius:3px; padding: 0; background-color: #00457C; width:", MALE_BAR, #width of the bar for males
 "%; margin-left: 5px;'></span><span style='display: inline-block; border-top-right-radius: 3px; border-bottom-right-radius:3px; padding:0; background-color: #AC1A2F; width:",
 FEMALE_BAR, #width of bar for females
 "%;margin-right: 5px;'></span><span>", FEMALE_BAR, #proportion of females
 "%</span></span>")

当然,如果您愿意,您也可以将数字放在条形内,但是当条形很小时,这是一个问题。

PDF
我对 pdf 的了解还不够远。到目前为止,这是我的解决方案:

\begin{tabular}{>{$\rhd$ }lrl}
Loop at line 151 in divergence  & \mybar{3.420}\\
Loop at line 1071 in radiation  & \mybar{3.270}\\
scalar face value               & \mybar{3.090}\\
Loop at line 102 in get         & \mybar{1.700}\\
get sensible enthalpy           & \mybar{1.250}\\
\end{tabular}

PDF 栏

它看起来不太好。我对乳胶很陌生。我仍然需要找出如何将数字放在栏后面。以及如何将其放入我的函数中的 if 语句中。如果是 HTML 则:如果是 pdf 则...

我希望这可以帮助别人。但是,请考虑这个线程中提到的包。它们非常好,我的解决方案非常基于它们。我只是无法准确地得到我正在寻找的包,所以我手动完成了这个。

4

3 回答 3

8

只需在上一个答案中添加一个迷你图示例。希望能帮助到你。

---
title: http://stackoverflow.com/q/32841221/680068
---

```{r results="asis"}

library(dplyr)
library(formattable)
library(sparkline)


res <- 
  iris %>% 
  group_by(Species) %>% 
  summarise(N=n(),
            SL_Mean=round(mean(Sepal.Length),3),
            SL_SD=round(sd(Sepal.Length),3),
            SW_Mean=round(mean(Sepal.Width),3),
            SW_SD=round(sd(Sepal.Width),3)) %>%
  mutate(sparkline = as.character(Species))

#using formattable
formattable(
  res,
  list(
    SL_Mean=color_bar("pink", proportion),
    "sparkline"=function(z){
      sapply(
        z,
        function(zz){
          knitr::knit(
            text = sprintf(
              '`r sparkline(c(%s))`',
              paste0(
                iris[which(iris$Species == zz),"Sepal.Length"],
                collapse=","
              )
            ),
            quiet = TRUE
          )
        }
      )
    }
  )
)


```

另外,我认为一个非rmarkdown示例可能会让一些人高兴。

library(dplyr)
library(formattable)
library(sparkline)


res <-
  iris %>%
  group_by(Species) %>%
  summarise(N=n(),
            SL_Mean=round(mean(Sepal.Length),3),
            SL_SD=round(sd(Sepal.Length),3),
            SW_Mean=round(mean(Sepal.Width),3),
            SW_SD=round(sd(Sepal.Width),3)) %>%
  mutate("sparkline" = as.character(Species))

#using formattable
ft <- formattable(
  res,
  list(
    SL_Mean=color_bar("pink", proportion),
    "sparkline"=function(z){
      sapply(
        z,
        function(zz){
          sprintf(
            '<span class="sparkline-bar">%s</span>',
            paste0(
              iris[which(iris$Species == zz),"Sepal.Length"],
              collapse=","
            )
          )
        }
      )
    }
  )
)

library(htmlwidgets)
browsable(
  attachDependencies(
    tagList(
      onRender(
        as.htmlwidget(ft),
        "function(el,x){$('.sparkline-bar').sparkline('html',{type:'bar'});}"
      )

    ),
    htmlwidgets:::widget_dependencies("sparkline","sparkline")
  )
)
于 2015-09-30T18:19:05.997 回答
7

我认为您需要的是可格式化的包。还有一个将迷你图与格式表混合的示例,但我无法根据您的需要进行调整。

---
title: http://stackoverflow.com/q/32841221/680068
---

```{r cars}

library(dplyr)
library(formattable)

res <- 
  iris %>% 
  group_by(Species) %>% 
  summarise(N=n(),
            SL_Mean=round(mean(Sepal.Length),3),
            SL_SD=round(sd(Sepal.Length),3),
            SW_Mean=round(mean(Sepal.Width),3),
            SW_SD=round(sd(Sepal.Width),3))

#using formattable
formattable(res,
            list(
              SL_Mean=color_bar("pink", 0.5)))


```

在此处输入图像描述

于 2015-09-30T08:10:58.923 回答
1

还有一个名为sparkTable的包,您可以创建迷你图对象并将它们添加到您的降价对象中。这些对象就像我猜你可以在 rmarkdown 中引用的 ggplot 对象。但您也可以将对象保存为 pdf、eds 或 png。

还有一个 sparkTable 对象,可以像您打印的那样打印表格。

来自论文(p28):

    ‘‘‘{r, echo=TRUE}
require( sparkTable )
sl <- newSparkLine (values = rnorm (25) , lineWidth = .18, pointWidth = .4,
width = .4, height = .08)
export(sl , outputType = "png", filename = "sparkLine ")
‘‘‘
This is a sparkline included in the ![ firstSparkLine ]( sparkLine.png)
text ...
于 2015-11-17T21:13:28.827 回答