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?
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:
p-value rather than input$p-valuep-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.data.frame in the call to renderDataTable(...) as if it was a variable.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.rowSums(...) doesn't work that way.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With