0

I'm trying to create a Shiny/flexdashboard Markdown file, and I've stuck the following in a R code chunk. Basically, it subsets the input dataframe using two inputs - the second input depends on the first:

output$Box1 = renderUI(selectInput('sector', 
                                 'Sector', 
                                 c(levels(test$Sector),"pick one"),
                                 "pick one") )

output$Box2 = renderUI(
if (is.null(input$sector) || input$sector == "pick one"){return() 
}else selectInput('FA', 
                  'Functional Area', 
                  c(levels(test$FA[which(test$Sector == input$sector)]),"pick one"),
                  "pick one"))

subdata1 = reactive(test[which(test$Sector == input$sector),])
subdata2 = reactive(subdata1()[which(subdata1()$FA == input$FA),])
renderDataTable({subdata2()},  options = list(dom = 'lpt'))

Now, this works fine to subset the data frame (Sector, then FA), and the subsetted data frame is visible in the dashboard via renderDataTable.

What I'd like to do is to perform things like table on the reactive data subset, to create a contingency table on factor levels:

 data1 <- reactive(table(subdata2()$UserCountry))

and then further use this new table, for example:

renderDataTable({data1()},  options = list(dom = 'lpt'))

When I try this, the new table doesn't render.

Is there a way of running this successfully?

4

1 回答 1

1

Try

data1 <- reactive(as.data.frame(table(subdata2()$UserCountry)))

While values returned by table() are printed like matrices on the console, they have their own distinct class (table). It is not handled by renderDataTable(), which requires a data.frame or matrix.

于 2016-09-27T18:14:52.840 回答