how to call different input arguments for different functions in Shiny

im developing the R shiny webtool with some R functions for getting there indices in webtool as results. I have 7 R functions and made a selectinput widget having 7 input combinations in it. 5 R functions have single input argument for its functions and takes same input argument for all five of them. And another two R functions have different input arguments in them. I made
selectinput("signatures",signatures,c("GC","AAC","SCU","CGR","Di-odd","AMIP","OFDEG"))

for GC,AAC,SCU,CGR and Di-odd, it takes single and same input file as input argument.
i.e., GC(inputfile){script}, AAC(inputfile) {script}, SCU(inputfile){script}, CGR(inputfile){script}, Di-odd(inputfile){script}
while AMIP takes 3 input arguments i.e., AMIP(inputfile,n1,n2) and OFDEG function takes 2 input arguments i.e, OFDEG(inputfile,C)
fileinput widget for argument inputfile and remaining all arguemnts it takes numerical input .

I wrote Rshinycode accordingly but im getting error as ** Warning: Error in observeEventExpr: object 'input' not found [No stack trace available]**
how can I write my server inorder to get the results correctly

You will need to provide a minimal reproducible example of your problem. I don't think we can figure it out from your description.

Its not clear where the additional parameters would be set from?

I have 6 R functions, they are as follows:

RSCU(file){script},

cgr_res(file){script},

zscore_cal(file){script},

GC_Content7(file){} and

extractmod(file){script} all these 5 functions have single and same arguments i.e., input file as same input argument for all five R functions**

and last R function that is 6th R function have AMIP(file,n1,n2) 3 input arguments here input file is same as remaining 5, but n1 and n2 are different arguments ,

for n1 and n2 arguments I created numericalInput() widget but result coming blank, and for AAC also im not getting any result please solve me this query.

   
  library("shinythemes")
  library("shiny")
  library("htmltools")
  library("bsplus")
  library("DT")
  library("shinyalert")
  library("shinyjs")
  library("shinycssloaders")
  library("dplyr")
  library("data.table")
  library("reshape2")
  library("ggplot2")
  library("plotly")
  library("tools")
  library("readxl")
  library("writexl")
  library("seqinr")
  library("Biostrings")
  library("BiocManager")
  library("entropy")
  library("protr")
  
  source("GS_Source.R")
  
  ui = fluidPage(
    headerPanel(
      h1(tags$strong("Welcome to Web based Tool for Computation of Genomic"),
         style = "font-size: 20pt; line-height: 30pt; width = 100; color: #Red",
         align = "center"),
      windowTitle = "Home"),
    
    sidebarLayout(
      sidebarPanel(
        style = "background-color: #E52B50;",
        tags$style(type='text/css', 
                   "label { font-size: 12px; }",
                   ".selectize-input { font-size: 12pt; line-height: 12pt;}, 
                   .selectize-dropdown { font-size: 12pt; line-height: 12pt; }"),
        
        fileInput('file', HTML('<p style="color:#4A2768; font-size: 12pt"> Choose file to upload the expression data </p>'),accept = c('text/csv','text/comma-separated-values',
                                                                                                                                       'text/tab-separated-values','text/plain','.csv', '.tsv','.fasta')),
        
        selectInput("Signatures", HTML('<p style="color:#4A27C5; font-size: 12pt"> Genome signature </p>'), 
                    c("GC-content","RSCU","CGR","DI-nuculeotide odd Ratio","AAC","AMIP")),
        numericInput("num1",label = "FROM",value = 2),
        numericInput("num2", label = "TO", value = 3),
      
        useShinyalert(),
        actionButton("action", tags$b("Submit")), width = 3,  style="color: #fff; background-color: #337ab7; border-color: #2e6da4"),
      mainPanel(
       verbatimTextOutput("res1"),
        verbatimTextOutput("res2"),
       plotOutput("res3"),
       verbatimTextOutput("res4"),
       verbatimTextOutput("res5"),
       verbatimTextOutput("res6"),
        
      )
    )
  )
  
  server <- function(input, output, session) {
  
  
   
    
  
  df <- eventReactive(input$action, {
                      my_func <- switch(input$Signatures,
                                       
                                       "RSCU" = RSCU,
                                       "CGR"=cgr_res,
                                       "DI-nuculeotide odd Ratio"=zscore_cal,
                                       "GC-content" = GC_content7,
                                       "AAC"=extractAAC_mod,
                      )
                     
                      my_func(input$file$datapath)
                     
  })
  
  df1 <- eventReactive(input$actions, { 
                   my_func1 <- switch(input$signatures, 
                          "AMIP"=AMIP,
  )
     my_func1(input$file$datapath,input$num1,input$num2)
  })
  
  

  
  output$res1 <- renderPrint({
    req(input$Signatures)
    if (input$Signatures=="GC-content") {
      
      df()
    }
  })
  
  output$res2 <- renderPrint({
    req(input$Signatures)
    if (input$Signatures=="RSCU") {
      
      df()
    }
  })
    
  output$res3 <- renderPlot({
    req(input$Signatures)
    if (input$Signatures=="CGR") {
      
      df()
    }
  })
  
  output$res4 <- renderPrint({
    req(input$Signatures)
    if (input$Signatures=="DI-nuculeotide odd Ratio") {
      
      df()
    }
  })                    
  output$res5 <- renderPrint({
    req(input$Signatures)
    if (input$Signatures=="AAC") {
      
      df()
    }
  })
  
  output$res6 <- renderPrint({
   req(input$signatures)
    if(input$signatures=="AMIP"){
      df1()
      }
    
 })
  
  observeEvent(input$action, {
    shinyalert(title = "Please wait for the results...", type = "success")
  })
  
  }
  
  shinyApp(ui, server)

sir please look at my reply for @woodward, I explained things clearly, hope so it will make u clear things about

I think the easiest way to structure this is using if then:

output$res <- renderPrint({
  req(input$Signatures, input$file$datapath)
  if (input$Signatures=="GC-content") {
    GC_content7(input$file$datapath)
  } else if (input$Signatures=="RSCU") {
    RSCU(input$file$datapath)
  } else if ... etc.{

  }
})

i am encountering too many errors in it and the selectinput widget with combinations for some of the functions is not working, can u help me out with little modifications in my script, as sent by me earlier. I am feeling difficulty as im new to coding and R shiny too.

server <- function(input, output, session) {
  
  
   
    
  
  df <- eventReactive(input$action, {
                   my_func <- switch(input$Signatures,
                                     "AAC"= extractAAC_mod,
                                      "RSCU" = RSCU,
                                       "CGR"=cgr_res,
                                     "DI-nuculeotide odd Ratio"=zscore_cal,
                                     "GC-content" = GC_content7,
                    )
                   
                     my_func(input$file$datapath)
                  
  })
  
df1 <- eventReactive(input$actions, { 
                my_func1 <- switch(input$signatures, 
                        "AMIP"=AMIP,
)
    my_func1(input$file$datapath,input$num1,input$num2)
  })
  
  
 
  
     output$res1 <- renderPrint{
    req(input$Signatures, input$file$datapath)
    if (input$Signatures=="GC-content") {
      GC_content7(input$file$datapath)
    } else if (input$Signatures=="RSCU") {
      RSCU(input$file$datapath)
    } else if  (input$signatures=="DI-nuculeotide odd Ratio"){
      zscore_cal(input$file$datapath)
    }else if (input$signatures=="AAC"){
      extractAAC_mod(input$file$datapath)
    } 
    }
    
    
    
  
    
  output$res2 <- renderPlot{
    req(input$signatures, input$file$datapath)
    if (input$signatres=="CGR"){
      cgr_res(input$file$datapath)
    }
  }
    
  
  output$res3 <- renderPrint{
    req(input$signatures, input$file$datapath, input$num1, input$num2)
    if (input$signatures=="AMIP"){
      AMIP(input$file$datapath, input$num1, input$num2)
    }
  }
    
  

  
  observeEvent(input$action, {
    shinyalert(title = "Please wait for the results...", type = "success")
  })
  
  }
  
  shinyApp(ui, server)

Here is a reprex for one possible solution to this type of thing

library(shiny)
library(tidyverse)
ui <- fluidPage(
  
  sidebarLayout(sidebarPanel = 
                  div(
                    textInput("myx","type an x string","example"),
                    selectInput("funcselector", "select a func",
                                choices = c("funca","funcb")),
                    uiOutput("additionalInputs")
                  ),
                mainPanel = div(
                  verbatimTextOutput("resulttext")
                ))
)

server <- function(input, output, session) {
  
  myn <- reactiveVal(3)
  
  funca <- function(x){
    paste0("funca : ", x)
  }
  
  funcb <- function(x,n){
    paste0("funcb : ", rep(x,n))
  }
  
  output$additionalInputs <- renderUI({
    if(req(input$funcselector)=="funca"){}
    else if(req(input$funcselector)=="funcb"){
      return(numericInput("n","choose rep",value = myn(),min=1,max=5))
    }
    return(div())
  })
  
  observeEvent(input$n,
               myn(input$n))
  
  my_result <- reactive({
   switch (req(input$funcselector),
                        "funca" = funca(req(input$myx)),
                        "funcb" =  funcb(req(input$myx),req(input$n)))
  })
  output$resulttext <- renderPrint({
    req(my_result())
  })
}

shinyApp(ui, server)

@nirgrahamuk im new to programming and difficult understand the things. i understood few things not all sir, please elaborate with these input widget and input arguments for R functions.

 fileInput('file', HTML('<p style="color:#4A2768; font-size: 12pt"> Choose file to upload the expression data </p>'),accept = c('.fasta')),
        
        selectInput("Signatures", HTML('<p style="color:#4A27C5; font-size: 12pt"> Genome signature </p>'),  c("GC-content","RSCU","DI-nuculeotide odd Ratio","CGR","AAC","AMIP")),
        numericInput("num1",label = "FROM",value = 2),
        numericInput("num2", label = "TO", value = 3),
             useShinyalert(),
        actionButton("action", tags$b("Submit")), width = 3),
      mainPanel(
       verbatimTextOutput("res1"),
        verbatimTextOutput("res2"),
       plotOutput("res3"),
       verbatimTextOutput("res4"),
       verbatimTextOutput("res5"),
       verbatimTextOutput("res6"),
        
      )
    )

following are 6 R functions using for this webtool

GC-content=GC_content7(file),

RSCU= RSCU(file),

DI-nuculeotide odd Ratio= zscore_cal(file),

CGR= cgr_res(file),

AAC=extractAAC(file),

AMIP= AMIP(file,n1,n2)

these 6 R functions I need to tag in selectinput widget , whenever I select one combination from drop down button.

and in AMIP R function there are 3 arguments (file,n1,n2) file argument is taken by inputfile widget, and n1 and n2 I created numericalInput widget for giving values in webtool.
for first 5 functions I don't have any difficulty but for AMIP having multiple arguments I don't know what to write in serverreactive side please help me sir, I have to sbmit my project in my college

how to write the server reactive side according to the given ui

If you study my example you should come to understand the approach...
Also please understand from it that it is a reprex or reproducible example. Unfortunately in your own code share there are two problems.

  1. you provide too much. There are libraries included that are not used. If you have no problem with large part of your app then... why include it in your request for help. Limit your request to help to your own example which is no larger than to contain your problem
  2. you provide too little. You source your functions so we don't see their definitions. Any example for assistance debug here should provide data and all necessary parts to run and so in principle can be demonstratably fixed.

I hope you understand . You can find a guide to good reprex here.
FAQ: How to do a minimal reproducible example ( reprex ) for beginners

1 Like

Maybe structure it like this. Obviously i don't have access to your functions or your data so you will need to provide the details.


library("shinythemes")
library("shiny")
library("htmltools")
library("bsplus")
library("DT")
library("shinyalert")
library("shinyjs")
library("shinycssloaders")
library("dplyr")
library("data.table")
library("reshape2")
library("ggplot2")
library("plotly")
library("tools")
library("readxl")
library("writexl")
library("seqinr")
# library("Biostrings")
# library("BiocManager")
library("entropy")
library("protr")

# source("GS_Source.R")

ui = fluidPage(
  headerPanel(
    h1(tags$strong("Welcome to Web based Tool for Computation of Genomic"),
       style = "font-size: 20pt; line-height: 30pt; width = 100; color: #Red",
       align = "center"),
    windowTitle = "Home"),
  
  sidebarLayout(
    sidebarPanel(
      style = "background-color: #E52B50;",
      tags$style(type='text/css', 
                 "label { font-size: 12px; }",
                 ".selectize-input { font-size: 12pt; line-height: 12pt;}, 
                   .selectize-dropdown { font-size: 12pt; line-height: 12pt; }"),
      
      fileInput('file', HTML('<p style="color:#4A2768; font-size: 12pt"> Choose file to upload the expression data </p>'),accept = c('text/csv','text/comma-separated-values',
                                                                                                                                     'text/tab-separated-values','text/plain','.csv', '.tsv','.fasta')),
      
      selectInput("Signatures", HTML('<p style="color:#4A27C5; font-size: 12pt"> Genome signature </p>'), 
                  c("GC-content","RSCU","CGR","DI-nuculeotide odd Ratio","AAC","AMIP")),
      numericInput("num1",label = "FROM",value = 2),
      numericInput("num2", label = "TO", value = 3),
      
      useShinyalert(),
      actionButton("action", tags$b("Submit")), width = 3,  style="color: #fff; background-color: #337ab7; border-color: #2e6da4"),
    mainPanel(
      verbatimTextOutput("res"),
      plotOutput("plot")
    )
  )
)

server <- function(input, output, session) {
  
  # return text from selected function or ""
  output$res <- renderPrint({
    req(input$Signatures, input$file$datapath)
    if (input$Signatures=="RSCU") {
      RSCU(input$file$datapath)
    } else if (input$Signatures=="CGR") {
      cgr_res(input$file$datapath)
    } else if (input$Signatures=="DI-nuculeotide odd Ratio") {
      zscore_cal(input$file$datapath)
    } else if (input$Signatures=="GC-content") {
      GC_content7(input$file$datapath)
    } else if (input$Signatures=="AAC") {
      extractAAC_mod(input$file$datapath)
    } else if (input$Signatures=="AMIP") {
      AMIP(input$file$datapath, input$num1,input$num2)
    } else {
      ""
    }  
  })

  # return plot from selected function or NULL
  output$plot <- renderPlot({
    req(input$Signatures, input$file$datapath)
    if (input$Signatures=="PLOTFUNC") {
      plot(input$file$datapath) # put your plot here
    } else {
      NULL
    }  
  })
  
  observeEvent(input$action, {
    shinyalert(title = "Please wait for the results...", type = "success")
  })
  
}

shinyApp(ui, server)
1 Like

@woodward sir here are the functions u asked for

I wasn't asking for the functions. I was saying you have to do it.