title: Conditional Output Shiny UI
body: <p>I have survey data. I want to use Shiny to share results of my univariate and bivariate analyses with collaborators. In the survey there are numeric and factor variables. Depending on whether the person viewing the Shiny applications is interested in univariate/bivariate summaries, and depending on the variable type(s) they want to summarize I want different output to appear. </p>
  
  <p>Specifically,</p>
  
  <p>i) If univariate and numeric then display:</p>
  
  <ul>
  <li>Item response rate: <code>length() - sum(is.na())</code> </li>
  <li><code>hist()</code> </li>
  <li><code>summary()</code></li>
  </ul>
  
  <p>ii) If univariate and factor then display:</p>
  
  <ul>
  <li>Item response rate</li>
  <li><code>barplot()</code></li>
  <li><code>table()</code></li>
  <li><code>prop.table()</code></li>
  </ul>
  
  <p>iii) If bivariate and numeric*numeric then display:</p>
  
  <ul>
  <li>Item response rate</li>
  <li>Scatter graph: <code>plot(x,y)</code></li>
  <li><code>summary(x)</code></li>
  <li><code>summary(y)</code></li>
  <li><code>cor(x,y,method="spearman")</code></li>
  </ul>
  
  <p>iv) If bivariate and factor*factor then display:</p>
  
  <ul>
  <li>Item response rate</li>
  <li>Bar Chart...something like "rCharts nvd3 multiBarChart"</li>
  <li><code>table(x,y)</code></li>
  <li><code>prop.table(x,y)</code></li>
  <li><code>chisq.test(x,y)</code></li>
  </ul>
  
  <p>v) If bivariate and (factor*numeric OR numeric*factor ) then display:</p>
  
  <ul>
  <li>Item response rate</li>
  <li>boxplot</li>
  <li>summary of numeric variable by factor variable: <code>by(numeric, factor, summary)</code></li>
  <li>Kruskal Wallis Test <code>kruskal.test(numeric ~ factor)</code></li>
  </ul>
  
  <p>Currently, I have code to generate the desired output for all 5 steps as separate applications. I want to bring them together into 1 Shiny app. I am struggling conceptually with how to set up the <code>mainPanel()</code> display to be reactive to the different output that it will receive as a function of the choices the user is making on the <code>sidebarPanel()</code> UI. </p>
  
  <p>Specifically,</p>
  
  <ul>
  <li>How to change <code>mainPanel()</code> UI headers to reflect different outputs</li>
  <li>How to conceptually expand my code below to include multiple pieces of output (i.e. Below code works for a single piece <code>verbatimTextOutput()</code> but I don't know how to proceed for the multiple pieces/types of output I want to display as discussed in (i-iv) above. e.g. Text, tables, plots.  </li>
  </ul>
  
  <p>Below is my code for the ui.R file:</p>
  
  <pre><code>library(shiny)
  shinyUI(pageWithSidebar(
  headerPanel("Shiny Example"),
  sidebarPanel(
  wellPanel(
  selectInput(inputId = "variable1",label = "Select First Variable:", 
  choices = c("Binary Variable 1" = "binary1",
  "Binary Variable 2" = "binary2", 
  "Continuous Variable 1" = "cont1",
  "Continuous Variable 2" = "cont2"),
  selected = "Binary Variable 1"
  )
  ),
  
  wellPanel(
  checkboxInput("bivariate", "Proceed to Bivariate Analysis", FALSE),
  conditionalPanel(
  condition="input.bivariate==true",
  selectInput(inputId = "variable2", 
  label = "Select Second Variable:",
  choices = c("Binary Variable 1" = "binary1",
  "Binary Variable 2" = "binary2", 
  "Continuous Variable 1" = "cont1",
  "Continuous Variable 2" = "cont2"),
  selected = "Binary Variable 2"
  )
  )
  )
  ),
  
  mainPanel(
  h5("Output"),
  verbatimTextOutput("out")
  )
  ))
  </code></pre>
  
  <p>Below is my simulated data and my server.R file:</p>
  
  <pre><code>binary1 &lt;- rbinom(100,1,0.5)
  binary2 &lt;- rbinom(100,1,0.5)
  cont1   &lt;- rnorm(100)
  cont2   &lt;- rnorm(100)
  
  dat &lt;- as.data.frame(cbind(binary1, binary2, cont1, cont2))
  
  dat$binary1 &lt;- as.factor(dat$binary1)
  dat$binary2 &lt;- as.factor(dat$binary2)
  dat$cont1 &lt;- as.numeric(dat$cont1)
  dat$cont2 &lt;- as.numeric(dat$cont2)
  
  library(shiny)
  library(rCharts)
  
  shinyServer(function(input, output) {
  
  inputVar1 &lt;- reactive({
  parse(text=sub(" ","",paste("dat$", input$variable1)))
  })
  
  inputVar2 &lt;- reactive({
  parse(text=sub(" ","",paste("dat$", input$variable2)))
  })
  
  output$out &lt;- renderPrint({
  
  if ( (input$bivariate==FALSE) &amp; (is.factor(eval(inputVar1()))==TRUE) ) {
  table(eval(inputVar1()))
  } else {
  
  if ( (input$bivariate==FALSE) &amp; (is.numeric(eval(inputVar1()))==TRUE) ) {
  summary(eval(inputVar1()))
  } else {
  
  if ( (input$bivariate==TRUE) &amp; (is.factor(eval(inputVar1()))==TRUE) &amp; (is.factor(eval(inputVar2()))==TRUE) ) {
  table(eval(inputVar1()), eval(inputVar2()))
  } else {
  
  if ( (input$bivariate==TRUE) &amp; (is.numeric(eval(inputVar1()))==TRUE) &amp; (is.numeric(eval(inputVar2()))==TRUE) ) {
  cor(eval(inputVar1()), eval(inputVar2()))
  } else {
  
  if ( (input$bivariate==TRUE) &amp; (is.factor(eval(inputVar1()))==TRUE) &amp; (is.numeric(eval(inputVar2()))==TRUE) ) {
  by(eval(inputVar2()), eval(inputVar1()), summary)
  } else { 
  
  if ( (input$bivariate==TRUE) &amp; (is.numeric(eval(inputVar1()))==TRUE) &amp; (is.factor(eval(inputVar2()))==TRUE) ) {
  by(eval(inputVar1()), eval(inputVar2()), summary)
  }
  }
  }
  }
  }
  }
  
  })
  
  })
  </code></pre>
  
  <p>Any help you could provide would be greatly appreciated. Even simply showing how to adjust the code to render two pieces of desired output given choices of variables. And how to adjust headers to reflect named pieces of output. </p>
  
  <p>Thanks in advance...Chris</p>