【问题标题】:Can I add multiple expressions to a renderTable?我可以向渲染表添加多个表达式吗?
【发布时间】:2020-11-24 12:25:37
【问题描述】:

我正在编写一个闪亮的应用程序,它获取用户上传的数据并根据他们数据中的任何变量计算汇总统计数据。如果您使用 iris 数据集作为示例,用户可以上传该数据集并按物种计算步长的汇总统计。我有代码可以为每个统计数据创建一个汇总表,但我希望能够创建一个表格,其中包含一个空间中的所有汇总统计数据。我正在使用的一段代码如下。您将看到用于计算平均值和中位数的代码,但它显示为两个表格。有没有办法对此进行调整,以便我可以在一个表中包含多个表达式?

server <- function(input, output, session) {
    
    data <- reactive({
        file1 <- input$csvFile
        if (is.null(file1)) { 
            return() 
        } 
        data = read.csv(file=file1$datapath)
        data
    })

    output$var_dem <- renderUI({
        selectInput("dem", "Select a demographic variable.", choices= names(data()))
    })
        
    output$var_ui <- renderUI({
        selectInput("var", "Select an outcome variable.", choices= names(data()))
    })
    
    output$mean <- renderTable(
        tapply(data()[,input$var], data()[,input$dem], mean), 
        rownames = TRUE, 
        colnames = FALSE
    )
    
    output$median <- renderTable(
        tapply(data()[,input$var], data()[,input$dem], median), 
        rownames = TRUE, 
        colnames = FALSE
    )

【问题讨论】:

标签: shiny tapply


【解决方案1】:

在均值和中位数的情况下,您可以为每个列创建一个数据框列并将其传递给 renderTable:

library(datasets)

data.frame(mean   = tapply(iris[,"Sepal.Length"], iris[,"Species"], mean),
           median = tapply(iris[,"Sepal.Length"], iris[,"Species"], median))

【讨论】:

    猜你喜欢
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多