【问题标题】:Contingency table in shiny闪亮的列联表
【发布时间】:2016-02-01 15:07:13
【问题描述】:

我有一张桌子:

structure(list(Gender = structure(c(2L, 1L, 2L, 2L, 2L), .Label = c("Female", 
"Male"), class = "factor"), AGE = c(20L, 20L, 15L, 16L, 13L), 
    BOTTLE_CNT = c(3L, 0L, 0L, 1L, 2L), QUALIFICATION_DESC = structure(c(2L, 
    2L, 1L, 2L, 2L), .Label = c("12th and below", "Graduation"
    ), class = "factor")), .Names = c("Gender", "AGE", "BOTTLE_CNT", 
"QUALIFICATION_DESC"), class = "data.frame", row.names = c(NA, 
-5L))

我正在构建一个闪亮的应用程序来呈现列联表。由于它是一个大表,我使用了以下代码:

library(shiny)

shinyApp(
    ui=shinyUI(bootstrapPage(
        fluidRow(
            column(3,
                   div(style = "font-size: 13px;", selectInput("colum", "Select Column Variable", ''))
            ),
            column(3,
                   div(style = "font-size: 13px;", selectInput("rowvar", label = "Select Row Variable", ''))
            )),
        fluidRow(
            tableOutput('foo')    
            )
    )),
    server=shinyServer(function(input, output, session) {

        s <- reactive(
            a
            )


        observe({
            updateSelectInput(session, "colum", choices = sort(as.character(colnames(s()))))
        })

        observe({
            updateSelectInput(session, "rowvar", choices = sort(as.character(colnames(s()))))
        })

        output$foo <- renderTable({
            with(s(), table(input$rowvar, input$colum))
        })
    })
)

而不是with(s(),table……我试过了,用

xtabs(~input$rowvar + input$colum, s())

如果我直接使用列名和行名,两者都不起作用。我想要的是选择的行和列变量,这两个变量的交叉表是必需的。我曾尝试使用来自library(gmodels)CrossTable,但无法弄清楚。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您需要将对象传递给tableinput$rowvarinput$colum 是字符串。

    你可以试试:

    with(s(), table(get(input$rowvar),get(input$colum)))
    

    如果您想使用xtabs,您可以尝试使用pasteas.formula 从输入创建公式:

    xtabs(as.formula(paste0("~",input$rowvar,"+",input$colum)), s())
    

    另外,您可以设置用户可以直接在ui.R 中选择的值,而不是使用updateSelectInput

    selectInput("colum", "Select Column Variable", sort(as.character(colnames(a))))
    

    如果你想使用updateSelectInput,你可能想在你的renderTable中使用validate/need,否则应用程序在初始化时会抛出一个错误,因为input$columinput$rowvar在更新之前是NULL

    output$foo <- renderTable({
          validate(need(input$rowvar,''),
                   need(input$colum,''))
          xtabs(as.formula(paste0("~",input$rowvar,"+",input$colum)), s())
        })
    

    【讨论】:

    • 这真的很好,NiceE!!!。非常感谢......我尝试了第一个选项,它就像一个魅力......从来不知道使用'get'功能。也会尝试所有其他选项,这将是一次很棒的学习体验。
    • @LeArNr,我有类似的要求,这篇文章非常有用。你能确认你的代码中's'和'a'的重要性吗?因此,我需要更新我的情况。谢谢!!
    • @NicE,我有类似的要求,这篇文章非常有用。您的解决方案非常有帮助。你能确认你的代码中's'和'a'的重要性吗?因此,我需要更新我的情况。谢谢!!
    • a 是用户的表。 s 是包裹在 reactive 中的表格。这里没有必要,因为它没有被修改,但我假设这是一个简化的例子。
    • @NicE,谢谢你的解释!我用我的数据集替换了一个 data.table 并且我开始收到错误“updateSelectInput:找不到对象'会话'”。我不确定为什么这个错误使其他一切都保持不变。然后我想按照您的建议在ui.R中设置值,而不是传递a的列名,而是传递“data_input”,这是我的data.table,但它给出了找不到data_input的错误。你知道什么可能导致错误“updateSelectInput: object 'session' not found”吗?
    猜你喜欢
    • 2015-09-11
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    • 2015-08-05
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 2021-01-31
    相关资源
    最近更新 更多