Simplifying repetitive code

I am working on a Shiny app which take four separate .fcs files (large data file from flow cytometery), analysis them, and outputs a .pdf of eight graphs and .csv with multiple values. each file undergoes the exact same analysis. Currently, I have the same ~60 lines of code (with variations on the names) repeated four times. Is there a method to have unique inputs and outputs, but to call saved functions? As I add more inputs the debugging is becoming to much for me to handle.

I cannot post the server because of IP, but here is the UI:

ui <- fluidPage(

# Application title
titlePanel("BDS77 Analysis"),

# Sidebar with a slider input for number of bins 
sidebarLayout(
    sidebarPanel(
        
        
        fileInput(inputId ="BDS77_54file", label ="BDS77 CD54 FCS File", multiple = F, accept = ".FCS", buttonLabel ="Browse CD54"),
        fileInput(inputId ="BDS77_ISOfile", label ="BDS77 Isotype FCS File", multiple = F, accept = ".FCS", buttonLabel ="Browse Isotype"),
        fileInput(inputId ="Aph_54file", label ="Aph CD54 FCS File", multiple = F, accept = ".FCS", buttonLabel ="Browse CD54"),
        fileInput(inputId ="Aph_ISOfile", label ="Aph Isotype FCS File", multiple = F, accept = ".FCS", buttonLabel ="Browse Isotype"),
        
        radioButtons("filetype", "File type:",
                     choices = c("csv", "pdf")),
        downloadButton('downloadData', 'Download CSV'),
        downloadButton('export', 'Download PDF')
        
    ),
    
    
    # Show a plot of the generated distribution
    mainPanel(
        
        
        fluidRow(
            
            textOutput(outputId="info"),
            dataTableOutput("BDS77_data"),
            textOutput("BDS77_CD54"), 
            splitLayout(cellWidths =c("50%","50%"), plotOutput(outputId = "BDS77_CD54plotSSC"), 
                        plotOutput(outputId = "BDS77_CD54plotFL2")),
            
            
            textOutput("BDS77_ISO"),
            splitLayout(cellWidths =c("50%","50%"), plotOutput(outputId = "BDS77_ISOplotSSC"),
                        plotOutput(outputId = "BDS77_ISOplotFL2")),
            
            
            textOutput("Aph_CD54"),
            splitLayout(cellWidths =c("50%","50%"), plotOutput(outputId = "Aph_CD54plotSSC"),
                        plotOutput(outputId = "Aph_CD54plotFL2")),
            
            
            textOutput("Aph_ISO"),
            splitLayout(cellWidths =c("50%","50%"), plotOutput(outputId = "Aph_ISOplotSSC"),
                        plotOutput(outputId = "Aph_ISOplotFL2"))
            
            
            
            
        )
    ))

)

Have a look at modules:

https://shiny.rstudio.com/articles/modules.html

2 Likes

That is exactly what I was looking for. Now I just have to figure out how to make it work!

1 Like