闪亮的渲染表 add.to.row

Shiny renderTable add.to.row

我有一个看起来像这样的数据框

> df
Product    noPat     Val  Tot TotVal
Product A  -318 -108.12 1356 461.04
Product C   403  544.05  462  623.7
Product D   -32  -39.68  529 655.96
Product B   N/A     N/A  N/A    N/A

我想使用 xtable 函数中的 add.to.row 参数添加两个多列。

print(xtable(df, digits=2, caption=strCaption, label="Test_table"), 
      size="footnotesize", #Change size; useful for bigger tables
      include.rownames=FALSE, #Don't print rownames
      caption.placement="top", 
      hline.after=NULL, #We don't need hline; we use booktabs
      add.to.row = list(pos = list(-1, 
                                   nrow(df)),
                        command = c(paste("\hline \n",
                                          "& \multicolumn{2}{c}{Growth} & \multicolumn{2}{c}{Total} \\ \n",
                                          "\hline \n"),
                                    "\hline \n")
      )
)

生成所需的乳胶输出:

\begin{table}[ht]
\centering
\caption{test} 
\label{Test_table}
\begingroup\footnotesize
\begin{tabular}{lllll}
  \hline 
 & \multicolumn{2}{c}{Growth} & \multicolumn{2}{c}{Total} \ 
 \hline 
Product & noPat & Val & Tot & TotVal \ 
 Product A & -318 & -108.12 & 1356 & 461.04 \ 
  Product C & 403 & 544.05 & 462 & 623.7 \ 
  Product D & -32 & -39.68 & 529 & 655.96 \ 
  Product B & N/A & N/A & N/A & N/A \ 
   \hline 
\end{tabular}
\endgroup
\end{table}

这是我想要的格式。我希望 Shiny 输出这个,但 Shiny 只是在 table.

上方添加 add.to.row 参数作为文本输出

我在 Shiny 中的实现:

Server.R

output$tabletest <- renderTable({
        rows <<- nrow(df)
        xtable(df, digits=2)

      },

      caption = "Sample Data",
      caption.placement = getOption("xtable.caption.placement", "bottom"), 
      caption.width = getOption("xtable.caption.width", NULL),
      include.rownames=getOption("xtable.include.rownames", FALSE), #Don't print rownames
      add.to.row = getOption("xtable.add.to.row",list(pos = list(-1, 
                                   rows),
                        command = c(paste("\hline \n",
                                          "& \multicolumn{2}{c}{Growth} & \multicolumn{2}{c}{Total} \\ \n",
                                          "\hline \n"),
                                    "\hline \n")))
      )

UI.R

tableOutput("tabletest")

使用 MathJax

按照 NicE 的建议,我尝试按以下方式使用 MathJax

Server.R

output$tabletest<- renderUI({
        result <- withMathJax(HTML(
        "\begin{table}[ht]
        \centering
        \caption{test}
        \label{Test_table}
        \begin{tabular}{lllll}
        \hline
        & \multicolumn{2}{c}{Growth} & \multicolumn{2}{c}{Total} \\
        \hline
        Product & noPat & Val & Tot & TotVal \\
        Product A & -318 & -108.12 & 1356 & 461.04 \\
        Product C & 403 & 544.05 & 462 & 623.7 \\
        Product D & -32 & -39.68 & 529 & 655.96 \\
        Product B & x & x & x & x \\
        \hline
        \end{tabular}
        \end{table}"
        ))
 })

UI.R

              withMathJax(),
              uiOutput("KeyIndicatorsPatients")

但这会导致

renderTable 将使用 type="html" 打印 xtable,但您的 add.to.row 是 Latex,因此不会打印它们。

您可以尝试将代码转换为 HTML,例如:

output$tabletest <- renderTable({
    xtable(df,digits=2)
  },
  size="footnotesize", #Change size; useful for bigger tables
  include.rownames=FALSE, #Don't print rownames
  caption.placement="top",
  include.colnames=FALSE,
  add.to.row = list(pos = list(0),
                    command = "<tr><th></th><th colspan='2'>Growth</td><th colspan='2'>Total</th></tr>
<tr> <th> Product </th> <th> noPat </th> <th> Val </th> <th> Tot </th> <th> TotVal </th>  </tr>"
  ))

我将 include.colnames=FALSE 添加到您的参数中,并在 add.to.row 中手动添加它们以将它们放在您的另一个 header 下。

结果如下所示: