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(id ="tabs",
              menuItem("DGE", tabName = "dge", icon = icon("server"))
body <- dashboardBody(
    tabItem(tabName = "dge",
            h2("DGE Analysis"),
              box(title = "TARGETS",
                  solidHeader = T, status = "info", width = 6,
                  fileInput("file2", "Load custom targets file",
                            accept = ".csv",
                            placeholder = "Please, load custom targets file"),
              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,
                               textInput(inputId = "group1",
                                         label="Group 1",
                                         value = "")
                               textInput(inputId = "group2",
                                         label = "Group 2",
                                         value = ""),
              box(title = "Contrasts",
                  solidHeader = T, status = "info",width = 12,

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

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

###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

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

#table contrasts

output$cont <- renderDataTable({


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:


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)
rownames(design) <-sampleInfo

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

The output with str is as follows:

 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:


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

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

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


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())

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


1 Like