本文介绍了在SHILINY中输出N个表,其中N取决于数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个shiny
应用程序,我想在其中打印多个表。问题是,我不知道我将预先拥有多少个表-这取决于数据。例如,如果变量"X"有5个级别,我想输出5个表-变量的每个级别对应一个表。要生成表,我调用server.R
中的renderTable()
内的函数,并将其分配给output
槽,如下所示:
output$tablePyramid <- renderTable ({
tableGeneratingFunction(argument1, argument2, ...)
})
如果我在renderTable()
中放入多个"tableGeneratingFunction",它只返回最后生成的表。因此,似乎每个output
槽只有一张表。我想我可以在server.R
文件中处理此问题,根据需要动态分配尽可能多的output
插槽。
ui.R
文件中的所有输出。两个表的示例摘录:mainPanel(
tabsetPanel(
... some code
tabPanel(title="Proportions",
tableOutput("tablePyramid"),
tableOutput("tablePyramid2")
),
... some more code
我是否必须在其自己的tableOutput
函数中列出每个表,或者是否有更优雅的方式继续,因为我事先不知道需要多少个tableOutput
?
推荐答案
我从我的问题(R Shiny - add tabPanel to tabsetPanel dynamically (with the use of renderUI))中包含Dieter链接的评论开始。原理是一样的-用Server.R
中的所有表生成HTML,然后在Ui.R
中用uiOutput()
显示它。不同之处在于,我在shiny
包中找不到类似于给定示例中的tabPanel()
生成HTML的函数。
但是我能够使用xtable()
生成HTML,并向其传递一些额外的参数,以便在呈现时使表看起来像shiny
框架中所期望的那样。
为任意数量的表生成HTML的函数示例:
tabelize <- function(variables, arg2, ...) {
tables <- list() # create a list to hold all tables
for (variable in variables) { # go through all possible values of variables
table <- function_that_returns_a_data_frame(variable, arg2, ...)
tables[[as.character(variable)]] <-
# save table into slot in created list
# print table as HTML with additional formatting options
print(xtable(table, caption=paste("Variable:", variable)),
type="html",
html.table.attributes='class="data table table-bordered table-condensed"',
caption.placement="top")
}
return(lapply(tables, paste)) # return HTML tables pasted together
}
在Server.R
(带有一些附加选项)中调用此函数并分配给output$
槽:
output$tables <- renderUI({
out <- tabelize(variables, arg2, ...)
# additional options to make rendering possible
return(div(HTML(out),class="shiny-html-output"))
})
在Ui.R
中对其执行uiOutput()
:
... code
uiOutput("tables")
... code
如果有更好的方法,请评论。
这篇关于在SHILINY中输出N个表,其中N取决于数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!