Showing posts with label knitr. Show all posts
Showing posts with label knitr. Show all posts
Sunday, September 20, 2015
knitr-Example: Use World Bank Data to Generate Report for Threatened Bird Species
I'll use the below script that retrieves data for threatened bird species from the World Bank via its API and does some processing, plotting and analysis. There is a package (WDI) that allows you to access the data easily.
BTW, for simplicity I use knitr::stitch with its default template...
You should get something like THIS PDF.
EDIT, MARCH 2013
OUTDATED! you can use this approach instead:
Read more »
# world bank indicators for species -For generating the report you can source the script from dropbox.com and stitch it in this fashion:
# I'll check bird species:
code <- as.character(WDIsearch("bird")[1,1])
bird_data <- WDI(country="all", indicator=code, start=2010, end=2012)
# remove NAs and select values in the range 50 - 1000:
bird_data_sub <- bird_data[!is.na(bird_data$EN.BIR.THRD.NO)&
bird_data$EN.BIR.THRD.NO < 1000&
bird_data$EN.BIR.THRD.NO > 50, ]
# change in numbers across years 2010 and 2011:
change.no <- aggregate(EN.BIR.THRD.NO ~ country, diff,
data = bird_data_sub)
# plot:
par(mar = c(3, 3, 5, 1))
plot(x = change.no[,2], y = 1:nrow(change.no),
xlim = c(-12, 12), xlab = "", ylab = "",
yaxt = "n")
abline(v = 0, lty = 2, col = "grey80")
title(main = "Change in Threatened Bird Species in\nCountries with Rich Avifauna (>50)")
text(y = 1:nrow(change.no),
x = -2, adj = 1,
labels = change.no$country)
segments(x0 = 0, y0 = 1:nrow(change.no),
x1 = change.no[, 2], y1 = 1:nrow(change.no))
# test hypothesis that probability of species decrease is
# equal to probability of increase:
binom.test(sum(change.no < 0), sum(change.no != 0))
stitch("http://dl.dropbox.com/s/ga0qbk1o17n17jj/Change_threatened_species.R")..this is one line of code - can you dig it?..
BTW, for simplicity I use knitr::stitch with its default template...
You should get something like THIS PDF.
EDIT, MARCH 2013
OUTDATED! you can use this approach instead:
library(knitr); library(RCurl); library(WDI)
destfile = "script.txt"
x = getBinaryURL("https://dl.dropbox.com/s/ga0qbk1o17n17jj/Change_threatened_species.R", followlocation = TRUE, ssl.verifypeer = FALSE)
writeBin(x, destfile, useBytes = TRUE)
source(paste(tempdir(), "/script.txt", sep = ""))
stitch(paste(tempdir(), "/script.txt", sep = ""))
Saturday, September 19, 2015
Online Questionnaire & Report Generation with Google Drive & R
Here's how I did it in 3 easy steps:
(1) Set up a form in Google Docs/Drive.
(2) Choose "Actions" and "Embed in Website" to get the URL for the iframe and put it in a post, like below. Then, go to the spreadsheet view of the form on Google Docs/Drive and set the share option to "everyone with the link" or "public" and copy the document key from this URL: https://docs.google.com/spreadsheet/ccc?key=0AmwAunwURQNsdFplUTBZUTRLREtLUDhabGxBMHBRWmc
(3) Make a report with knitr in R using the responses that were put to a spreadsheet at Google Docs/Drive. Do (knit) something like THIS to produce a markdown-file, i.e. (I pushed the md-file to Github for publishing - see it rendered to HTML HERE)..
Read more »
(1) Set up a form in Google Docs/Drive.
(2) Choose "Actions" and "Embed in Website" to get the URL for the iframe and put it in a post, like below. Then, go to the spreadsheet view of the form on Google Docs/Drive and set the share option to "everyone with the link" or "public" and copy the document key from this URL: https://docs.google.com/spreadsheet/ccc?key=0AmwAunwURQNsdFplUTBZUTRLREtLUDhabGxBMHBRWmc
(3) Make a report with knitr in R using the responses that were put to a spreadsheet at Google Docs/Drive. Do (knit) something like THIS to produce a markdown-file, i.e. (I pushed the md-file to Github for publishing - see it rendered to HTML HERE)..
Playing with knitr: Create Report with Dynamic List
Here is a little toy example using knitr, LaTeX/MiKTeX and Google Docs.
Say you had a list on Google Docs (say a list of attendants) and you want to print a report with it..
Then see this example using this Rnw-file and the output...
make the tex-file with:
or with this shortcut:
Read more »
Say you had a list on Google Docs (say a list of attendants) and you want to print a report with it..
Then see this example using this Rnw-file and the output...
make the tex-file with:
library(knitr)..then compile the tex-file with MiKTeX.
knit("knitr_list_of_attendants.Rnw")
or with this shortcut:
knit2pdf("knitr_list_of_attendants.Rnw")
browseURL("knitr_list_of_attendants.pdf")
Subscribe to:
Posts (Atom)