Search code examples
rshinyreshape2dcast

Using an multiple input from Shiny in Reshape2 dcast()


Taking reference from Using an input from Shiny Widget in Reshape Cast(), I am able to create dynamic summary table by Shiny input with single row variable.

However, when I wish to create multiple row layer columns, I found the second or the third object cannot be found. I've tried to print out the formula, which appears to be the correct one. For example, if "vs" and "hp" was selected in Shiny, then the formula became "vs + hp ~ gear", which seems correct. But the error "object 'hp' not found" appears.

Could you please help me on this? Thanks!

UI:

library(shiny)
library(dplyr)
library(reshape2)
library(DT)

ui <- fluidPage(
  headerPanel("Summary Table"),
  sidebarPanel(selectInput("row", "Row variable", names(mtcars), selected = "vs", multiple = T),
               selectInput("col", "Column variable", names(mtcars), selected = "gear")),
  mainPanel(DT::dataTableOutput('table'))
)

Server:

server <- function(input, output) {

  output$table <- renderDataTable({

    formula <- as.formula(paste(paste(input$row, collapse = "+"), "~", input$col))
    print(formula)

    car0 <- mtcars %>% 
      group_by_(input$row, input$col) %>% 
      summarize(n = n_distinct(cyl)) %>% 
      dcast(formula, value.var = "n")   

    datatable(car0)

  })
}

shinyApp(ui, server)

Solution


  • Something like that?

    library(shiny)
    library(dplyr)
    library(reshape2)
    library(tidyr)
    library(DT)
    ui <- fluidPage(
      headerPanel("Summary Table"),
      sidebarPanel(selectInput("row", "Row variable", names(mtcars), selected = "vs", multiple = T),
                   selectInput("col", "Column variable", names(mtcars), selected = "gear")),
      mainPanel(DT::dataTableOutput('table'))
    )
    
    server <- function(input, output) {
    
      output$table <- renderDataTable({
    
        car0 <- mtcars %>% 
          select(input$row,input$col,cyl)%>%
          group_by_at(., vars(input$row, input$col))%>%
          summarize(n = n_distinct(cyl))%>%
          tidyr::spread(input$col,n)
    
    
        datatable(car0)
    
      })
    }
    
    shinyApp(ui, server)
    

    for formula and reshape2

    library(shiny)
    library(dplyr)
    library(reshape2)
    library(tidyr)
    library(DT)
    ui <- fluidPage(
      headerPanel("Summary Table"),
      sidebarPanel(selectInput("row", "Row variable", names(mtcars), selected = "vs", multiple = T),
                   selectInput("col", "Column variable", names(mtcars), selected = "gear")),
      mainPanel(DT::dataTableOutput('table'))
    )
    
    server <- function(input, output) {
    
      output$table <- renderDataTable({
    
        formula <- as.formula(paste(paste(input$row, collapse = "+"), "~", input$col))
        print(formula)
    
        car0 <- mtcars %>% 
          select(input$row,input$col,cyl)%>%
          group_by_at(., vars(input$row, input$col))%>%
          summarize(n = n_distinct(cyl))%>%
          #tidyr::spread(input$col,n)
          dcast(formula, value.var = "n")  
    
    
        datatable(car0)
    
      })
    }
    
    shinyApp(ui, server)