闪亮的renderTable add.to.row

时间:2016-02-29 16:03:45

标签: r shiny xtable

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

> 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 argmument添加两个多列。

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只是将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")

但这会导致

MathJax output

1 个答案:

答案 0 :(得分:0)

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中手动添加它们,将它们放在您的其他标题下。

结果如下:

table example