【发布时间】: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,但无法弄清楚。
【问题讨论】: