Skip to content

Commit abce0e2

Browse files
committed
Added export functionality (.csv, .txt, .tsv) in web app
1 parent 82e02ef commit abce0e2

File tree

4 files changed

+487
-197
lines changed

4 files changed

+487
-197
lines changed

Block_Randomization/Block_randomization_web_app.R

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Documentation -----------------------------------------------------------
22

3-
# Web app to produce block randomization trials
3+
# Shiny web app to produce block randomization trials
44
# Author: Deepak Sharma
55
# Computer Programmer, JPNATC - AIIMS
66
# Date: 28 March, 2017
@@ -13,59 +13,47 @@ library(psych)
1313
# User interface ----------------------------------------------------------
1414

1515
ui <- fluidPage(
16-
titlePanel("Block randomization in R"),
17-
sidebarLayout(
18-
sidebarPanel(
19-
sliderInput(
20-
"trials",
21-
"Number of trials:",
22-
min = 1,
23-
max = 300,
24-
value = 150
25-
),
26-
sliderInput(
27-
"blocks",
28-
"Number of blocks:",
29-
min = 1,
30-
max = 10,
31-
value = 3
32-
),
33-
sliderInput(
34-
"cases",
35-
"Number of cases:",
36-
min = 1,
37-
max = 4,
38-
value = 2
39-
),
40-
downloadButton('downloadData', 'Download')
41-
),
42-
43-
# Show result table
44-
mainPanel(tableOutput("filetable"))
45-
46-
))
47-
16+
titlePanel('Block randomization in R'),
17+
sidebarLayout(
18+
sidebarPanel(
19+
h3("Instructions"),
20+
h6("For 150 trials enter trials = 150"),
21+
h6("Factor = Desired block trials / number of cases"),
22+
h6("Example: To produce a block of 6 trials"),
23+
h6("Enter factor = 3 and cases = 2, since 3 * 2 = 6 "),
24+
textInput("trials", "Number of trials ","150"),
25+
textInput("factor", "Number of factor ","3"),
26+
textInput("cases", "Number of cases ","2"),
27+
radioButtons("filetype", "Save as ", choices = c("txt", "csv", "tsv")),
28+
downloadButton('trials', 'Download trials')
29+
),
30+
mainPanel(tableOutput('table')
31+
)
32+
)
33+
)
4834

4935
# Server ------------------------------------------------------------------
5036

51-
server <- function(input, output)
52-
{
53-
output$filetable <- renderTable({
54-
condition <-
55-
block.random(n = input$trials, c(block = input$blocks, drug = input$cases))
37+
server <- function(input, output){
38+
datasetInput <- reactive({
39+
condition <- block.random(as.numeric(input$trials), c(as.numeric(input$factor), as.numeric(input$cases)))
5640
output <- data.frame(condition)
5741
colnames(output) <- c("Block", "to_be_discarded", "Case")
5842
data <- output[,-2]
43+
})
44+
output$table <- renderTable({
45+
datasetInput()
5946
})
60-
data <- mtcars
61-
output$table <- renderTable({
62-
datasetInput()
63-
})
64-
output$downloadData <- downloadHandler(
47+
48+
output$trials <- downloadHandler(
6549
filename = function() {
66-
paste("trials", '.txt', sep='')},
67-
content = function (file){
68-
write.csv(data,file)
50+
paste(input$filetype, sep = ".")
51+
},
52+
53+
# Write content in file
54+
content = function(file) {
55+
sep <- switch(input$filetype, "csv" = ",", "tsv" = "\t","txt" = " ")
56+
write.table(datasetInput(), file, sep = sep, row.names = FALSE)
6957
}
7058
)
7159
}

0 commit comments

Comments
 (0)