Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
server.R
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
library(shiny)
|
2 |
+
library(readxl)
|
3 |
+
library(stringr)
|
4 |
+
library(openxlsx)
|
5 |
+
library(shinythemes)
|
6 |
+
|
7 |
+
|
8 |
+
server <- function(input, output) {
|
9 |
+
# Store the original file content
|
10 |
+
originalFileContent <- reactiveVal(NULL)
|
11 |
+
|
12 |
+
observeEvent(input$file, {
|
13 |
+
req(input$file) # Read the file content
|
14 |
+
|
15 |
+
file_type <- switch(input$file_type,
|
16 |
+
"CSV" = "csv",
|
17 |
+
"XLSX" = "xlsx",
|
18 |
+
"TXT" = 'txt')
|
19 |
+
|
20 |
+
data_sheet <- tryCatch(
|
21 |
+
switch(file_type,
|
22 |
+
"csv" = {
|
23 |
+
sep <- if (input$csv_sep != "Other") input$csv_sep else input$csv_other
|
24 |
+
read.csv(input$file$datapath, sep = sep)
|
25 |
+
},
|
26 |
+
"xlsx" = {
|
27 |
+
tryCatch(
|
28 |
+
read_xlsx(input$file$datapath, sheet = input$excel_sheet),
|
29 |
+
error = function(e) {
|
30 |
+
showModal(modalDialog(
|
31 |
+
title = "Error",
|
32 |
+
"La hoja de Excel especificada no existe en el archivo subido. Por favor, selecciona una hoja válida.",
|
33 |
+
easyClose = TRUE
|
34 |
+
))
|
35 |
+
return(NULL)
|
36 |
+
}
|
37 |
+
)
|
38 |
+
},
|
39 |
+
"txt" = {
|
40 |
+
readLines(input$file$datapath)
|
41 |
+
}
|
42 |
+
),
|
43 |
+
error = function(e) {
|
44 |
+
showModal(modalDialog(
|
45 |
+
title = "Error",
|
46 |
+
"El archivo no se pudo leer. Por favor, verifica que el archivo sea válido y que el tipo de archivo seleccionado sea correcto.",
|
47 |
+
easyClose = TRUE
|
48 |
+
))
|
49 |
+
return(NULL)
|
50 |
+
}
|
51 |
+
)
|
52 |
+
|
53 |
+
file_ext <- tools::file_ext(input$file$name)
|
54 |
+
|
55 |
+
if (tolower(file_ext) != tolower(input$file_type)) {
|
56 |
+
showModal(modalDialog(
|
57 |
+
title = "Error",
|
58 |
+
"The file extension does not match the selected file type. Please select the correct file type or upload a file with the correct extension.",
|
59 |
+
easyClose = TRUE
|
60 |
+
))
|
61 |
+
return()
|
62 |
+
}
|
63 |
+
|
64 |
+
email_pattern <- "([_a-z0-9-]+(?:\\.[_a-z0-9-]+)*@[a-z0-9-]+(?:\\.[a-z0-9-]+)*(?:\\.[a-z]{2,63}))"
|
65 |
+
|
66 |
+
correos <- c()
|
67 |
+
|
68 |
+
switch(file_type,
|
69 |
+
"csv" = {
|
70 |
+
data_sheet <- read.csv(input$file$datapath, sep = sep)
|
71 |
+
|
72 |
+
|
73 |
+
#crete checkbox group
|
74 |
+
output$checkbox_group <- renderUI({
|
75 |
+
checkboxGroupInput("columns", "Select columns:", choices = names(data_sheet))
|
76 |
+
})
|
77 |
+
|
78 |
+
observeEvent(input$columns, {
|
79 |
+
req(input$columns)
|
80 |
+
for (col_name in input$columns) {
|
81 |
+
correos_raw <- na.omit(as.character(data_sheet[[col_name]]))
|
82 |
+
|
83 |
+
for (entry in correos_raw) {
|
84 |
+
correos_found <- str_extract_all(entry, email_pattern)[[1]]
|
85 |
+
correos <- unique(c(correos, correos_found))
|
86 |
+
}
|
87 |
+
}
|
88 |
+
originalFileContent(correos)
|
89 |
+
|
90 |
+
output$contents <- renderTable({
|
91 |
+
data.frame(Content = originalFileContent())
|
92 |
+
})
|
93 |
+
})
|
94 |
+
},
|
95 |
+
"xlsx" = {
|
96 |
+
data_sheet <- read_xlsx(input$file$datapath, sheet = input$excel_sheet)
|
97 |
+
|
98 |
+
|
99 |
+
#crete checkbox group
|
100 |
+
output$checkbox_group <- renderUI({
|
101 |
+
checkboxGroupInput("columns", "Select columns:", choices = names(data_sheet))
|
102 |
+
})
|
103 |
+
|
104 |
+
|
105 |
+
observeEvent(input$columns, {
|
106 |
+
req(input$columns)
|
107 |
+
for (col_name in input$columns) {
|
108 |
+
correos_raw <- na.omit(as.character(data_sheet[[col_name]]))
|
109 |
+
|
110 |
+
for (entry in correos_raw) {
|
111 |
+
correos_found <- str_extract_all(entry, email_pattern)[[1]]
|
112 |
+
correos <- unique(c(correos, correos_found))
|
113 |
+
}
|
114 |
+
}
|
115 |
+
originalFileContent(correos)
|
116 |
+
|
117 |
+
output$contents <- renderTable({
|
118 |
+
data.frame(Content = originalFileContent())
|
119 |
+
})
|
120 |
+
})
|
121 |
+
},
|
122 |
+
"txt" = {
|
123 |
+
data_sheet <- readLines(input$file$datapath)
|
124 |
+
emails <- unlist(regmatches(data_sheet, gregexpr(email_pattern, data_sheet)))
|
125 |
+
originalFileContent(unique(emails))
|
126 |
+
output$contents <- renderTable({
|
127 |
+
data.frame(Content = originalFileContent())
|
128 |
+
})
|
129 |
+
}
|
130 |
+
)
|
131 |
+
|
132 |
+
save_file <- function() {
|
133 |
+
file.copy(input$file$datapath, "/home/juan/Desktop/shiny_email")
|
134 |
+
}
|
135 |
+
|
136 |
+
output$Download <- downloadHandler(
|
137 |
+
filename = function() {
|
138 |
+
paste(tools::file_path_sans_ext(input$file$name), "_emails", ".xlsx", sep = "")
|
139 |
+
},
|
140 |
+
content = function(file) {
|
141 |
+
emails_df <- data.frame(Emails = as.character(originalFileContent()))
|
142 |
+
write.xlsx(emails_df, file)
|
143 |
+
}
|
144 |
+
)
|
145 |
+
|
146 |
+
output$Download2 <- downloadHandler(
|
147 |
+
filename = function() {
|
148 |
+
paste(tools::file_path_sans_ext(input$file$name), "_emails", ".txt", sep = "")
|
149 |
+
},
|
150 |
+
content = function(file) {
|
151 |
+
write.table(originalFileContent(), file)
|
152 |
+
}
|
153 |
+
)
|
154 |
+
|
155 |
+
}) # End of observeEvent of file upload
|
156 |
+
} # End of server function
|
ui.R
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
ui <- fluidPage(#shinythemes::themeSelector(),
|
2 |
+
tags$head(
|
3 |
+
tags$link(rel = "stylesheet", type = "text/css", href = "styles.css")
|
4 |
+
),
|
5 |
+
|
6 |
+
titlePanel("Email Extractor: "),
|
7 |
+
|
8 |
+
sidebarLayout(
|
9 |
+
|
10 |
+
sidebarPanel(
|
11 |
+
helpText("Please select the file type first, then upload your file."),
|
12 |
+
selectInput("file_type", "Choose File Type:", choices = c("CSV", "XLSX","TXT")),
|
13 |
+
fileInput("file", "Choose a File", accept = c("text/csv", "text/xlsx", 'text/txt' , ".csv", ".xlsx", ".txt")),
|
14 |
+
|
15 |
+
conditionalPanel(
|
16 |
+
condition = "input.file_type == 'CSV'",
|
17 |
+
selectInput("csv_sep", "CSV Separator:", choices = c("Comma" = ",", "Semicolon" = ";", "Tab" = "\t", "Space" = " ", "Other" = "Other")),
|
18 |
+
textInput('csv_other', 'Other Separator:', ',')
|
19 |
+
),
|
20 |
+
|
21 |
+
conditionalPanel(
|
22 |
+
condition = "input.file_type == 'XLSX'",
|
23 |
+
numericInput("excel_sheet", "Excel Sheet Number:", 1)
|
24 |
+
),
|
25 |
+
|
26 |
+
uiOutput("checkbox_group"),
|
27 |
+
downloadButton("Download", "Download xlsx", icon = icon("download"), class = 'DESCARGA1'),
|
28 |
+
downloadButton("Download2", "Download txt", icon = icon("download"), class = 'DESCARGA2')
|
29 |
+
), # End of sidebarPanel
|
30 |
+
# Show the content of the file
|
31 |
+
mainPanel(
|
32 |
+
tableOutput("contents")
|
33 |
+
) # End of mainPanel
|
34 |
+
), # End of sidebarLayout
|
35 |
+
#tuturial panel
|
36 |
+
mainPanel(
|
37 |
+
h2("Tuturial:"),
|
38 |
+
p("This app is designed to extract email addresses from different file types."),
|
39 |
+
p("Please select the file type first, then upload your file."),
|
40 |
+
p("If you choose CSV or TXT, you can select the separator."),
|
41 |
+
p("If you choose XLSX, you can input the sheet number."),
|
42 |
+
p("After you upload the file, you can download the extracted email addresses in xlsx and txt format.")
|
43 |
+
) # End of mainPanel
|
44 |
+
) # End of fluidPage
|