0

I have several large .R files. I want to merge them into a single .R file. Here are two example .R files

MN.R

#' @title Mineral N i.e., summation of ammonium-N and nitrate-N in soil
#'
#' @description This function determines the amount of mineral N (ammonium-N + nitrate-N) in soil as extracted by 2 M KCl, expressed in terms of oven-dried soil mass (Rowell, 1994).
#'
#' @usage Mineral_N(W = W, m = m, VA = VA, VE = VE, VS = VS, VB = VB, X = X)
#'
#' @param  W Mass of moist soil sample (g)
#' @param  m Moisture content of the field-moist soil sample (% w/w)
#' @param  VA Volume of aliquot of the extract (mL) taken for distillation
#' @param  VE Volume of 2 M KCl (mL) added as extractant
#' @param  VS Volume of standard sulphuric acid solution (mL) consumed in titration with soil
#' @param  VB Volume of standard sulphuric acid solution (mL) consumed in titration without soil (i.e., blank titration)
#' @param  X Normality of standard sulphuric acid solution
#' @return N_mgkg - Mineral N in soil (mg/kg) (expressed in terms of oven-dried soil mass)
#'
#'
#' @references Biswas, D.R. (2018) Determination of different forms of nitrogen in soil and total N in plant. In Manual on Advanced Techniques for Analysis of Nutrients and Pollutant Elements in Soil, Plant and Human (S.P. Datta, M.C. Meena, B.S. Dwivedi, A.K. Shukla (Eds.)), Westville Publishing House, New Delhi, pp. 57-68.
#'
#' Rowell, D.L. (1994) Soil Science Methods and Application, Pearson Education Ltd., Essex, England.
#'
#' @details Mineral N in soil consists of ammonium and nitrate ions present on soil exchange sites or solution or both. It can be extracted by 2 M KCl. The extract is steam-distilled in the presence of magnesium oxide and Devarda’s alloy (Cu: Al: Zn :: 50: 45: 5) to liberate ammonia. The evolved ammonia is absorbed in 2% boric acid solution containing mixed indicator, pH 4.5, and titrated against standard sulphuric acid solution to know the volume of the boric acid consumed during ammonia absorption (Rowell, 1994; Biswas, 2018).
#'
#' @export
#' @examples
#' with(data = df_Minearl_N, Mineral_N(W = Mass_Moistsoil, m = Moisture_Percent,
#' VA = Vol_Aliquot, VE = Vol_Extractant, VS = Vol_Sulphuric_Soil,
#' VB = Vol_Sulphuric_Blank, X = Normality_Sulphuric))
#'

Mineral_N <- function(W = W, m = m, VA = VA, VE = VE, VS = VS, VB = VB, X = X){
  N_mgkg = (VS-VB)*14*X*(VE+(W*m)/(100+m))*(100+m)*10/(VA*W)
  
  return(N_mgkg)
}

WBC.R

#' @title Oxidizable organic carbon (C) or Walkley-Black C in soil
#'
#' @description The oxidizable organic C or Walkley-Black C (WBC) in soil is obtained by this function based on the method described by Walkley and Black (1934).
#'
#' @usage WBC(W = W, Vk = Vk, Vb = Vb, Vs = Vs, Nk = Nk)
#'
#' @param  W Mass of soil sample (g)
#' @param  Vk Volume of potassium dichromate solution (mL)
#' @param  Vb Volume of ferrous ammonium sulphate solution (mL) consumed in titration without soil (i.e., blank titration)
#' @param  Vs Volume of ferrous ammonium sulphate solution (mL) consumed in titration with soil
#' @param  Nk Normality of potassium dichromate solution
#' @return WBC_pc - Oxidizable organic C or Walkley-Black C in soil (%)
#' WBC_gkg - Oxidizable organic C or Walkley-Black C in soil (g/kg)
#'
#'
#' @references Basak RK (2000) Soil Testing and Recommendation: A Text Book. Kalyani Publishers, New Delhi, India.
#'
#' Walkley AJ, Black CA (1934) An estimation of the Degtjareff method for determining soil organic matter and a proposed modification of the chromic acid titration method. Soil Science 37, 29–38. doi:10.1097/00010694-193401000-00003
#'
#' @details Soil organic C is very important to soil fertility, nutrient cycling and C sequestration. Routine soil testing includes determination of oxidizable organic C as per the method described by Walkley and Black (1934). The oxidizable organic C mostly encompasses C present in partly decomposed organic matter and slightly the well decomposed organic matter in soil (Basak, 2000). The method suggested by Walkley and Black (1934) involves oxidation of soil organic C with an excess of standard (usually 1 N) potassium dichromate solution (usually, 10 mL for 1 g soil) in presence of concentrated sulphuric acid (20 mL). The unreacted potassium dichromate solution is titrated against freshly prepared ferrous ammonium sulphate solution to get the volume of potassium dichromate solution consumed in oxidizing soil organic C, from which the content of oxidizable organic C is calculated.
#' @export
#' @examples
#' with(data = df_WBC, WBC(W = Mass_Soil, Vk = Vol_Potassium_dichromate,
#' Vb = Vol_FAS_Blank, Vs = Vol_FAS_Soil, Nk = Normality_Potassium_dichromate))
#'

WBC <- function(W = W, Vk = Vk, Vb = Vb, Vs = Vs, Nk = Nk){
  WBC_pc = Vk*(1-(Vs/Vb))*Nk*0.3/W
  WBC_gkg = WBC_pc*10
  
  return(list(WBC_pc = WBC_pc, WBC_gkg = WBC_gkg))
}

I want to merge them into single .R file like

Merged.R

#' @title Mineral N i.e., summation of ammonium-N and nitrate-N in soil
#'
#' @description This function determines the amount of mineral N (ammonium-N + nitrate-N) in soil as extracted by 2 M KCl, expressed in terms of oven-dried soil mass (Rowell, 1994).
#'
#' @usage Mineral_N(W = W, m = m, VA = VA, VE = VE, VS = VS, VB = VB, X = X)
#'
#' @param  W Mass of moist soil sample (g)
#' @param  m Moisture content of the field-moist soil sample (% w/w)
#' @param  VA Volume of aliquot of the extract (mL) taken for distillation
#' @param  VE Volume of 2 M KCl (mL) added as extractant
#' @param  VS Volume of standard sulphuric acid solution (mL) consumed in titration with soil
#' @param  VB Volume of standard sulphuric acid solution (mL) consumed in titration without soil (i.e., blank titration)
#' @param  X Normality of standard sulphuric acid solution
#' @return N_mgkg - Mineral N in soil (mg/kg) (expressed in terms of oven-dried soil mass)
#'
#'
#' @references Biswas, D.R. (2018) Determination of different forms of nitrogen in soil and total N in plant. In Manual on Advanced Techniques for Analysis of Nutrients and Pollutant Elements in Soil, Plant and Human (S.P. Datta, M.C. Meena, B.S. Dwivedi, A.K. Shukla (Eds.)), Westville Publishing House, New Delhi, pp. 57-68.
#'
#' Rowell, D.L. (1994) Soil Science Methods and Application, Pearson Education Ltd., Essex, England.
#'
#' @details Mineral N in soil consists of ammonium and nitrate ions present on soil exchange sites or solution or both. It can be extracted by 2 M KCl. The extract is steam-distilled in the presence of magnesium oxide and Devarda’s alloy (Cu: Al: Zn :: 50: 45: 5) to liberate ammonia. The evolved ammonia is absorbed in 2% boric acid solution containing mixed indicator, pH 4.5, and titrated against standard sulphuric acid solution to know the volume of the boric acid consumed during ammonia absorption (Rowell, 1994; Biswas, 2018).
#'
#' @export
#' @examples
#' with(data = df_Minearl_N, Mineral_N(W = Mass_Moistsoil, m = Moisture_Percent,
#' VA = Vol_Aliquot, VE = Vol_Extractant, VS = Vol_Sulphuric_Soil,
#' VB = Vol_Sulphuric_Blank, X = Normality_Sulphuric))
#'

Mineral_N <- function(W = W, m = m, VA = VA, VE = VE, VS = VS, VB = VB, X = X){
  N_mgkg = (VS-VB)*14*X*(VE+(W*m)/(100+m))*(100+m)*10/(VA*W)
  
  return(N_mgkg)
}


#' @title Oxidizable organic carbon (C) or Walkley-Black C in soil
#'
#' @description The oxidizable organic C or Walkley-Black C (WBC) in soil is obtained by this function based on the method described by Walkley and Black (1934).
#'
#' @usage WBC(W = W, Vk = Vk, Vb = Vb, Vs = Vs, Nk = Nk)
#'
#' @param  W Mass of soil sample (g)
#' @param  Vk Volume of potassium dichromate solution (mL)
#' @param  Vb Volume of ferrous ammonium sulphate solution (mL) consumed in titration without soil (i.e., blank titration)
#' @param  Vs Volume of ferrous ammonium sulphate solution (mL) consumed in titration with soil
#' @param  Nk Normality of potassium dichromate solution
#' @return WBC_pc - Oxidizable organic C or Walkley-Black C in soil (%)
#' WBC_gkg - Oxidizable organic C or Walkley-Black C in soil (g/kg)
#'
#'
#' @references Basak RK (2000) Soil Testing and Recommendation: A Text Book. Kalyani Publishers, New Delhi, India.
#'
#' Walkley AJ, Black CA (1934) An estimation of the Degtjareff method for determining soil organic matter and a proposed modification of the chromic acid titration method. Soil Science 37, 29–38. doi:10.1097/00010694-193401000-00003
#'
#' @details Soil organic C is very important to soil fertility, nutrient cycling and C sequestration. Routine soil testing includes determination of oxidizable organic C as per the method described by Walkley and Black (1934). The oxidizable organic C mostly encompasses C present in partly decomposed organic matter and slightly the well decomposed organic matter in soil (Basak, 2000). The method suggested by Walkley and Black (1934) involves oxidation of soil organic C with an excess of standard (usually 1 N) potassium dichromate solution (usually, 10 mL for 1 g soil) in presence of concentrated sulphuric acid (20 mL). The unreacted potassium dichromate solution is titrated against freshly prepared ferrous ammonium sulphate solution to get the volume of potassium dichromate solution consumed in oxidizing soil organic C, from which the content of oxidizable organic C is calculated.
#' @export
#' @examples
#' with(data = df_WBC, WBC(W = Mass_Soil, Vk = Vol_Potassium_dichromate,
#' Vb = Vol_FAS_Blank, Vs = Vol_FAS_Soil, Nk = Normality_Potassium_dichromate))
#'

WBC <- function(W = W, Vk = Vk, Vb = Vb, Vs = Vs, Nk = Nk){
  WBC_pc = Vk*(1-(Vs/Vb))*Nk*0.3/W
  WBC_gkg = WBC_pc*10
  
  return(list(WBC_pc = WBC_pc, WBC_gkg = WBC_gkg))
}

I have seen some similar questions here and here and I have tried like:

# Load tidyverse
library(tidyverse)

# List files and source each
list.files("path_to_folder", full.names = TRUE) %>% map(source)

which runs without any error. Now how can I write it as one .R file?

UseR10085
  • 7,120
  • 3
  • 24
  • 54
  • 1
    `source` does nothing to change the underlying files, it reads, parses, and loads the objects into the current environment. What you're asking about has nothing to do with _parsing_ the contents, just _appending_. How about `list.files(..) %>% map(readLines) %>% unlist() %>% writeLines("single_file.R")`? – r2evans Jul 14 '23 at 11:37
  • But unless you need to do this frequently, why not just copy the contents of one file (in your IDE or text-editor) and paste to the end of the other? – r2evans Jul 14 '23 at 11:39
  • I have 29 large .R files. During copy and past, I may create some error. – UseR10085 Jul 14 '23 at 11:47
  • Curious, why do you want to combine all of the `*.R` files? While it's certainly a personal preference, I've not participated in a project where having 29+ functions in one file was considered better than some organization within files. R doesn't care if you `source("onefile.R")` or many, it should take about the same resources and perhaps a millisecond or two longer to read them in. – r2evans Jul 14 '23 at 12:19
  • Actually, I am trying to review one R package developed by someone else. I am correcting the source code in GitHub but it is not reflecting in R documentation when I am using help after reinstalling. Because in the source code, some parameters are described as `@inheritParams` from some other function. Thats why I want to merge all the .R files and try to see it. – UseR10085 Jul 14 '23 at 12:24
  • As suggested by you, I will merge those files manually. – UseR10085 Jul 14 '23 at 12:30

2 Answers2

2

Open a command prompt/terminal/shell in the folder which contains the files, then:

If you're on Mac or Linux, run:

cat MN.R WBC.R > Merged.R

or on Windows, run:

type MN.R WBC.R > Merged.R
Mark
  • 7,785
  • 2
  • 14
  • 34
  • Whether manually I have to type the file names? – UseR10085 Jul 14 '23 at 11:49
  • 3
    `cat *.R > Merged.R` (though don't repeat it without removing `Merged.R` first, otherwise you'll have multiple copies of everything ... been there, done that) – r2evans Jul 14 '23 at 11:49
  • Can I have the file name as separator within the merged file? – UseR10085 Jul 14 '23 at 11:54
  • @UseR10085 sure you can, but that's a slightly different question! :') – Mark Jul 14 '23 at 12:08
  • @r2evans is right, with the caveat that it will merge together all R files in the current directory. So if you have a file called `my_secret_information.R`, it would be in there too. – Mark Jul 14 '23 at 12:10
  • Based on the use of `list.files("path_to_folder", full.names = TRUE)`, that would be a problem with your initial attempt too, so perhaps not a factor. – r2evans Jul 14 '23 at 12:17
  • @UseR10085 on Mac/Linux, you would run `for file in *.R; do echo "\n# $file -------------------------------------------------------------" && cat "$file"; done` – Mark Jul 14 '23 at 13:46
0

If you want to add the source file names, you could try along:

## example folder source files:
list.files('./source_files/', pattern = '\\.R$', full.names = TRUE)
[1] "./source_files/hello.R" "./source_files/world.R"

map and reduce:

list.files('./source_files/', pattern = '\\.R$', full.names = TRUE) |>
  Map(f = \(fn) sprintf('## source: %s\n%s', fn, readLines(fn))) |>
  Reduce(f = \(old, new) paste(old, new, sep = '\n')) |>
  writeLines(con = file('combined.R'))

content of file 'combined.R':

## source: ./source_files/hello.R
cat('Hello,')
## source: ./source_files/world.R
cat('world!')
I_O
  • 4,983
  • 2
  • 2
  • 15