-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.R
118 lines (103 loc) · 3.97 KB
/
app.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
library(shiny)
library(DT)
library(anonymizer)
ui <- fluidPage(
fluidRow(
column(3,
wellPanel(
fileInput('file', 'Choose file to upload',
accept = c(
'text/csv',
'text/comma-separated-values',
'text/tab-separated-values',
'text/plain',
'.csv',
'.tsv'
)
),
# Taken from: http://shiny.rstudio.com/gallery/file-upload.html
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
','),
radioButtons('quote', 'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
'"'),
################################################################
actionButton("choice", "Parse file")
),
wellPanel(
h3("Anonymizer Settings"),
p("Uses package", a(href = "https://cran.r-project.org/package=anonymizer", "anonymizer", target = "_blank"), "by Paul Hendricks"),
selectInput("anon_algo", label = "Algorithm",
choices = c("md5", "sha1", "crc32", "sha256", "sha512",
"xxhash32", "xxhash64", "murmur32"),
selected = "sha256"
),
numericInput("anon_seed", label = "Set Seed", 0),
numericInput("anon_nchars", label = "Number of characters to salt", 5)
),
wellPanel(
checkboxGroupInput("columns", "Select Columns", choices = NULL) # no choices before uploading
),
wellPanel(
downloadButton('downloadData', 'Download Anonymized Data')
)
),
column(9,
h3("Raw Data:"),
DT::dataTableOutput("rawdata"),
h3("Anonymized Data:"),
DT::dataTableOutput("data2")
)
)
)
server <- function(input, output, session) { # added session for updateSelectInput
info <- eventReactive(input$choice, {
inFile <- input$file
req(inFile)
# Changes in read.table
f <- read.table(inFile$datapath, header = input$header, sep = input$sep, quote = input$quote)
vars <- names(f)
# Update select input immediately after clicking on the action button.
updateCheckboxGroupInput(session, "columns","Select Columns", choices = vars)
f
})
output$rawdata <- DT::renderDataTable(
DT::datatable(info(),
options = list(searching = F, pageLength = 5)
)
)
anonymizeData <- reactive({
pii_cols <- input$columns
if(!is.null(pii_cols)) {
pii <- info()
for(i in 1:length(input$columns)) {
pii[, input$columns[i]] <- anonymize(pii[, input$columns[i]], .algo = input$anon_algo, .seed = input$anon_seed, .n_chars = input$anon_nchars)
}
} else pii <- info()
return(pii)
})
output$data2 <- DT::renderDataTable(
DT::datatable(anonymizeData(),
options = list(searching = F, pageLength = 5, columnDefs = list(list(
targets = 1:ncol(anonymizeData()),
render = JS(
"function(data, type, row, meta) {",
"return type === 'display' && data.length > 6 ?",
"'<span title=\"' + data + '\">' + data.substr(0, 6) + '...</span>' : data;",
"}")
))), callback = JS('table.page(3).draw(false);'))
)
output$downloadData <- downloadHandler(
filename = function() { paste(tools::file_path_sans_ext(input$file), '_anonymized.csv', sep='') },
content = function(file) {
write.csv(anonymizeData(), file, row.names = F)
}
)
}
shinyApp(ui, server)