title: In R Shiny, how to create a function for formatting numbers in renderTable output?
body: <p>The below MWE code formats the numeric render table outputs. However you can see in the MWE that quite a bit of formatting code is repeated in (i) generating the default table that appears when first invoking the App, and (ii) generating the table after having clicked the &quot;Modify&quot; action button.</p>
  <p>So I created a function <code>formatDF1</code> that captures this repeated code. But it doesn´t work! When I comment out the section of code between &quot;BEGIN TEST...&quot; and &quot;END TEST&quot; below, and un-comment out <code>formatDF1(df)</code> immediately above &quot;BEGIN TEST...&quot;, the <code>formatDF1</code> function is completely ignored and the default table is generated without the desired numeric formats.</p>
  <p>What am I doing wrong?</p>
  <p>MWE code:</p>
  <pre><code>library(shiny)
  library(shinyMatrix)
  library(shinyjs)
  
  # Function that I'm trying to implement...
  formatDF1 &lt;- function(x){
    dfA &lt;- format(x[1,],nsmall=0)
    dfB &lt;- format(x[2,],nsmall=0)
    dfC &lt;- paste(format(x[3,],nsmall=2),'%')
    dfD &lt;- paste(format(x[4,],nsmall=2),'%')
    x &lt;- rbind(x[0,],dfA,dfB,dfC,dfD)
    n &lt;- dim(x)[2]
    colnames(x) &lt;- paste(&quot;Series&quot;, 1:n)
    rownames(x) &lt;- matrix3Headers()
  }
  
  matrix3Headers &lt;- function(){
    c('Issuance period',
      'Scheduled amort period',
      '3m average XS trigger',
      'Percentage of capital')}
  
  # Assigns default values to first column of input matrix grid
  matrix3Default &lt;- matrix(
    c( # Below are default values for table1
        1,
        24,
        0,
        100
      ), # close concatenate
      4, # specify default number of matrix rows
      1, # specify default number of matrix columns
      dimnames=list(matrix3Headers(), NULL)
    ) # close matrix function
  
  # Automatically assigns names to column headers
  colnames(matrix3Default) &lt;- paste0(&quot;Series &quot;,1:ncol(matrix3Default))
  
  # Matrix input function
  matrix3Input &lt;- function(x, matrix3Default){
    matrixInput(x,
                label =  'Input series terms into below grid:',
                value =  matrix3Default, 
                rows  =  list(extend=FALSE,names=TRUE), 
                cols  =  list(extend=TRUE,names=TRUE,editableNames=FALSE,delete=TRUE),
                class =  'numeric'
    ) # close matrix input
  } # close function
  
  ui &lt;- fluidPage(
    useShinyjs(),
    titlePanel('Inputs'),
    fluidRow(actionButton('modify','Modify'),
             tableOutput('table1'))
  ) # close fluid page
  
  server &lt;- function(input, output, session){
    
    rv &lt;- reactiveValues(
      mat3=matrix3Input('matrix3',matrix3Default),
      input=matrix3Default,
      colHeader = colnames(input)
    ) # close reactive values
    
    observeEvent(input$modify,{
      showModal(modalDialog(
        rv$mat3
      )) # close shown modal and modal dialog
    }) # close observe event
    
    output$table1 &lt;- renderTable({
      
      if(!isTruthy(input$modify)){ # &lt;&lt; Generates default table when first invoking the App
        df &lt;- matrix3Default
        # formatDF1(df)
        # BEGIN TEST &gt;&gt;&gt; Below indented code adds numeric formats to table output
          dfA &lt;- format(df[1,],nsmall=0)
          dfB &lt;- format(df[2,],nsmall=0)
          dfC &lt;- paste(format(df[3,],nsmall=2),'%')
          dfD &lt;- paste(format(df[4,],nsmall=2),'%')
          df &lt;- rbind(df[0,],dfA,dfB,dfC,dfD)
  
        n &lt;- dim(df)[2]
        colnames(df) &lt;- paste(&quot;Series&quot;, 1:n)
        rownames(df) &lt;- matrix3Headers()
        # &lt;&lt;&lt; END TEST
        }
      else{ # &lt;&lt; Generates table after user clicks &quot;Modify&quot; action button
        req(input$matrix3)
        rv$mat3 &lt;- matrix3Input('matrix3',input$matrix3)
        df &lt;- input$matrix3
        
        # Below indented code adds numeric formats to table output  
          dfA &lt;- format(df[1,],nsmall=0)
          dfB &lt;- format(df[2,],nsmall=0)
          dfC &lt;- paste(format(df[3,],nsmall=2),'%')
          dfD &lt;- paste(format(df[4,],nsmall=2),'%')
          df &lt;- rbind(df[0,],dfA,dfB,dfC,dfD)
        
        n &lt;- dim(df)[2]
        colnames(df) &lt;- paste(&quot;Series&quot;, 1:n)
        rownames(df) &lt;- matrix3Headers()
        rv$input &lt;- df
      } # close else
      df
    },rownames=TRUE, colnames=TRUE) # close output$table1
    
  } # close server
  
  shinyApp(ui, server)
  </code></pre>