Q:

Using an input from Shiny Widget in Reshape Cast()

I'm attempting to create a sort of pivot table in my shiny web app where users select which variables they want to cast against one another. The problem I'm finding is that when I try to use cast with my 'input$columns' I get the following: Error : Casting formula contains variables not found in molten data: input$columns. The input$columns should be one of three column names found in my dataset, so I'm not sure what the problem might be. I'm attaching a simplified version of the code to try to isolate the problem.
UI
 library(shiny)

    shinyUI(fluidPage(

     titlePanel("Reactivity"),

     sidebarLayout(
       sidebarPanel(
       textInput("caption", "Caption:", "Column Name:"),

       selectInput("columns", "Choose a dataset:", 
                  choices = c("Publisher", "Region", "Representative")),

       numericInput("obs", "Number of observations to view:", 10)
      ),

     mainPanel(
      h3(textOutput("caption")), 

      verbatimTextOutput("summary"), 

      dataTableOutput('view')
     )
    )
    ))

Server
library(shiny)
library(datasets)

shinyServer(function(input, output) {

  datasetInput <- reactive({
    switch(input$columns,
           "Publisher" = Publisher,
           "Region" = Region,
           "Representative" = Representative)
  })

  output$caption <- renderText({
    input$caption
  })

  output$summary <- renderPrint({
    dataset <- datasetInput()
    summary(dataset)
  })

  output$view <- renderDataTable({
    test <- input$columns
    m.book.sales <- melt(book.sales)
    cast(m.book.sales, test ~ variable, sum)
  })
})

and dataset
> head(book.sales)
  Representative Region      Month Publisher    Subject Sales Margin Quantity
1            Raj      S 1997-01-01      SAGE Management 135.0  63.45        9
2            Raj      S 1997-01-01 Routledge Statistics 120.0  48.00        6
3            Raj      S 1997-01-01   Penguin  Economics  54.0  22.68        4
4            Raj      S 1997-01-01 Routledge    Fiction 234.0 128.70        9
5           Soni      S 1997-01-01   Penguin   Politics  54.0  22.68        4
6           Soni      S 1997-01-01      SAGE   Politics 185.4 100.12       12

A:

You need to pass cast a valid formula. test is a variable and cannot be used in your formula call. You can construct a forumla using paste and use as.formula to convert:
 output$view <- renderDataTable({
   test <- input$columns
   m.book.sales <- melt(book.sales)
   myFormula <- as.formula(paste0(test, " ~ variable"))
   cast(m.book.sales, myFormula, sum)
 })

