david
david

Reputation: 825

Shiny formula with variable names error

While trying to compute a formula with shiny i get an error. As a short example the reactivevalues is fed with an input button. Let´s say it contains a character vector (e.g Age). Adonis is available from the vegan library.

  values <-reactiveValues(data = NULL)

  adof <-  function()({

    #metadata is a dataframe with columns variables for each sample (Age,gender...)
    metadata <- as(sample_data(phyloseq), "data.frame")
    dis <- phyloseq::distance(phyloseq, method="uunifrac")
    #dis is a distance matrix 


    ad <- data.frame(adonis2(dis ~ values$data,data=metadata)
}
#Results
Error:object of type 'closure' is not subsettable

However if i use the following formula it will work. (changing reactivevalue with a character vector)

 adof <-  function()({

        metadata <- as(sample_data(phyloseq), "data.frame")
        dis <- phyloseq::distance(phyloseq, method="uunifrac")
        ad <- data.frame(adonis2(dis ~ Age ,data=metadata))           
        cat("Selected:"values$data)
        ad
        }

# Results
    selected: Age 
              Df  SumOfSqs        R2        F Pr..F.
    Age       4  9.863528 0.2394484 21.09395  0.001
    Residual 268 31.329187 0.7605516       NA     NA
    Total    272 41.192715 1.0000000       NA     NA    

The way my reactive value is fed is with an observe statement

observe({ 
  # Fed reactive value with my selected radiobutton
  values$data =  input$data
}) 

Upvotes: 0

Views: 94

Answers (1)

Bertil Baron
Bertil Baron

Reputation: 5003

the problem is that values$data is variable containing a string. You have to convert it to a formula before you can use it.

try something like this.

adonis2(as.fomula(paste0("dis ~ ",values$data)),data=metadata)

Hope this helps!!

Upvotes: 1

Related Questions