.gitignore 0 → 100644
.Rproj.user
.Rhistory
.RData
.Ruserdata
DESCRIPTION 0 → 100644
Package: FarmDynR
Type: Package
Title: Run FarmDyn from R and create sample farms
Date: 2022-10-03
Version: 0.2.0
Authors@R: c(person('Hugo', 'Scherer', role=c('aut', 'cre'), email="hugo.scherer@wur.nl"),
person('Marc', 'Müller', role='ctb', email="marc.muller@wur.nl"))
Author: Hugo Scherer [aut, cre]
Marc Müller [ctb]
Maintainer: Hugo Scherer <hugo.scherer@wur.nl>
Description: This package allows the user to run FarmDyn from R and includes useful functions such as:
Modes() to retrieve the mode of any set of numbers or characters in a dataset or vector, and
groupstats() to generate descriptive statistics used for reporting based on a grouping.
It also writes gdx files for groupstats() and samplr(), and includes a faster version of gdxrrw:wgdx.reshape() called gdxreshape()
Depends: R (>= 4.2.1)
Imports: tidyverse, gdxrrw
License: GPL (>= 3)
Encoding: UTF-8
LazyData: true
URL: https://www.wur.nl/
RoxygenNote: 7.2.1
Suggests:
stringr (>= 1.4.1)
FarmDynR.Rproj 0 → 100644
Version: 1.0
RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: Sweave
LaTeX: pdfLaTeX
AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace,vignette
LICENSE.md 0 → 100644
This diff is collapsed.
NAMESPACE 0 → 100644
# Generated by roxygen2: do not edit by hand
export(Modes)
export(gdxbinwider)
export(gdxreshape)
export(groupstats)
export(runFarmDynfromBatch)
export(samplr)
export(str_firstLine_replace)
export(str_lastLine_replace)
export(str_line_replace)
export(writeBatch)
NEWS.md 0 → 100644
# FarmDynR 0.2.0
* Added a `NEWS.md` file to track changes to the package.
* Added `str_line_replace()` and `writeBatch()` functions
R/FarmDynR.R 0 → 100644
This diff is collapsed.
R/FarmDynR.html 0 → 100644
This diff is collapsed.
R/data.R 0 → 100644
#' FarmDynR example data
#'
#' Random fake data used to play with the FarmDynR package.
#'
#' @docType data
#'
#' @usage data(fd2gui); data(fdnl); data(gdxmapping)
#'
#' @format Objects of class `tbl_df`
#'
#' @keywords datasets, FarmDynR
#'
#' @examples
#' data(fd2gui)
#' data(fdnl)
#' data(gdxmapping)
'fd2gui'
README.Rmd 0 → 100644
---
output: rmarkdown::github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# FarmDynR
Developed by Hugo Scherer and Marc Müller at Wageningen Economic Research.
<!-- badges: start -->
<!-- badges: end -->
The goal of FarmDynR is to give the user the ability to aggregate FADN Data from GDX files to create representative farms for any grouping, generate descriptive statistics, and to run FarmDyn from R.
## Requirements
This package requires that you have GAMS and FarmDyn installed. Additionally, the package imports from:
- gdxrrw (remember to load GAMS with `igdx()` with the path to your version of GAMS)
- tidyverse
It also suggests:
- stringr
## Installation
You can install the development version of FarmDynR like so:
``` r
install.packages('https://gitlab.iiasa.ac.at/mind-step/FarmDynR')
```
## Workflow
1. Create GDX files with the mappings, global settings per farm, farm data, and weights
2. Load everything into R with `gdxbinwider()`
3. Use `samplr()` to create sample farms based on your selected mapping
4. Write batch file with `writeBatch()` and run FarmDyn with `runFarmDynfromBatch()` using the created batch file
- Optional: Create descriptive statistics for reporting with `groupstats()`
## Example
This is a basic example which shows you how to solve a common problem:
```{r example, eval=FALSE, include=TRUE}
library(FarmDynR)
## basic example code
groupstats(filename = 'FarmDynRexampledata.gdx', BINDir = 'inst/extdata/GAMS/', gdxmap = gdxmapping, mapping = mapping, cols = c(a,b,c), w = Weight, writegdx = FALSE, filtern = FALSE) # Bad, won't work
groupstats(filename = 'FarmDynRexampledata.gdx', BINDir = 'inst/extdata/GAMS/', gdxmap = 'gdxmapping', mapping = 'mapping', cols = c('a','b','c'), w = 'Weight', writegdx = FALSE, filtern = FALSE) # Good, there can also be more than one mapping with c('mapping1', 'mapping2'...)
```
README.docx 0 → 100644
File added
# FarmDynR
## Getting started
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
```
cd existing_repo
git remote add origin https://gitlab.iiasa.ac.at/mind-step/farmdynr.git
git branch -M main
git push -uf origin main
```
## Integrate with your tools <!-- README.md is generated from README.Rmd. Please edit that file -->
- [ ] [Set up project integrations](https://gitlab.iiasa.ac.at/mind-step/farmdynr/-/settings/integrations) # FarmDynR
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template. Developed by Hugo Scherer and Marc Müller at Wageningen Economic
Research.
## Suggestions for a good README <!-- badges: start -->
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. <!-- badges: end -->
## Name The goal of FarmDynR is to give the user the ability to aggregate FADN
Choose a self-explaining name for your project. Data from GDX files to create representative farms for any grouping,
generate descriptive statistics, and to run FarmDyn from R.
## Description ## Requirements
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges This package requires that you have GAMS and FarmDyn installed.
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. Additionally, the package imports from: - gdxrrw (remember to load GAMS
with `igdx()` with the path to your version of GAMS) - tidyverse
## Visuals It also suggests: - stringr
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation ## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support You can install the development version of FarmDynR like so:
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap ``` r
If you have ideas for releases in the future, it is a good idea to list them in the README. install.packages('https://gitlab.iiasa.ac.at/mind-step/FarmDynR')
```
## Contributing ## Workflow
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. 1. Create GDX files with the mappings, global settings per farm, farm
data, and weights
2. Load everything into R with `gdxbinwider()`
3. Use `samplr()` to create sample farms based on your selected mapping
4. Write batch file with `writeBatch()` and run FarmDyn with
`runFarmDynfromBatch()` using the created batch file
- Optional: Create descriptive statistics for reporting with
`groupstats()`
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. ## Example
## Authors and acknowledgment This is a basic example which shows you how to solve a common problem:
Show your appreciation to those who have contributed to the project.
## License ``` r
For open source projects, say how it is licensed. library(FarmDynR)
## basic example code
groupstats(filename = 'FarmDynRexampledata.gdx', BINDir = 'inst/extdata/GAMS/', gdxmap = gdxmapping, mapping = mapping, cols = c(a,b,c), w = Weight, writegdx = FALSE, filtern = FALSE) # Bad, won't work
## Project status groupstats(filename = 'FarmDynRexampledata.gdx', BINDir = 'inst/extdata/GAMS/', gdxmap = 'gdxmapping', mapping = 'mapping', cols = c('a','b','c'), w = 'Weight', writegdx = FALSE, filtern = FALSE) # Good, there can also be more than one mapping with c('mapping1', 'mapping2'...)
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. ```
library(dplyr)
library(tidyr)
library(arsenal)
library(gdxrrw)
GAMSDir = "C:/GAMS/40" # Change to your GAMS Version!
igdx(GAMSDir)
set.seed(1234)
abcd <- c(stringi::stri_rand_strings(80,5))
mapping <- rep(LETTERS[5:9], times = c(10, 10, 40, 60, 68))
set.seed(1234)
all_binid <- c(sample(1:187), 'dummy')
set.seed(1234)
item <- c('global', 'yields', 'maxrot', 'misc')
set.seed(1234)
value <- rnorm(15040, mean = 0, sd=100)
a <- data.frame(item,abcd) %>% tibble()
a[a$item=='global',]$abcd[19:20] <- c('soilTypeFirm', 'derogatie')
map2id <- data.frame(all_binid = as.factor(all_binid), grouping = as.factor(mapping))
fadat2gui <- data.frame(all_binid = as.factor(all_binid), a, value=value)
test<- fadat2gui[fadat2gui$item=='global',]$value
#fadat2gui[fadat2gui$all_binid=='dummy',]$item <- 'global'
set.seed(1234)
fadat2gui[fadat2gui$all_binid=='dummy',]$value[sample(nrow(fadat2gui[fadat2gui$all_binid=='dummy',]),3)] <- c(1.6e+303, 1.59e+303, 1.56e+303)
set.seed(1234)
fdnl <- data.frame(all_binid = as.factor(all_binid[1:94]),
year = rep(2017:2020, c(5, 11, 15, 16)),
items = c(stringi::stri_rand_strings(94,5),rep('Weight', 94)),
value = rnorm(94, mean=0, sd=40)
)
set.seed(1234)
fdnl[fdnl$items == 'Weight',]$value <- runif(94, min = 0.01)
write.csv(fadat2gui, 'inst/extdata/GAMS/fadat2gui.csv')
write.csv(fdnl, 'inst/extdata/GAMS/fdnl.csv')
write.csv(map2id, 'inst/extdata/GAMS/map2id.csv')
# Run GAMS
# After running GAMS
fd2gui <- (gdxrrw::rgdx.param("inst/extdata/GAMS/DynRexampledata.gdx", "p_farmData2GUI", names=c("all_binid", 'items', "varias", "value"), compress=FALSE, ts=FALSE,
squeeze=TRUE, useDomInfo = TRUE, check.names = TRUE))
fdnl <- (gdxrrw::rgdx.param("inst/extdata/GAMS/DynRexampledata.gdx", "p_farmData_NL", names=c("all_binid", 'years', "items2", "value"), compress=FALSE, ts=FALSE,
squeeze=TRUE, useDomInfo = TRUE, check.names = TRUE))
gdxmapping <- (rgdx.set("inst/extdata/GAMS/DynRexampledata.gdx", 'map2binid',names = c('all_binid', 'mapping'), compress=FALSE, ts=FALSE,
useDomInfo = TRUE, check.names = TRUE))
save(fd2gui, file = 'data/fd2gui.RData')
save(fdnl, file = 'data/fdnl.RData')
save(gdxmapping, file = 'data/gdxmapping.RData')
groupstats('DynRexampledata.gdx', BINDir="inst/extdata/GAMS/", gdxmap = 'map2binid', mapping = 'mapping', cols = c('d0EfV', 'hXHvI'), w='Weight')
data/fd2gui.RData 0 → 100644
File added
data/fdnl.RData 0 → 100644
File added
File added
File added
This diff is collapsed.
File added
Set
items 'set of fake items'
all_binid 'fake id numbers'
mapping 'fake mapping used'
map2binid 'fake mapping to fake bin id'
varias 'fake variables used'
year 'fake years'
items2 'fdnl items';
$gdxIn fd2gui.gdx
$load all_binid
$load varias
$load items
Parameter p_farmData2GUI(all_binid, items, varias) 'Fake Random farmdata2gui';
$LOAD p_farmData2GUI
$gdxIn
$gdxIn fdnl.gdx
$load year
$load items2
Parameter p_farmData_NL(all_binid, year, items2) 'Fake Random farmData NL';
$LOAD p_farmData_NL
$gdxIn
$gdxIn map2id.gdx
$LOAD mapping
$LOAD map2binid
execute_unload 'DynRexampledata.gdx'
all_binid
year
items
items2
mapping
map2binid
varias
p_farmData_NL
p_farmData2GUI