0

Here is the reproducible data:

file1 <- data.frame(animal = c('cat','dog','horse'), w = c(10,20,30)) file2 <- data.frame(animal = c('pig','dog','bat'), w = c(11,22,33)) file3 <- data.frame(animal = c('dog','eagle','horse'), w = c(110,220,330)) write.csv(file1, 'foobar_TEST_1.csv') write.csv(file2, 'foobar_TEST_2.csv') write.csv(file3, 'foobar_TEST_3.csv') 

I would like to upload all 3 files and if the values under 'animal' column are duplicates, rename only the duplicates using the last part of file names. From our data: dog, dog_TEST_2, dog_TEST_3, horse, horse_TEST_2

Here's the Shiny code and ideally, I'd like to implement the code in the reactive():

ui <- fluidPage( sidebarPanel( fileInput(inputId = "animal", label = "Upload Multiple Files", multiple = T, accept = c("text/csv", "text/comma-separated-values,text/plain", ".csv"))), mainPanel( DT::dataTableOutput("table1"))) server <- function(input, output, session) { data <- reactive({ req(input$animal) inFile <- input$animal$datapath df <- lapply(inFile, function(y){ files <- read.csv(y, header=TRUE) }) final <- do.call(rbind, df) list(final = final) }) output$table1 <- DT::renderDataTable({ final <- data()$final datatable(data = final) }) } shinyApp(ui = ui, server = server) 

1 Answer 1

1

That's more an issue in data wrangling than in shiny. (; As a first step add the filename to each df when reading your datasets. Second step would be to keep only the part of the filenames you want to use to label your duplicates. Finally, rename the duplicated animals.

server <- function(input, output, session) { data <- reactive({ req(input$animal) inFile <- input$animal$datapath inFile_names <- input$animal$name df <- Map(function(x, y) { files <- read.csv(x, header = TRUE) files$file <- y files }, inFile, inFile_names) final <- do.call(rbind, df) row.names(final) <- NULL final$file <- gsub("^foobar_(.*?)\\.csv$", "\\1", final$file) dups <- duplicated(final$animal) final$animal[dups] <- paste(final$animal[dups], final$file[dups], sep = "_") final$file <- NULL list(final = final) }) output$table1 <- DT::renderDataTable({ final <- data()$final DT::datatable(data = final) }) } 

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.