title: Select appropriate columns from table based on multiple input
body: <p>I built a small app in <strong>R</strong>'s shiny package that displays some columns from selected dataframe based on the input IDs/Names. My dummy data looks like this (code below):</p>
  <pre><code>ID1 ID2 ID3 ID4  Client Amount
  1   NA  333 3344 John   100
  1   88  NA  3344 John   200
  1   86  777 8888 Mike   300
  3   66  987 4545 Dyke   400
  4   11  123 3636 Vike   500
  </code></pre>
  <p>Notice, that ID1 may have more than one record per ID, as well as ID4 and Client but multiple records with same ID4 or Client can not have different ID1. Ideally, I would like to manipulate data on server side based on ID1 or ID4 (other records can be matched to both of them).</p>
  <p>So I built 6 inputs, 4 numeric inputs for IDs, and 2 for text inputs for Client name (list, and text input), and would like to do the following:</p>
  <blockquote>
  <p>if there is no input for ID1, take the last input in order (eg. if there is input for Client text, Client list, ID2 and ID3 select ID3), and match it to ID4 unless its ID4.</p>
  <p>then if there is input for ID1 output table based on ID1 input, and if no input for ID1 then output table based on ID4.</p>
  </blockquote>
  <p>My only solution is to &quot;bruteforce&quot; it, as I am novice in programming, but because I want to display 20 tables, it would be crazy code (I know) and I guess there must be an elegant solution to it. Code&gt;</p>
  <p><strong>ui.R:</strong></p>
  <pre class="lang-r prettyprint-override"><code>#ui.R
  library(shiny)
  dataset = data.frame(ID1 = c(1,1,1,3,4), ID2 = c(NA,88,86,66,11), 
                       ID3 = c(333,NA,777,987,123), ID4 = c(3344,3344,8888,4545, 3636), 
                       Client = c(&quot;John&quot;, &quot;John&quot;, &quot;Mike&quot;, &quot;Dyke&quot;, &quot;Vike&quot;), 
                       Amount = c(100,200,300,400,500))
  
  shinyUI(bootstrapPage(
      headerPanel(&quot;Tabsets&quot;),
      sidebarPanel(
          textInput('clientN', 'Client Name'),
          selectInput('client', 'Client', c('None','John','Mike', 'Dyke', 'Vike')),
          numericInput('id2', 'ID 2'),
          numericInput('id3', 'ID 3'),
          numericInput('id4', 'ID 4'),
          numericInput('id1', 'ID 1')
      ),
      mainPanel(
          tabsetPanel(
              tabPanel(&quot;1&quot;, tableOutput(&quot;tableA&quot;)),
              tabPanel(&quot;2&quot;, tableOutput(&quot;tableA&quot;))
      ))))
  </code></pre>
  <p><strong>server.R</strong></p>
  <pre class="lang-r prettyprint-override"><code>#server.R
  library(shiny)
  dataset = data.frame(ID1 = c(1,1,1,3,4), ID2 = c(NA,88,86,66,11), 
                       ID3 = c(333,NA,777,987,123), ID4 = c(3344,3344,8888,4545, 3636), 
                       Client = c(&quot;John&quot;, &quot;John&quot;, &quot;Mike&quot;, &quot;Dyke&quot;, &quot;Vike&quot;), 
                       Amount = c(100,200,300,400,500))
  
  shinyServer(function(input, output) {
  
      select &lt;- reactiveTable(function() {
          sel &lt;- 0
          if (input$clientN != NA)
              sel &lt;- 1
          if (input$client != 'None')
              sel &lt;- 2
          if (input$id2 &gt; 0)
              sel &lt;- 3
          if (input$id3 &gt; 0)
              sel &lt;- 4
          if (input$id3 &gt; 0)
              sel &lt;- 5
          if (input$id1 &gt; 0)
              sel &lt;- 6
          sel
      })
  
      output$tableA &lt;- reactiveTable(function() {
          if(select == 0)
              table &lt;- dataset
  
          if(select == 1)
              table = dataset[dataset$Client == input$clientN, c('Client','Amount')]
  
          if(select == 2)
              table = dataset[dataset$Client == input$client, c('Client','Amount')]
  
          if(select == 3)
              table = dataset[dataset$ID2 == input$id2, c('Client','Amount')]
  
          if(select == 4)
              table = dataset[dataset$ID3 == input$id3, c('Client','Amount')]
  
          if(select == 5)
              table = dataset[dataset$ID4 == input$id4, c('Client','Amount')]
  
          if(select == 6)
              table = dataset[dataset$ID1 == input$id1, c('Client','Amount')]
  
      table
      })
  })
  </code></pre>
  <hr />
  <p>But how do I actually make in one function whether input exists in ID1 or other Inputs, and if only in Inputs other than ID1 map them to ID4, and then in another function output table by ID4 unless there is input for ID1 in which case output table by ID1?</p>
  <p>I think this is also general programming problem rather than language specific or package specific, so if you could explain in anyway, I could implement in <strong>R</strong>.</p>