Extract list of files with column of files names in R
try:
data_files$filename = basename(rownames(data_files))
list files from https sever using R
The following works for me:
library(httr)
library(XML)
df <- readHTMLTable(content(GET("https://oceandata.sci.gsfc.nasa.gov/MODIS-Aqua/Mapped/Monthly/4km/sst"), "text"))[[1]]
download.file(paste0("https://oceandata.sci.gsfc.nasa.gov/cgi/getfile/",
tail(df,1)$Filename), tail(df,1)$Filename, mode = "wb")
r dplyr - read list of files and use filename as a variable
I think that this should work. Since you get the information about the filename and pass it to the image_read
but want to use this information also for another function (image_annotate
), you can easily put the two in a function within map
. If you don't want the whole path as annotation, just replace image_annotate(fnme)
by image_annotate(stringr::str_extract(string = fnme, pattern = "(?<=(/|\\\\)).+(?=\\.png)"))
which matches anything between /
or \
and .png
(assuming that you don't want the ending, else just remove the lookahead (?=\\.png
).
library(dplyr)
library(purrr)
library(magick)
list.files(path = "", pattern = "*.png", full.names = T) %>%
map(function(fnme) {
image_read(fnme) %>% image_annotate(fnme)
}) %>%
image_join() %>%
image_animate(fps=1) %>%
image_write("animated.gif")
How to append input file name to corresponding list output index in R
The 'out' list
doesn't have any names because it was not named. If the names should come from the files
part, we may name the output ('out') with the substring of file names
# returns all the file paths for csv
files <- list.files("C:\\mypath\\", pattern="\\.csv$", full.names = TRUE)
# get the substring of file names without the .csv part
filenms <- sub("\\.csv$", "", basename(files))
Now, we use the same code as in the OP's post or just loop over the files directly
out <- lapply(files, function(x) { # use lapply to loop over all files
this_data <- read.csv(x, header = TRUE)
aov_mod <- aov(revenue ~ dept, data = this_data)
tuk <- TukeyHSD(x= aov_mod)
# output of TukeyHSD is a list which can be summarised into tibble
# with tidy from broom
broom::tidy(tuk)
})
# set the names with filenms
names(out) <- filenms
If we want to write the output use imap/iwalk
from purrr
which is concise as .y
returns the names and .x
returns the value of the list
purrr::iwalk(out, ~ write.csv(.x, paste0(.y, ".csv"), row.names = FALSE))
If we want pdf
file, an option is to use tableGrob
library(grid)
library(gridExtra)
library(gtable)
# reproducible exmaple
out <- list(iris = head(iris), mtcars = head(mtcars))
pdf(file.path(getwd(), "Downloads/filepdfMar9.pdf"))
iwalk(out, ~ {
title <- textGrob(.y, gp = gpar(fontsize = 50))
padding <- unit(0.5,"line")
x1 <- gtable_add_rows(
tableGrob(.x), heights = grobHeight(title) + padding, pos = 0
)
x1 <- gtable_add_grob(
x1, list(title),
t = 1, l = 1, r = ncol(x1)
)
grid.newpage()
grid.draw(x1)
})
dev.off()
-output
Related Topics
Change Thickness of a Marker in Ggplot2
Creating a Cumulative Step Graph in R
Ggplot2: Making Changes to Symbols in The Legend
Run R Interactively from Rscript
Multiplication of Large Integers
Can't Install Any R Packages on Linux Server
Extract Only Folder Name Right Before Filename from Full Path
How to Calculate Euclidean Distance Between Two Matrices in R
Ggplot: Line Plot for Discrete X-Axis
Why Ggplot2 Legend Not Show in The Graph
Spread with Duplicate Identifiers for Rows
How to Create a Continuous Legend (Color Bar Style) for Scale_Alpha
How to Get All Possible Combinations of N Number of Data Set