generating tables with sequential numbers in shiny

I am trying to put together an app to generate a table containing ceratin information that can then be fed as a .csv to a barcode printer.
I am sure I am probably missing something really straight forward but I am struggling to get my shiny app to generate sequential numbers when generating the table. It should generate sequential numbers from 1 to the number of aliquots being generated but it numbers them all as 1.

I have attached a reprex below:

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)

db_protein <- data.frame(
  SysID = "EG1",
  Name = "batchcode example_prot",
  Alternative_name = NA,
  Description = NA,
  Created_at = NA,
  Owner = "Scientist",
  Protein = "example prot",
  Batch = "batchcode",
  Date_Created = NA,
  Aliquot = 1,
  Subaliquot = 1,
  Subsubaliquot = 1,
  Barcode = NA)

mod_Proteins_ui <- function(id){
  ns <- NS(id)
  
  shinyjs::useShinyjs()
  
  tagList(
    h1("Protein Inventory Barcoding"),
    fluidRow(
      shinydashboard::box(
        title="Generate New Barcode",
        width=10,
        selectizeInput(ns('nb_prt_protein'), "Protein Name", choices = unique(db_protein$Protein), options = list(create=TRUE)),
        selectizeInput(ns('nb_prt_batch'), "Protein Batch", choices = unique(db_protein$Batch), options = list(create=TRUE)),
        dateInput(ns('nb_prt_date'), "Date of production"),
        
        checkboxInput(ns('Gen_A'), "Generate Aliquots"),

        conditionalPanel(
          condition = paste0("input['",ns("Gen_A"),"'] == true"),
          numericInput(ns('nb_prt_multipleA'), "Number of Aliquots Produced",0),
        ),

        conditionalPanel(
          condition = paste0("input['",ns("Gen_A"),"'] == true || input['",ns("Gen_B"),"'] == true || input['",ns("Gen_C"),"'] == true"),
          actionButton(ns('GenBarcodes'), "Generate New Barcodes")
        ),
        
      )
    ),
    fluidRow(
      conditionalPanel(
        condition = paste0("input['",ns("GenBarcodes"),"'] != 0"),
        shinydashboard::box(
          title="New Barcodes Generated",
          width=10,
          DT::dataTableOutput(ns('barcode_export'))
        )
      )
    )
  )
}

Proteins Server Function

@noRd

mod_Proteins_server <- function(input, output, session){
  ns <- session$ns
  
  output$value <- renderText({input$caption})
  
  n_newbarcodes <- reactive({
    as.integer(ifelse(input$Gen_A==TRUE,input$nb_prt_multipleA,
                      ifelse(input$Gen_B==TRUE,input$nb_prt_multipleB,
                             ifelse(input$Gen_C==TRUE,input$nb_prt_multipleC,0))))
  })
  
  newbarcodes <- reactive({
    DT::datatable(
      data.frame(
        SysID = rep(NA,n_newbarcodes() ),
        Name = rep(paste0(input$nb_prt_batch," ",input$nb_prt_protein),n_newbarcodes() ),
        Alternative_Name = rep(NA,n_newbarcodes() ),
        Description = rep(NA,n_newbarcodes() ),
        Created_at = rep(NA,n_newbarcodes() ),
        Owner = rep(NA,n_newbarcodes() ),
        Protein = rep(input$nb_prt_protein,n_newbarcodes() ),
        Batch = rep(input$nb_prt_batch,n_newbarcodes() ),
        Date_Created = rep(input$nb_prt_date,n_newbarcodes() ),
        Aliquot = ifelse(input$Gen_A==TRUE,seq_len(input$nb_prt_multipleA),rep(input$nb_prt_aliquot,n_newbarcodes() )),
        Barcode = rep(NA,n_newbarcodes() )
      )
    )
  })
  
  output$barcode_export = DT::renderDT({ newbarcodes() })
  
}


# Define UI for application that draws a histogram
ui <- fluidPage(
  
  mod_Proteins_ui("Proteins_ui_1")
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  callModule(mod_Proteins_server, "Proteins_ui_1")
}

# Run the application 
shinyApp(ui = ui, server = server)
#> 
#> Listening on http://127.0.0.1:3624
#> Warning: Error in rep: invalid 'times' argument

Created on 2021-05-06 by the reprex package (v0.3.0)

maybe

 Aliquot = if(input$Gen_A) {seq_len(input$nb_prt_multipleA)}else{rep(input$nb_prt_aliquot,n_newbarcodes() )},

YES, thank-you, that works like a charm, not sure what shiny has against ifelse statements but very happy that is now working.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.