stringr issue r shiny

Hello,
I would like to read files from a list according to their names, i did that with str_which from the stringr package in r and it worked perfectly, but when i tried to do the same in r shiny with uploaded files, it gives nothing, but a warning .

Warning in stri_detect_regex(string, pattern, negate = negate, opts_regex = opts(pattern)) :
  argument is not an atomic vector; coercing

below's the r and the rshiny code

library(data.table)
library(tidyverse)

liste <- list.files("~/s/coord",pattern="\\.xlsx$", full.names=TRUE)
 
 coord<- lapply(liste,read_excel, skip=2,.name_repair = ~ gsub(" ", ".",.x, fixed = TRUE))
 
 p1 <-str_which(liste, regex(" DV", fixed=T, ignore_case = T))
 
 p2 <- str_which(liste, regex("RDV", fixed=T, ignore_case = T))
 
 p3 <- str_which(liste, regex("Vendeur", fixed =T, ignore_case = T))
 
 mail_dv <- rbindlist(coord[p1])
 mail_rdv <- rbindlist(coord[p2])
 mail_vend <- rbindlist(coord[p3])
library(shiny)
shinyApp(
    ui = fluidPage(
        titlePanel("Cible IPSOS"),
        sidebarLayout(
            sidebarPanel(
                   
                fileInput("mails",
                                    label="Mail DV RDV & Vendeur",
                                    multiple=TRUE,
                                    accept=c(".xlsx"))),
        
            mainPanel(
              dataTableOutput("table")
                ))
        
    ),
    server = function(input, output) {
        output$table <- renderDataTable({
                                             
                                      coord<- lapply(input$mails$datapath,read_excel, skip=2, .name_repair = ~ gsub(" ", ".",.x, fixed = TRUE))
                                      
                                     
                                       p1 <-  which(str_detect(input$mails$datapath, regex(" DV", fixed=T,ignore_case = T)))

                                       p2 <- stringr::str_which(input$mails$datapath, regex("RDV",fixed=T, ignore_case = T))
                                       
                                       p3 <- stringr::str_which(input$mails$datapath, regex("Vendeur",fixed=T, ignore_case = T))

                                      mail_dv <- rbindlist(coord[p1]) 
                                      mail_rdv <- rbindlist(coord[p2])
                                      mail_vend <- rbindlist(coord[p3])
                                    
                                    return(mail_dv)
                                      }
         )
        
    }
)

Hi,

It's just because of a confusing difference between the data path in Shiny and the path of a local file. When a user uploads a file, it gets a temporary path and name that does not resemble the original file name.

input$mails$datapath
"C:\\Users\\PJVANC~1\\AppData\\Local\\Temp\\RtmpK43XyX/e1295409afe5827dfed6a449/0.xlsx"

The file name is stored separately in the input variable

input$mails$name
test DV.xlsx

So all you need to do is change that, and it should be fixed. (not for the loading of the datasets with read_xl though, that needs the datapath)

coord<- lapply(input$mails$datapath,read_excel, skip=2, .name_repair = ~ gsub(" ", ".",.x, fixed = TRUE))

p1 <- which(str_detect(input$mails$name, regex(" DV", fixed=T,ignore_case = T)))      
p2 <- str_which(input$mails$name, regex("RDV",fixed=T, ignore_case = T))      
p3 <- str_which(input$mails$name, regex("Vendeur",fixed=T, ignore_case = T))

Hope this helps,
PJ

2 Likes

In addition to the advice above, you may also want to handle the case when input$mails is NULL. {stringr} functions appear to accept NULL without throwing an error, i.e. stringr::str_which(NULL, "???"). Even so, you may not want to render anything until a file is uploaded. In this case you could add req(input$mails) at the top of your render data table expression.

1 Like

thank you so much PJ, it works perfectly! :slight_smile:

thank's for the tip :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.