str_replace_last('Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs', '\\n macro = \\n')) %>%
str_replace('farmIds = \\w*', replacement = paste0('farmIds = ', paste0('ABNRegs', '\\'))) %>%
str_replace('execute=Gamsrun', replacement = '  startparallel
FOR farmidinloop = %allfarms%
farmIds  = %farmidinloop%
execute = Gamsrun
ENDFOR
collectparallel
*  execute=Gamsrun
') %>%
writeLines(con = 'test.txt')
readLines('genericbatchfile.txt') %>%  str_replace(pattern = 'Scenario description = \\w*', replacement = paste0('Scenario description = ', 'ABNRegs')) %>%
str_replace_first('Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs')) %>%
stri_replace_last('Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs', '\\n macro = \\n')) %>%
str_replace('farmIds = \\w*', replacement = paste0('farmIds = ', paste0('ABNRegs', '\\'))) %>%
str_replace('execute=Gamsrun', replacement = '  startparallel
FOR farmidinloop = %allfarms%
farmIds  = %farmidinloop%
execute = Gamsrun
ENDFOR
collectparallel
*  execute=Gamsrun
') %>%
writeLines(con = 'test.txt')
readLines('genericbatchfile.txt') %>%  str_replace(pattern = 'Scenario description = \\w*', replacement = paste0('Scenario description = ', 'ABNRegs')) %>%
str_replace_first('Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs')) %>%
stri_replace_last(regex='Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs', '\\n macro = \\n')) %>%
str_replace('farmIds = \\w*', replacement = paste0('farmIds = ', paste0('ABNRegs', '\\'))) %>%
str_replace('execute=Gamsrun', replacement = '  startparallel
FOR farmidinloop = %allfarms%
farmIds  = %farmidinloop%
execute = Gamsrun
ENDFOR
collectparallel
*  execute=Gamsrun
') %>%
writeLines(con = 'test.txt')
readLines('genericbatchfile.txt') %>%  str_replace(pattern = 'Scenario description = \\w*', replacement = paste0('Scenario description = ', 'ABNRegs')) %>%
stri_replace_first(regex='Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs')) %>%
stri_replace_last(regex='Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs', '\\n macro = \\n')) %>%
str_replace('farmIds = \\w*', replacement = paste0('farmIds = ', paste0('ABNRegs', '\\'))) %>%
str_replace('execute=Gamsrun', replacement = '  startparallel
FOR farmidinloop = %allfarms%
farmIds  = %farmidinloop%
execute = Gamsrun
ENDFOR
collectparallel
*  execute=Gamsrun
') %>%
writeLines(con = 'test.txt')
readLines('genericbatchfile.txt') %>%  str_replace(pattern = 'Scenario description = \\w*', replacement = paste0('Scenario description = ', 'ABNRegs')) %>%
stri_replace_first(regex='Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs')) %>%
stri_replace_last(regex='Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs', '\n macro = \n')) %>%
str_replace('farmIds = \\w*', replacement = paste0('farmIds = ', paste0('ABNRegs', '\\'))) %>%
str_replace('execute=Gamsrun', replacement = '  startparallel
FOR farmidinloop = %allfarms%
farmIds  = %farmidinloop%
execute = Gamsrun
ENDFOR
collectparallel
*  execute=Gamsrun
') %>%
writeLines(con = 'test.txt')
readLines('genericbatchfile.txt') %>%  str_replace(pattern = 'Scenario description = \\w*', replacement = paste0('Scenario description = ', 'ABNRegs')) %>%
stri_replace_first(regex='Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs')) %>%
stri_replace_last(regex='Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs', '\n  macro = \b')) %>%
str_replace('farmIds = \\w*', replacement = paste0('farmIds = ', paste0('ABNRegs', '\\'))) %>%
str_replace('execute=Gamsrun', replacement = '  startparallel
FOR farmidinloop = %allfarms%
farmIds  = %farmidinloop%
execute = Gamsrun
ENDFOR
collectparallel
*  execute=Gamsrun
') %>%
writeLines(con = 'test.txt')
readLines('genericbatchfile.txt') %>%  str_replace(pattern = 'Scenario description = \\w*', replacement = paste0('Scenario description = ', 'ABNRegs')) %>%
stri_replace_first(pattern='Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs')) %>%
stri_replace_last(pattern='Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs', '\n  macro = \b')) %>%
str_replace('farmIds = \\w*', replacement = paste0('farmIds = ', paste0('ABNRegs', '\\'))) %>%
str_replace('execute=Gamsrun', replacement = '  startparallel
FOR farmidinloop = %allfarms%
farmIds  = %farmidinloop%
execute = Gamsrun
ENDFOR
collectparallel
*  execute=Gamsrun
') %>%
writeLines(con = 'test.txt')
readLines('genericbatchfile.txt') %>%  str_replace(pattern = 'Scenario description = \\w*', replacement = paste0('Scenario description = ', 'ABNRegs')) %>%
stri_replace_first(regex='Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs')) %>%
stri_replace_last(regex='Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs', '\n  macro = \b')) %>%
str_replace('farmIds = \\w*', replacement = paste0('farmIds = ', paste0('ABNRegs', '\\'))) %>%
str_replace('execute=Gamsrun', replacement = '  startparallel
FOR farmidinloop = %allfarms%
farmIds  = %farmidinloop%
execute = Gamsrun
ENDFOR
collectparallel
*  execute=Gamsrun
') %>%
writeLines(con = 'test.txt')
stri_replace_last('farmdata farmdata farmdata', regex = 'farmdata', replacement = 'AAAAAAAAAAAAAAAAAAAAAA')
stri_replace_first('farmdata farmdata farmdata', regex = 'farmdata', replacement = 'AAAAAAAAAAAAAAAAAAAAAA')
a<- a %>%   stri_replace_first(regex='Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs'))
a <-  a %>%  stri_replace_last(regex='Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs', '\n  macro = \b'))
a %>%   str_replace('farmIds = \\w*', replacement = paste0('farmIds = ', paste0('ABNRegs', '\\'))) %>%
str_replace('execute=Gamsrun', replacement = '  startparallel
FOR farmidinloop = %allfarms%
farmIds  = %farmidinloop%
execute = Gamsrun
ENDFOR
collectparallel
*  execute=Gamsrun
') %>%
writeLines(con = 'test.txt')
readLines('genericbatchfile.txt') %>%  str_replace(pattern = 'Scenario description = \\w*', replacement = paste0('Scenario description = ', 'ABNRegs')) %>%
stri_replace_first(regex='Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs')) %>%
stri_replace_last(regex='Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs', '\n  macro = \b')) %>%
str_replace('farmIds = \\w*', replacement = paste0('farmIds = ', paste0('ABNRegs', '\\'))) %>%
str_replace('execute=Gamsrun', replacement = '  startparallel
FOR farmidinloop = %allfarms%
farmIds  = %farmidinloop%
execute = Gamsrun
ENDFOR
collectparallel
*  execute=Gamsrun
') %>%
writeLines(con = 'test.txt')
## Library loading ----
library(gdxrrw)
library(data.table)
library(arsenal)
library(tidyverse)
## Directory specification ----
GAMSDir     = "C:/GAMS/40" # Change to your GAMS Version!
#ADCDir     = "C:/FARMDYNDATA/ADC"
BINDir     = "C:/FARMDYNDATA/BIN"
#LMMDir     = "W:\\WECR\\PIA\\PW\\DZK\\GHG2020\\output Jakob"
MyLMMDir     = "C:/FARMDYNDATA/LMM"
MyBINDir     = "C:/FARMDYNDATA/MYBIN"
MyDRAMDir     = "C:/FARMDYNDATA/DRAM"
FDDir     = "C:/FARMDYNTRUNK/DAT"
WWLDir     <- "C:/FARMDYNDATA/WWL"
RDir     = "C:/FARMDYNDATA/R"
### Filenames ----
farmdata2abn <- "farmdata2ABN.gdx" # For testing
# GAMS loading
igdx(GAMSDir)
# Working directory
setwd(RDir)
setwd("C:/FARMDYNDATA/R/DynR")
readLines('genericbatchfile.txt') %>%  str_replace(pattern = 'Scenario description = \\w*', replacement = paste0('Scenario description = ', 'ABNRegs')) %>%
stri_replace_first(regex='Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs')) %>%
stri_replace_last(regex='Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs', '\n  macro = \b')) %>%
str_replace('farmIds = \\w*', replacement = paste0('farmIds = ', paste0('ABNRegs', '\\'))) %>%
str_replace('execute=Gamsrun', replacement = '  startparallel
FOR farmidinloop = %allfarms%
farmIds  = %farmidinloop%
execute = Gamsrun
ENDFOR
collectparallel
*  execute=Gamsrun
') %>%
writeLines(con = 'test.txt')
library(stringr)
library(stringi)
readLines('genericbatchfile.txt') %>%  str_replace(pattern = 'Scenario description = \\w*', replacement = paste0('Scenario description = ', 'ABNRegs')) %>%
stri_replace_first(regex='Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs')) %>%
stri_replace_last(regex='Farm sample file = \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs', '\n  macro = \b')) %>%
str_replace('farmIds = \\w*', replacement = paste0('farmIds = ', paste0('ABNRegs', '\\'))) %>%
str_replace('execute=Gamsrun', replacement = '  startparallel
FOR farmidinloop = %allfarms%
farmIds  = %farmidinloop%
execute = Gamsrun
ENDFOR
collectparallel
*  execute=Gamsrun
') %>%
writeLines(con = 'test.txt')
a<-readLines(con='test.txt')
a
a<-readLines(con='test.txt')
readLines(con='feiktekst.txt') %>% stri_replace_first(regex = 'AAAA', 'Changed')
readLines(con='feiktekst.txt') %>% stri_replace_first(regex = 'AAAA', 'Changed') %>% writeLines(con='feiktekst.txt')
readLines(con='feiktekst.txt') %>% sub('AAAAA', 'Changed') %>% writeLines(con='feiktekst.txt')
readLines('genericbatchfile.txt')[1]
readLines('genericbatchfile.txt')[1:10]
readLines('genericbatchfile.txt')[1:11]
readLines('genericbatchfile.txt') %>% sub(pattern = 'Farm sample file= \\w*', replacement = paste0('Farm sample file = ', 'farmData_', 'ABNRegs', '\n  macro = \b'))
readLines(con='feiktekst.txt') %>% stri_locate_first_fixed('AAAAA')
readLines(con='feiktekst.txt') %>% stri_locate_first_fixed('AAAAA')[,1]
readLines(con='feiktekst.txt') %>% stri_locate_first_fixed(pattern = 'AAAAA')[,1]
readLines(con='feiktekst.txt') %>% stri_locate_first_fixed(pattern = 'AAAAA')[1,]
readLines(con='feiktekst.txt') %>% stri_locate_first_fixed(pattern = 'AAAAA')
stri_locate_first_fixed(readLines(con='feiktekst.txt'), pattern = 'AAAAA')
stri_locate_first_fixed(readLines(con='feiktekst.txt'), pattern = 'AAAAA')[,1]
stri_locate_first_fixed(readLines('genericbatchfile.txt'), pattern = 'Farm sample file = \\w*')[,1]
stri_locate_first_fixed(readLines('genericbatchfile.txt'), pattern = 'Farm sample file =')[,1]
rownames(stri_locate_first_fixed(readLines('genericbatchfile.txt'), pattern = 'Farm sample file =')[,1])