File size: 5,434 Bytes
106e0c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
library(shiny)
library(readxl)
library(stringr)
library(openxlsx)  
library(shinythemes)


server <- function(input, output) {
  # Store the original file content
  originalFileContent <- reactiveVal(NULL)
  
  observeEvent(input$file, {
    req(input$file) # Read the file content
    
    file_type <- switch(input$file_type,
                        "CSV" = "csv",
                        "XLSX" = "xlsx",
                        "TXT" = 'txt')
    
    data_sheet <- tryCatch(
      switch(file_type,
             "csv" = {
               sep <- if (input$csv_sep != "Other") input$csv_sep else input$csv_other
               read.csv(input$file$datapath, sep = sep)
             },
             "xlsx" = {
               tryCatch(
                 read_xlsx(input$file$datapath, sheet = input$excel_sheet),
                 error = function(e) {
                   showModal(modalDialog(
                     title = "Error",
                     "La hoja de Excel especificada no existe en el archivo subido. Por favor, selecciona una hoja válida.",
                     easyClose = TRUE
                   ))
                   return(NULL)
                 }
               )
             },
             "txt" = {
               readLines(input$file$datapath)
             }
      ),
      error = function(e) {
        showModal(modalDialog(
          title = "Error",
          "El archivo no se pudo leer. Por favor, verifica que el archivo sea válido y que el tipo de archivo seleccionado sea correcto.",
          easyClose = TRUE
        ))
        return(NULL)
      }
    )
    
    file_ext <- tools::file_ext(input$file$name)
    
    if (tolower(file_ext) != tolower(input$file_type)) {
      showModal(modalDialog(
        title = "Error",
        "The file extension does not match the selected file type. Please select the correct file type or upload a file with the correct extension.",
        easyClose = TRUE
      ))
      return()
    }
    
    email_pattern <- "([_a-z0-9-]+(?:\\.[_a-z0-9-]+)*@[a-z0-9-]+(?:\\.[a-z0-9-]+)*(?:\\.[a-z]{2,63}))"
    
    correos <- c()
    
    switch(file_type,
           "csv" = {
             data_sheet <- read.csv(input$file$datapath, sep = sep)
             
             
             #crete checkbox group
             output$checkbox_group <- renderUI({
               checkboxGroupInput("columns", "Select columns:", choices = names(data_sheet))
             })
             
             observeEvent(input$columns, {
               req(input$columns)
               for (col_name in input$columns) {
                 correos_raw <- na.omit(as.character(data_sheet[[col_name]]))
                 
                 for (entry in correos_raw) {
                   correos_found <- str_extract_all(entry, email_pattern)[[1]]
                   correos <- unique(c(correos, correos_found))
                 }
               }
               originalFileContent(correos)
               
               output$contents <- renderTable({
                 data.frame(Content = originalFileContent())
               })
             })
           },
           "xlsx" = {
             data_sheet <- read_xlsx(input$file$datapath, sheet = input$excel_sheet)
             
             
             #crete checkbox group
             output$checkbox_group <- renderUI({
               checkboxGroupInput("columns", "Select columns:", choices = names(data_sheet))
             })
             
             
             observeEvent(input$columns, {
               req(input$columns)
               for (col_name in input$columns) {
                 correos_raw <- na.omit(as.character(data_sheet[[col_name]]))
                 
                 for (entry in correos_raw) {
                   correos_found <- str_extract_all(entry, email_pattern)[[1]]
                   correos <- unique(c(correos, correos_found))
                 }
               }
               originalFileContent(correos)
               
               output$contents <- renderTable({
                 data.frame(Content = originalFileContent())
               })
             })
           },
           "txt" = {
             data_sheet <- readLines(input$file$datapath)
             emails <- unlist(regmatches(data_sheet, gregexpr(email_pattern, data_sheet)))
             originalFileContent(unique(emails))
             output$contents <- renderTable({
               data.frame(Content = originalFileContent())
             })
           }
    )
    
    save_file <- function() {
      file.copy(input$file$datapath, "/home/juan/Desktop/shiny_email")
    }
    
    output$Download <- downloadHandler(
      filename = function() {
        paste(tools::file_path_sans_ext(input$file$name), "_emails", ".xlsx", sep = "")
      },
      content = function(file) {
        emails_df <- data.frame(Emails = as.character(originalFileContent()))
        write.xlsx(emails_df, file)
      }
    )
    
    output$Download2 <- downloadHandler(
      filename = function() {
        paste(tools::file_path_sans_ext(input$file$name), "_emails", ".txt", sep = "")
      },
      content = function(file) {
        write.table(originalFileContent(), file)
      }
    )
    
  }) # End of observeEvent of file upload
} # End of server function