-1

I've created a shiny app where the user uploads a file with data. The dataframe is edited slightly on the first tab of the app and is called exp_dff(). On the second page of the app, I've created an action button which renders UI that allows a user to select columns from the table to create experimental/control groups, e.g. Experimental Group 1 = column 1 + 2 + 3, etc. Users can either create experimental or control groups. I'm running into a bit of an odd issue--the code itself works completely fine if I either have the code for the Experimental option OR the control option. But if both buttons exist, for whatever reason it tells me that the Experimental option has argument of length 0. Does anyone know why this is happening?

library(dplyr)
library(viridis)
library(tidyverse)
library(rio)
library(shiny)
library(missForest)
library(KSEAapp)

options(shiny.maxRequestSize = 30*1024^2)

ui <- navbarPage("App Name",
                 tabPanel("File Upload",
                 sidebarLayout(
                   sidebarPanel(
                     fileInput("file1",
                               "Choose File",
                               multiple = FALSE,
                               accept = c("text/csv",
                                          "text/tab-separated-values",
                                          ".csv",
                                          "text/tsv",
                                          "text/comma-separated-values",
                                          ".tsv")),
                     placeholder = "No file selected",
                     tags$hr(),
                     selectInput("dftype", label = "Data Type",
                                 choices = c(Phosphoproteome = "pProt",
                                             Proteome = "Prot"),
                                 selected = "Prot"),
                     tags$hr()
                   ),
                   mainPanel(
                     dataTableOutput(outputId = "exp_df")
                   )
                 )),
tabPanel("Data Processing",
           sidebarPanel(
             tags$h1(tags$b("Select Groups")),
             uiOutput("cr_exp_gr"),
             actionButton("cr_exp", "Create new experimental group"),
             tags$hr(),
             uiOutput("cr_con_gr"),
             actionButton("cr_con", "Create new control group"),
             tags$hr(),
             tags$h1(tags$b("Null Value Settings")),
             tags$p(tags$em("How many null values are permitted? Null value threshold can be set per table, per row, or per experimental group. Rows not meeting the threshold will be removed. Rows meeting the threshold will have null values imputed.")),
             tags$p(tags$em("Rows must satisfy a minimum number of non-null values. Rows not meeting the threshold will be filtered out. What percentage of values must be present (not null)?")),
             sliderInput("thresh", label = "Null Value Threshold",
                         min = 0, max = 100,
                         value = 100),
             tags$hr(),
             tags$p(tags$em("Null Value Threshold applies per row. Would you like to consider groups separately?")),
             selectInput("threshmode", label = "Threshold Mode",
                            choices = c("In Each Row (Default)" = "row", "Per Group" = "grp", "In At Least One Group" = "grp+"),
                         selected = "row"),
             tags$hr(),
             selectInput("imp_method", label = "Imputation Method",
                         choices = c("Normal Distribution" = "nd", "Random Forest" = "rf"),
                         selected = "nd"),
             tags$hr()
           )
         ),
         mainPanel(
           dataTableOutput(outputId = "proc_df")
         ))

server <- function(input, output, session) {
  mydf <- eventReactive(input$file1,{
    infile <- input$file1
    req(infile)
    return(import(infile$datapath))
  })
  exp_dff <- reactive({
    req(mydf(),newvar(),nv(),nvar())
    return(
      cbind(
        mydf(),
        Residue = nv(),
        Position = nvar(),
        Protein_id = newvar())
    )
  })

  nv <- reactive({
    req(mydf())
    eI <- mydf()$Index
    gsub('[0-9]*','', sub(".*?_(.*?)$", "\\1",eI))
  })

  nvar <- reactive({
    req(mydf())
    eI <- mydf()$Index
    gsub('[A-Z.-]','', sub(".*?_(.*?)$","\\1", eI))
  })

  newvar <-reactive({
    req(mydf())
    eI <- mydf()$Index
    sub("(.*?)_(.*?)$","\\1",eI)
  })

  output$exp_df <- renderDataTable({
    exp_dff()
  })

  outputOptions(output, "exp_df", suspendWhenHidden=FALSE)

  values <- reactiveValues(
    numExpCr = 1
  )

  observeEvent(input$cr_exp, {
    values$numExpCr <- values$numExpCr + 1
  })

  output$cr_exp_gr <- renderUI({
    crExp_list <- lapply(1:values$numExpCr, function(i) {
      expname <- paste("Exp", i, sep="")
      selectizeInput("sel_exp", label = "Select samples for new experimental group", choices = colnames(exp_dff()), options = list(create=TRUE), multiple = TRUE)
    })

    do.call(tagList, crExp_list)
  })

  observeEvent(values$cr_exp, {
    for (i in 1:values$cr_exp) {
      df1 <- data.frame(input$sel_exp)
      assign(paste("Exp", i, sep="_"),df1)
    }})

  values <- reactiveValues(
    numConCr = 1
  )

  observeEvent(input$cr_con, {
    values$numConCr <- values$numConCr + 1
  })

  output$cr_con_gr <- renderUI({
    crCon_list <- lapply(1:values$numConCr, function(i) {
      conname <- paste("Con", i, sep="")
      selectizeInput("sel_con", label = "Select samples for new control group", choices = colnames(exp_dff()), options = list(create=TRUE), multiple = TRUE)
    })

    do.call(tagList, crCon_list)
  })

  observeEvent(values$cr_con, {
    for (i in 1:values$cr_con) {
      df2 <- data.frame(input$sel_con)
      assign(paste("Con", i, sep="_"),df2)
    }})

}


shinyApp(ui=ui, server=server)
lsoch
  • 17
  • 2
  • Hi, the code isn't autogenerated--I wrote it myself. – lsoch Feb 21 '22 at 06:32
  • Hi, could you make a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? All the code here is probably not necessary to reproduce the issue. Also, it would be good if you could adapt your code to use some data that everybody has. We don't know what type of data we need to upload and it's probably irrelevant for the problem here – bretauv Feb 21 '22 at 10:22
  • You are defining `values` as a `reactiveValues` object two times. Instead, define it once as `values <- reactiveValues( numExpCr = 1, numConCr = 1 )`. Also, where are `values$cr_exp` and `values$cr_con` defined? It is best to define them before using them in `observeEvent(...)` – YBS Feb 21 '22 at 12:11

1 Answers1

-1

Thanks for all the suggestions: I managed to resolve the issue with your help. I'm going to post the solution in case anyone in the future has a similar problem:

1)I mistakenly was using values$cr_exp and values$cr_con in some places instead of values$numExpCr and values$numConCr. 2) defining values <- reactiveValues(numExpCr=1, numConCr=1) fixed the problem

lsoch
  • 17
  • 2