1

I am working in a ShinyApp which objective here is to create a selection based on attributes that are generated from an Excel database (the number of attributes may vary). Below is the most important part of my server.R code:

shinyServer(function(input, output, session){

    seleciona_planilha <- observe({

      dados = input$arquivo

      if (is.null(dados))
      {
        return(NULL)
      }
      else
      {
        capturar = names(getSheets(loadWorkbook(dados$datapath)))

        updateSelectInput(session,"planilha",choices = capturar)
      }
    }) 

 carrega_dados <- reactive({

      dados = input$arquivo

      if (is.null(dados))
      {
       return(NULL)
      }

      else{return(read.xlsx(dados$datapath, sheetName = input$planilha))}
    })

The first "observe" function above works well in order to select the correct sheet and proceed to analysis with the code below.

rotina <- reactive({ 

  dados = carrega_dados()
  tam_dados = length(dados)
  pos_ini = 22
  vet_comp = vector()
  resultados = as.data.frame(matrix(nrow = length(seq(pos_ini,tam_dados,2)), ncol = 10))
  nomes = names(dados)

  cd = 4
  k = 1

  amostra = vector()

  for(i in 1:length(dados[,1]))
  {
    if(dados[i,6] == 1)
    {
      amostra[1] = as.character(dados[i,7])
      break
    }
  }

  for(i in 1:length(dados[,1]))
  {
    if(dados[i,6] == 2)
    {
      amostra[2] = as.character(dados[i,7])
      break
    }
  }

  names(resultados) = c("Name",amostra[1], amostra[2],"NSD",
                        "Tau","Var(Tau)","D-Prime",
                        "Var(D-Prime)","IC(D-Prime)","p-value(D-Prime)")

  for(i in seq(pos_ini,tam_dados,2))
  {
    cNSD = 0
    c1 = 0
    c2 = 0

    for(j in 1:length(dados[,i]))
    {
      if(as.character(dados[j,i]) == " NSD" || as.character(dados[j,i]) == "NSD")
      {
        cNSD = cNSD + 1
      }

      if(as.character(dados[j,i]) == "1")
      {
        c1 = c1 + 1
      }

      if(as.character(dados[j,i]) == "2")
      {
        c2 = c2 + 1
      }
    }

    vet_comp = c(c1,cNSD,c2)

    resultados[k,1] = nomes[i]
    resultados[k,2] = c1
    resultados[k,3] = c2
    resultados[k,4] = cNSD
    resultados[k,5] = round(twoAC(vet_comp)$coefficients[1,1],cd)
    resultados[k,6] = round((twoAC(vet_comp)$coefficients[1,2])^2,cd)
    resultados[k,7] = round(twoAC(vet_comp)$coefficients[2,1],cd)
    resultados[k,8] = round((twoAC(vet_comp)$coefficients[1,2])^2,cd)
    if(vet_comp[1] != 0 && vet_comp[2] != 0 && vet_comp[3] != 0)
    {
      resultados[k,9] = paste("[",round(twoAC(vet_comp)$confint[,1],cd),";",
                              round(twoAC(vet_comp)$confint[,2],cd),"]")
    }
    else
    {
      resultados[k,9] = paste("No IC")
    }
    resultados[k,10] = round((twoAC(vet_comp)$p.value)/2,cd)

    k = k + 1
  }

  return(resultados)
})

The reactive function "rotina" returns a data.frame. The first column in the data.frame are the attribute names that I would like to use in a selector.

But for some reason I don't know, when I call another "observe" function to get the attribute names and pass to the selector, it not works.

  seleciona_atributo <- observe({

     resultados = rotina()

     atributos = resultados[,1]

     updateSelectInput(session,"atributo",choices = atributos)

    })

I tried to assign "resultados" as a global variable too, but with no success.

Finally, my ui.R code:

    shinyUI(fluidPage(
  titlePanel("2-AC Sensory Tool"),
  sidebarLayout(
    sidebarPanel(
      fileInput('arquivo', 'Choose XLS/XLSX File',
                accept=c('.xls','.xlsx')),
      tags$hr(),
      selectInput("planilha",label = h4("Select data sheet"),""),
      tags$hr(),
      selectInput("atributo",label = h4("Select attribute to generate d-prime graphic",""),
      downloadButton('download', 'Download results')
    ),
    mainPanel(
      plotOutput("grafico_dp"),
      plotOutput("grafico_dist"),
      h4("Results Table"),
      dataTableOutput("saida")
    )
  )
))

Thanks in advance!

  • What is the error that you get? Or is there no error and you just don't get an updated drop down menu? – tblznbits Mar 21 '16 at 13:13
  • The app crashes soon after executing! – PietroTiaraju Mar 21 '16 at 13:17
  • It's not clear from your example what is the problem. It also relies on files we don't have access, too. Please provide a [minimal working example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that reproduces the problem you're having. – Roman Luštrik Mar 21 '16 at 13:23
  • What is the error that is reported in the R console? I'm assuming it's the fact that `input$arquivo` is `NULL` at first and not all of your functions/observations are set up to account for that. We'll be able to help more if you can provide the actual error you're getting though. – tblznbits Mar 21 '16 at 13:42
  • Sorry. The reported error is "ERROR: [on_request_read] connection reset by peer.". I'll try to work in a more reproducible example and post here when I finish. – PietroTiaraju Mar 21 '16 at 13:50
  • [This](http://r.789695.n4.nabble.com/Shiny-error-connection-reset-by-peer-td4675871.html) might help? I'm not sure what browser you're using. – tblznbits Mar 21 '16 at 14:23
  • Unfortunately not. I tried both in Shiny Browser and Chrome and doesn't not work when I use `seleciona_atributo` as an observer. If I substitute the selector by a slider or a numeric selector it works fine (but I don't need to use an observe function in this two cases). So I think the problem is when I'm loading the results from a data.frame into the observe function. – PietroTiaraju Mar 21 '16 at 15:20

0 Answers0