Error using text input in shiny app

I want to use two text from by the user in a Shiny apps, and pass that two text values to another function, I don't know what I'm missing because the code doesn't works and returns "input object not found"

I want to use that two inputs to make the contrasts in the makeContrasts() function. That function accept character vectors to make the contrasts

Here is the code

header <- dashboardHeader(
  title = "DGE Ebayes Analyzer"
)

sidebar <- dashboardSidebar(
  #sidebarmenu
  sidebarMenu(id ="tabs",
              menuItem("DGE", tabName = "dge", icon = icon("server"))
  )
)
        
body <- dashboardBody(
  
  #tabitems
  tabItems(
    tabItem(tabName = "dge",
            h2("DGE Analysis"),
            fluidRow(
              box(title = "TARGETS",
                  solidHeader = T, status = "info", width = 6,
                  
                  fileInput("file2", "Load custom targets file",
                            accept = ".csv",
                            placeholder = "Please, load custom targets file"),
                  submitButton("Submit")
              ),
              box(title = "Selecting Groups",
                  solidHeader = T, status = "warning", width = 12,
                  
                  #code to make group boxes for text input from user
                  box(title = "Select Groups",
                      solidHeader = T, status = "info", width = 12,
                      fluidRow(
                        column(6,
                               textInput(inputId = "group1",
                                         label="Group 1",
                                         value = "")
                        ),
                        column(6,
                               textInput(inputId = "group2",
                                         label = "Group 2",
                                         value = ""),
                               submitButton("Submit")
                               
                               
                        ))
                      
                      )
                  ),
              box(title = "Contrasts",
                  solidHeader = T, status = "info",width = 12,
                  dataTableOutput("cont"))
              )
            )
    )
)

ui <- dashboardPage(header, sidebar, body, skin = "yellow")

server <- function(input, output){
  
  #####Load targets file#################
  
targets <- reactive({
    validate(
      need(input$file2 !="", "Please load the custom targets file")
    )
    targets1 = input$file2
    data1 = read.csv(targets1$datapath,sep = ";")
    return(data1)
  })

###Design Matrix######

design <- reactive({
  groups <- as.character(targets()$Group)
  groups1 <- as.factor(groups)
  lev <- factor(groups1, levels = unique(groups1))
  design <- model.matrix(~ 0 + lev)
  colnames(design) <- levels(lev)
  rownames(design) <- targets()$Name
  return(design)
})

#Make Contrasts
cont.matrix <- reactive({
  contrasts <- makeContrasts(
    contrasts =paste(input$group1, input$group2, sep="-"),
    levels = design())
})

#table contrasts

output$cont <- renderDataTable({
  cont.matrix()
})

}

shinyApp(ui = ui, server = server)

Sorry that I can't help.
I played with this setup briefly, but since your shiny-reprex leaves out makeContrasts and an example dataset, I have to work through a number of errors, making (probably bad) assumptions about what makeContrasts does and what a typical input file should look like.

I think you'll be more likely to find help here if you could supply makeContrasts and example data-file.

Hello, sorry I add that info now, makeContrasts() builds a contrast matrix corresponding to specified contrast of a set of parameters as a numeric matrix, he takes 2 arguments , contrasts= a character vector specifying contrasts. and levels.

makeContrasts documentation
For example in my custom targets file i have the following groups:

-Control
-SIPGC

And I want to use them as a contrasts, so i build the makeContrasts as follows:

#first i make the desing matrix with the file

groups <- as.character(targets$Group)
groups <- as.factor(groups)
lev<-factor(groups, levels=unique(groups)) 
design <- model.matrix(~0+lev)
colnames(design)<-levels(lev)
rownames(design) <-sampleInfo

 #Then build the constrasts
cont.matrix <- makeContrasts(
    contrasts = control - SIPGC, 
    levels=design)

The output with str is as follows:

str(cont.matrix)
 num [1:2, 1] 1 -1
 - attr(*, "dimnames")=List of 2
  ..$ Levels   : chr [1:2] "control" "SIPGC"
  ..$ Contrasts: chr "contrasts"

And if i table as follows:


kable(cont.matrix)


|        | contrasts|
|:-------|--------------------:|
|control |                    1|
|SIPGC   |                   -1|

So in mi shiny app i want that the desired output as:

contrasts
input$group1 1
input$group2 -1

But in my code I did something wrong and the textinputs doesn't work as parameters to makeContrasts() , the funtion accepts character vectors.

I let my targets file with the groups if you want to try

targets.csv

The finality is that the final user of the shiny app, load his targets file and cand define the contrasts of the makeContrasts() function, with the text input boxes.

thank you in advance

Best regards

1 Like

Thanks for the example input file, and letting us know that with that input file, we'd want to list "control" and "SIPGC" the Select Groups UI field. That was really helpful in getting this reproducible.

In make contrasts, I think you want to return contrasts:

  #Make Contrasts
  cont.matrix <- reactive({
    contrasts <- limma::makeContrasts(
      contrasts = paste(input$group1, input$group2, sep="-"),
      levels = design())
    return(contrasts)
  })

With this change, I managed to get your cont.matrix() table to appear as above in the "Contrasts" box


Now it works thank you a lot, I was pretty stuck with that problem

:slight_smile:

1 Like