3
votes

I'm building a Shiny app that reads in a csv and then displays three shiny dataTables, each displayed in its own tabPanel; i'm having trouble figuring out how to handle the third table.

The input csv contains values that look like

0, 1, 2, 3, 4, 5
0.01, 0.02, 0.11, 0.00, 0.1
.
.
.

I'm trying to display a table that displays the values, and an additional column, that tally's row values that are smaller than a slider predicate. So if I set a slider value of 0.05 the desired output would be

tally, 0, 1, 2, 3, 4, 5
3, 0.01, 0.02, 0.11, 0.00, 0.1

In UI.r I've tried (excluding non-relevant code)

sidebarPanel(
        sliderInput("p-value", "P-Value Adjustment:",
                    min = 0.01, max = 0.1, value = 0.05, step = 0.01),
        ),

mainPanel(
        #excluding two other panels that work correctly 
        tabsetPanel(
            tabPanel("Interactive-Data",
                     dataTableOutput("interactive.table"))
          )
     )

and then in Server.r

shinyServer(function(input, output) {
   value.frame <- read.csv("path/to/file")

   sliderValues <- reactive({

    input.frame <- data.frame(
        Name = c("p-value"),
        Value = c(input$p-value))

    data.frame(cbind(input.frame, value.frame, stringsAsFactor=FALSE))

    })

#I'm lost here
output$interactive.table = renderDataTable({
    data.frame$tally <- rowSums(data.frame < p-value)
    data.frame

    })

I'm getting lost with how to use the input from the sliderInput to dynamically recalculate the value of the interactive.table$tally column. Is renderDataTable appropriate or is there another way to go about this?

1
In the output$interactive.table I would think that just having value.frame$tally <- rowSums(value.frame < input$p-value would be sufficient. Why the extra reactive function? - harkmug

1 Answers

3
votes

This seems to do what you are asking:

library(shiny)
shinyUI = pageWithSidebar(
  headerPanel("Title"),
  sidebarPanel(
    sliderInput("p.value", "P-Value Adjustment:",
                min = 0.01, max = 0.1, value = 0.05, step = 0.01)),
  mainPanel(
    tabsetPanel(
      tabPanel("Interactive-Data",
               dataTableOutput("interactive.table"))))
)
shinyServer = function(input,output){
  value.frame <- data.frame(matrix(round(runif(60,0,0.2),2),ncol=6))
  output$interactive.table <- renderDataTable({
    tally <- sapply(1:nrow(value.frame), function(i)
             rowSums(value.frame[i,2:ncol(value.frame)] < input$p.value,na.rm=T))
    df <- data.frame("p-value"=input$p.value,tally,value.frame)
    colnames(df)[3:8] <- c(as.character(0:5))
    df
  })}
runApp(list(
  ui = shinyUI,
  server = shinyServer
))

There are several problematic aspects of your code. Here are a few:

  1. Referencing p-value rather than input$p-value
  2. Using p-value as a variable name. R thinks input$p-value is input$p minus value. You can get around this by using input$"p-value" but this makes the code almost unintelligible, so I changd p-value to p.value.
  3. You reference data.frame in the call to renderDataTable(...) as if it was a variable.
  4. Most of the functions that create or return data frames prepend X to any column names that start with a number, so if you insist on numbers for column names you have to get rid of those X's.
  5. rowSums(...) doesn't work that way.