0

I have a written a function in r markdown. I want to run the below on a number of different csvs stored in the same folder.

How would I therefore go about creating a generic function [in place of the below code within my overall code] so that it allowed for the import and analysis of each of the csvs in the folder?

dat <- read.csv("importsinglecsv.csv",
                    header = TRUE,
                    sep=',')

Overall code outlined below:

Single Test
    dat <- read.csv("importsinglecsv.csv",
                    header = TRUE,
                    sep=',')
    
    datA <- subset(dat, Version == "A")
    datB <- subset(dat, Version == "B")
    olsA <- lm(measure ~ mark, data = datA)
    olsB <- lm(measure ~ mark, data = datB)
    coeffs <- rbind(olsA$coefficients, olsB$coefficients)
    r.Squared <- rbind(summary(olsA)$r.squared, summary(olsB)$r.squared)
    corA <-cor(datA$measure, datA$mark)
    corB <- cor(datB$measure, datB$mark)
    ```

I've tried to develop a for loop linking to the markdown file, but have done something wrong.....

for (i in length(df)) {
  dat <- df[[i]]
  datA <- subset(dat, Version == "A")
  datB <- subset(dat, Version == "B")
  rmarkdown::render('"filepath"/tes.rmd',
                    output_file=paste0("filepath", df[i], ".html"),
                    params=list(new_title=paste("Exploratory analysis -", i)))
}
j.rahilly
  • 33
  • 5
  • 1
    So you want a separated file that would open these files and create these variables that you could call from R Markdown file? – ViviG Jan 14 '21 at 21:52
  • 1
    Thanks ViviG. Yes.....I want to run a generic markdown file (tes.rmd) on 100 csvs [which all have identical column names] contained within a single folder. I tried to write a for loop [included in above edit]m, but am not too sure of how to do it.... – j.rahilly Jan 15 '21 at 09:16
  • Maybe you could have a look at [passing variables to rmd-files](https://stackoverflow.com/questions/32479130/passing-parameters-to-r-markdown). Example follows. – randomchars42 Jan 15 '21 at 09:29

1 Answers1

0

You're on the right track. This is a case for passing parameters to rmd-files as solved here:

Your .Rmd file needs to know which .csv to load, so we pass the filename as a parameter:

analyse.r

your_path <- 'your/path/goes/here'

for (filename in list.files(path=your_path, pattern='*.csv',  full.names=FALSE)) {
  rmarkdown::render(
    './report.Rmd',
    params=list(filename=paste0(your_path, '/', filename)),
    output_file=paste0(your_path, '/', filename, '.html'))
}

report.Rmd:

---
title: "report"
output: html_document
params:
  filename: False
---

```{r setup}
dat <- read.csv(filename, header = TRUE, sep=',')
    
datA <- subset(dat, Version == "A")
datB <- subset(dat, Version == "B")
olsA <- lm(measure ~ mark, data = datA)
olsB <- lm(measure ~ mark, data = datB)
coeffs <- rbind(olsA$coefficients, olsB$coefficients)
r.Squared <- rbind(summary(olsA)$r.squared, summary(olsB)$r.squared)
corA <-cor(datA$measure, datA$mark)
corB <- cor(datB$measure, datB$mark)
```

Your RMarkdown goes here.

Explanation:

analyse.R loops over all files ending with *.csv. `rmardown.render(..., params=list(filename=filename)) then passes the filename on to the file it renders.

Your .Rmd file is prepared to take arguments by the YAML-header. Params are accessed using params$PARAMNAME_SPECIFIED_IN_HEADER.

There's a great explanation in yihui's "R Markdown: The Definitive Guide", 15.3 Knitting with parameters (already linked in the thread linked above).

randomchars42
  • 335
  • 1
  • 8