-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
216 lines (204 loc) · 9.39 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
##### libraries ####
library(shiny); library(shinyjs); library(shinyalert); library(dplyr);
library(DT); library(shinyBS); library(data.table); library(markdown);
library(rio);
source('functions.R');
requireNamespace('readxl');
requireNamespace('feather');
requireNamespace('fst');
requireNamespace('rmatio');
requireNamespace('jsonlite');
requireNamespace('readODS');
requireNamespace('xml2');
requireNamespace('yaml');
requireNamespace('pzfx');
requireNamespace('csvy');
##### global settings ####
debug <- file.exists('.debug');
gitlink <- 'https://github.com/bokov/anyfile'
source('www/docs/helptext.R');
hcol <- '#008c99';
formats <- gsub('.import.rio_','',grep('^\\.import\\.rio_'
,methods(.import),value=TRUE));
tryfirst <- intersect(c('xlsx','ods','xls','xml','rdata','r','json'
,'html'),formats);
trylast <- c('dat','csvy','yml');
nevertry <- c('clipboard','fortran','csv','csv2','psv','fwf','txt','eviews',trylast);
tryother <- setdiff(formats,c(tryfirst,nevertry));
tryformats <- c(tryfirst,tryother,trylast);
neverexport <- c('clipboard','sqlite');
exportformats <- setdiff(gsub('.export.rio_'
,'',grep('^\\.export\\.rio_'
,methods(.export),value=TRUE))
,neverexport);
# UI ####
ui <- fluidPage(
# + Head ####
tags$head(tags$link(rel="shortcut icon", href="favicon.ico")
,includeScript("ga.js"))
,includeCSS('df.css')
,useShinyjs()
,useShinyalert()
,fluidRow(
# + Title etc. ####
column(1)
,column(2,img(src='sitelogo_color.png',width='100%',maxwidth='100vw'),br()
,if(debug) actionButton('debug','Debug') else c())
,column(8,h3("AnyFile",id='apptitle')
,em('A free, open-source webapp by Alex Bokov, PhD'
,'made possible by support from'
,'NIH/NCATS UL1TR001120 (IIMS) and the'
,'Long School of Medicine KL2 Award.'
,'Uses the',a('rio'
,href='https://github.com/leeper/rio')
,' library by Thomas J. Leeper, PhD.'
,' Source code available on',a('GitHub',href=gitlink
,target='_blank')))
,column(1))
,fluidRow(# + File Upload ####
column(1)
,column(10,hr()
,p("Sometimes you are provided data in an unfamiliar"
," format, or in a format that needs software"
," that you do not own, or even a format that is"
," completely unknown. ", tags$b('AnyFile')
," supports over a "
," dozen of the most common data formats and will"
," do its level best to find a way to read your"
," data, then give you a choice of formats"
," into which you can convert it.")
,fileInput("infile"
,div("Choose a file to upload and convert to a"
," format of your choice" #,HTML(' ')
)
,multiple = FALSE,width = '100%'
))
,column(1))
# + File Convert ####
,hidden(fluidRow(column(1)
,column(10,hr()
,p("Some data formats (e.g. Excel and OpenOffice)"
," may contain multiple tables of data. Here you"
," are being asked which one to import in such a"
," case. If the one you specify is not available "
,tags$b('AnyFile')," will go back to importing"
," the first one it finds.")
,numericInput('which',span('Which sheet or table? '
,'(if in doubt, you can leave it as-is'
,' and just click the button below) '
#,HTML(' ')
)
,min=1,max=20,value=1,width='100%')
,br()
,actionButton('import','Interpret File')
,hidden(div(hr()
,selectInput('saveas','Convert to:'
,choices = exportformats
,selected = 'csv')
#,HTML(' ')
,actionButton('convert','Convert File')
,hidden(textInput('download_clicked'
,label = '',value = ''))
,hidden(span(id='plswait'
,'Converting...'))
,hidden(
span(downloadButton(
'download','Download Converted File')
,id='downloaddiv')),id='convertdiv'))
)
,column(1),id='importdiv'))
,hidden(fluidRow(column(1),column(10,hr(),bsCollapsePanel(span("Preview"
,icon('angle-down'))
,dataTableOutput('preview')))
,column(1),id='previewrow'))
)
# Server ####
server <- function(input, output, session) {
# reactive values ####
rv <- reactiveValues(disclaimerAgreed=F);
# user agreement ####
if(!isTRUE(getOption("shiny.testmode"))&&!file.exists('.testmode')) {
shinyalert('User Agreement',text=helptext$disclaimer
,html=T,confirmButtonText = 'I agree',confirmButtonCol = hcol
,className = 'dfDisclaimer',closeOnEsc = FALSE
,animation = 'slide-from-top',callbackR = function() {
rv[['disclaimerAgreed']] <- TRUE;
show('infile')})} else rv[['disclaimerAgreed']] <- TRUE;
# record file info ####
observeEvent(c(input$infile,rv$disclaimerAgreed,input$which),{
req(input$infile$datapath,rv$disclaimerAgreed);
rv$infile <- input$infile$datapath;
rv$infilename <- input$infile$name;
show('importdiv');
hide('convertdiv');hide('downloaddiv');hide('previewrow');
});
# change in output format ####
observeEvent(input$saveas, hide('downloaddiv'));
# detect download ####
onclick('download',updateTextInput(session,'download_clicked'
,value = as.numeric(Sys.time()))
,add = TRUE);
# read with rio ####
observeEvent(input$import,{
readfile <- try(try_import(rv$infile,which=input$which),silent=TRUE);
if(is(readfile,'try-error')){
shinyalert('You have discovered an (as yet) unsupported file',
'We would appreciate it if you would submit a bug
report to https://github.com/bokov/AnyFile/issues/new
so we can figure out a way to make this app work for
your file as well.
',type='warning')
} else {
if(!is.null(comment(readfile))){
showNotification(paste0(comment(readfile),collapse=' ')
,type='warning')};
rv$readfile <- readfile;
show('convertdiv'); show('previewrow')
hide('downloaddiv');
}
});
# convert with rio ####
observeEvent(input$convert,{
show('plswait');
out <- setNames(rv$readfile,nm=gsub('\\.','_'
,make.names(names(rv$readfile)
,unique = TRUE)));
# hack to avoid errors on pzfx
if(input$saveas == 'pzfx') for(ii in names(out)){
if(inherits(out[[ii]],c('character','factor'))){
out[[ii]] <- as.numeric(factor(out[[ii]]));
showNotification(sprintf('Column %s converted to numeric',ii)
,type='warning');
}
}
result <- try(export(out
,file = tempfile(fileext = paste0('.',input$saveas))
,format=input$saveas));
hide('plswait');
if(is(result,'try-error')) shinyalert('Error converting file'
,as.character(result))
else {
fnicename <- paste0(tools::file_path_sans_ext(rv$infilename)
,'.',input$saveas);
output$download <- downloadHandler(filename=fnicename
,content=function(con) {
file.copy(result,con)});
show('downloaddiv');
}
})
# render datatable ####
output$preview <- renderDataTable({
DT::datatable(rv$readfile,extensions = 'Scroller'
,autoHideNavigation=T,rownames=F,fillContainer=T
,options=list(processing=T,searching=F,scroller=T
,scrollx='100%',scrolly='20vh'
,dom='Bfrtip'
))
},server=FALSE);
# debug ####
observeEvent(input$debug,{
browser();
});
}
# Run the application ####
shinyApp(ui = ui, server = server)