0

Thanks for your suggestion. I checked that question before I asked my own but I couldn´t fix my problem. I define a vector var_name<-reactive(input$xcol) and then I used tableData[,c("var_name")] as the input in the hist function but with no success.

I´m building an app in which the user can upload his own file and make some analysis. I could manage to update the variable´s names in a selectInput each the user upload a different file. Now my issue is that I can´t pass the variable selected in the selectInput to build a histogram. How could I pass the variable selected as an input to a histogram? Here is my ui.r code. Once I execute my code the error I get in my app is "object of type 'closure' is not subsettable". Any advice please? Thanks

library(shiny)
ui<-fluidPage(
titlePanel(title = "My app"),
sidebarLayout(
sidebarPanel(
  fileInput('file1', 'Cargar archivo',
            accept = c(
              'text/csv',
              'text/comma-separated-values',
              'text/tab-separated-values',
              'text/plain',
              '.csv',
              '.tsv'
            )
  ),
  checkboxInput('header', '¿Contiene Encabezado?', TRUE),
  radioButtons('sep', 'Delimitador',
               c(Comma=',',
                 "Punto y coma"=';',
                 Tab='\t'),
               ','),
  radioButtons('quote', 'Quote',
               c(Ninguna='',
                 'Dobles'='"',
                 'Simples'="'"),
               '"'),
  radioButtons('resume', 'Summary',
               c('Individual',
                 'Múltiple'), selected = "Múltiple",
               inline = TRUE),
  conditionalPanel("input.resume === 'Individual'",
                   selectInput('xcol', 'Variable X', ""),
  sliderInput("bins","Seleccione el número de clases",min=5, max = 15, value = 6),
  radioButtons("color","Seleccione un color", c("Amarillo","Azul","Rojo","Gris"), selected = "Azul", inline = TRUE)
  )
),
mainPanel(h3("Muestra del archivo cargado:"),
          tableOutput('contents'),
          verbatimTextOutput("summary"),
          plotOutput("histog")
  )
  )
  )


server<-function(input, output, session) {
tableData <- reactive({

inFile <- input$file1

if (!is.null(inFile)){
  read.csv(inFile$datapath, header=input$header, sep=input$sep, 
           quote=input$quote)
}
else {
  return(NULL)
}
})
observe({
updateSelectInput(
  session,
  "xcol",
  choices=names(tableData()))
  })
var_name<-reactive(input$xcol)

output$contents <- renderTable({
head(tableData())
})
output$summary <- renderPrint({
summary(tableData())
})
output$histog<-renderPlot({

hist(tableData[,c("var_name")],breaks= seq(0,10,l=input$bins+1), col(input$color))
})
}

shinyApp(ui = ui, server = server)
Jrgsua83
  • 3
  • 4
  • Possible duplicate of [Error in : object of type 'closure' is not subsettable](http://stackoverflow.com/questions/11308367/error-in-my-code-object-of-type-closure-is-not-subsettable) – yeedle Nov 10 '16 at 19:04
  • Thanks @ yeedle. I checked that question but it didn´t solve my problem. I defined in my server.R the following: var_name<-reactive(input$xcol) output$histog<-renderPlot({ hist(tableData[,c("var_name")],breaks= seq(0,10,l=input$bins+1), col(input$color)) }) but this didn´t fix my problem. – Jrgsua83 Nov 10 '16 at 20:37

1 Answers1

0

There are actually several issues with your shiny app:

  1. To be recognized, the colors should be coded this way:

    c(Amarillo="yellow",Azul="blue",Rojo="red",Gris="grey")
    
  2. The var_name should handle NULL

    var_name<-reactive({
        if(input$xcol!="")
           input$xcol
        else
          NULL
        })
    
  3. The reactive values should always be used with parenthesis:

    tableData()
    var_name()
    

This is actually what produces the error

  1. Color parameter should be written this way:

    col=input$color
    
  2. You should check whether your reactive values are NULL or not

    if(!is.null(tableData())&&!is.null(var_name()))
        hist(tableData()[,var_name()],breaks= seq(0,10,l=input$bins+1), col=input$color)
    
HubertL
  • 19,246
  • 3
  • 32
  • 51