From 6438bbab9eec1e45e1b729db8b5101e6405f3027 Mon Sep 17 00:00:00 2001 From: Daniel Falster <daniel.falster@mq.edu.au> Date: Mon, 16 Sep 2013 11:38:44 +1000 Subject: [PATCH] Fix package loading function, needs character.only =TRUE for library function to work in loop' --- R/format.data/TRY.R | 21 ++++++++------------- R/packages.R | 5 +++-- readme.md | 14 +++++++------- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/R/format.data/TRY.R b/R/format.data/TRY.R index 8a0e202..af9c8a8 100644 --- a/R/format.data/TRY.R +++ b/R/format.data/TRY.R @@ -6,10 +6,9 @@ ################ use AccSpeciesName because not author name source("../format.data/try.fun.R") -library(MASS) -library(doParallel) -library(mvoutlier) -dir.create("../../output/formatted/TRY", recursive=TRUE,showWarnings=FALSE) + +out.dir <- "../../output/formatted/TRY" +dir.create(out.dir, recursive=TRUE,showWarnings=FALSE) ## read TRY data TRY.DATA <- read.table("../../data/raw/TRY/TRY_Proposal_177_DataRelease_2013_04_01.txt", @@ -51,7 +50,7 @@ TRY.DATA.FORMATED <- foreach(ObservationID.t = unique(TRY.DATA$ObservationID), . ## head(TRY.DATA.FORMATED) dim(TRY.DATA.FORMATED) -saveRDS(TRY.DATA.FORMATED, file = "../../data/formatted/TRY/TRY.DATA.FORMATED.rds") +saveRDS(TRY.DATA.FORMATED, file = file.path(out.dir, "TRY.DATA.FORMATED.rds")) #### TODO MODIFY THE FUNCTION TO KEEP TRAITS SD AND INCLUDE IT LATER ON @@ -65,8 +64,7 @@ traits <- c("StdValue.Leaf.nitrogen..N..content.per.dry.mass", "StdValue.Seed.ma data.TRY.std <- subset(TRY.DATA.FORMATED,select=c("ObservationID","AccSpeciesName","TF.exp.data","Reference","Sun.vers..shade.leaf.qualifier",traits)) names(data.TRY.std) <- c("obs.id","Latin_name","TF.exp.data","Reference","Sun.vs.shade.leaf","Leaf.N", "Seed.mass", "SLA", "Wood.density","height") -saveRDS(data.TRY.std,file="../../output/formatted/TRY/data.TRY.std.rds") - +saveRDS(data.TRY.std,file= file.path(out.dir, "data.TRY.std.rds")) ##################################################################### @@ -151,15 +149,12 @@ names(sd.vec.genus) <- c("sdlog10.gs.Nmass", "sdlog10.gs.Seed.Mass", "sdlog10.gs "sdlog10.gs.WD", "sdlog10.gs.Height") ## save mean species and genus sd -saveRDS(sd.vec.sp, file = "../../data/formatted/TRY/sd.vec.sp.rds") -saveRDS(sd.vec.genus, file = "../../data/formatted/TRY/sd.vec.genus.rds") - - - +saveRDS(sd.vec.sp, file = file.path(out.dir, "sd.vec.sp.rds")) +saveRDS(sd.vec.genus, file = file.path(out.dir, "sd.vec.genus.rds")) ### plot sd to show mark -pdf("../../figs/sd.traits.pdf") +pdf(file.path(out.dir, "sd.traits.pdf")) r <- barplot(sd.vec.sp, names.arg = c("Leaf.N", "SM", "SLA", "WD", "Vessel", "LL"), las = 2, ylim = c(0, 0.9), ylab = "sd log10") points(r[, 1], sd.vec.genus, col = "red", pch = 16, cex = 2) diff --git a/R/packages.R b/R/packages.R index be5df41..354f305 100644 --- a/R/packages.R +++ b/R/packages.R @@ -2,10 +2,11 @@ # CHECK PACKGES are install and load check_packages <- function(packages=c("data.table", "dismo", "doParallel", "foreign", "knitr", "maptools", "markdown", "MASS", "mvoutlier", "pander", "quantreg", "RColorBrewer", "reshape", "rgdal", "rjson", "rworldmap", "sp")){ + #install if needed new.packages <- packages[!(packages %in% installed.packages()[,"Package"])] if(length(new.packages)) install.packages(new.packages) - #load - lappply(packages, library, quietly=TRUE) + for(p in packages) + library(p, quietly=TRUE, character.only =TRUE) } diff --git a/readme.md b/readme.md index 2d6e307..68239d7 100644 --- a/readme.md +++ b/readme.md @@ -10,7 +10,7 @@ Kunstler *et al.* (2012) "Competitive interactions between forest trees are driv The repository has the following basic directories: -- `data`: contains raw data and metadata. Treat this as read-only, i.e. do not save output from R scripts here, these go in outputs. +- `data`: contains raw data and meta data. Treat this as read-only, i.e. do not save output from R scripts here, these go in outputs. - `R`: where all R code used in analysis is saved. The only exception is `*.Rmd` files used in creating some of the documents, which are stored in `docs` directory. - `output`: a temporary directory that is used to hold all process data. Material in this directory is not tracked because it can be recreated from scripts. - `docs`: contains documents developed for the project. @@ -19,16 +19,16 @@ Some specific folders of likely interest: - `docs/meeting.agenda`: agenda for workshop - `docs/analysis.outline`: theoretical description of analysis -- `docs/analysis.pipeline`: outline of workflow (code, data etc). +- `docs/analysis.pipeline`: outline of work flow (code, data etc). - `data/raw`: Directory containing all the raw data from different sites. By default, this directory is not available. -- `docs/metadata`: Contains infomration about the dataset from each site +- `docs/metadata`: Contains information about the dataset from each site - `ms`: eventual home for manuscript materials ## Tools We are making use of the following tools: -- *R stats program* +- [*R stats program*](http://www.r-project.org/), for all analyses - A number R libraries. We have embedded a function in the code to load missing packages where possible (see `R/packages.R`) - [*markdown*](http://daringfireball.net/projects/markdown/) - The [*knitr*](http://cran.r-project.org/web/packages/knitr/index.html) package in R, for generating documents with R and markdown. For a n intro see [here](http://nicercode.github.io/guides/reports/) @@ -40,20 +40,20 @@ Documents are kept under version control using git, and stored on github. To acc 1. Sign up to github and send your username to Georges so he can add you to the repository for this workshop, called `trait.competition.workshop` 2. Download repository via github software - - [Github for mac] (http://mac.github.com/) + - [Github for mac](http://mac.github.com/) - [Github for windows](http://windows.github.com/) 3. You also view the material online [here](https://github.com/kunstler/trait.competition.workshop) For an intro to git, see [http://nicercode.github.io/git/](http://nicercode.github.io/git/) -Once downloaded, you can interact with the material via your github software, the shell, or [Rstudio](http://www.rstudio.com/), and R editor with built in git capabilities. +Once downloaded, you can interact with the material via your github software, the shell, or [Rstudio](http://www.rstudio.com/) - an R editor with built in git capabilities. ## Key points for intro - Intro to git and github - version control - text files - - setup: + - set up: - install github, rstudio - R project - Ideal: run on anyone's computer, never use setwd() -- GitLab