全部版块 我的主页
论坛 计量经济学与统计论坛 五区 计量经济学与统计软件 LATEX论坛
1431 1
2016-06-18
Create Multiple Reports with RMarkdownRich Majerus

Introduction

Using two files (an r script and an rmarkdown script) we can create a series of reports. The following example creates five reports using the first five rows of the mtcars data. The rmarkdown file is called by the rscript one time for each unique car name in the subset of the mtcars data. An example of the reports produced by these files can be found here.

The rscript and rmarkdown script referenced in this tutorial can be found here along with the rmarkdown file that creates this tutorial.



File 1: R Script# References for automation # http://www.r-bloggers.com/how-to ... automator-and-ical/# http://www.engadget.com/2013/03/ ... s-in-mountain-lion/# File 1: Should be an R-Script     # contains a loop that iteratively calls an Rmarkdown file (i.e. File 2)# load packageslibrary(knitr)library(markdown)library(rmarkdown)# use first 5 rows of mtcars as example datamtcars <- mtcars[1:5,# create map to plot data on (this is outside the loop so it is only called once)map <-  get_map(location="United States",          source= 'google', maptype = 'terrain', color='bw', zoom=4) # for each type of car in the data create a report  # these reports are saved in output_dir with the name specified by output_filefor (car in unique(rownames(mtcars))){  rmarkdown::render('/Users/majerus/Desktop/R/auto_reporting/test/r_script.Rmd',  # file 2                   output_file =  paste("report_", car, '_', Sys.Date(), ".html", sep=''),                    output_dir = '/Users/majerus/Desktop/R/auto_reporting/test/reports')# for pdf reports  #   rmarkdown::render(input = "/Users/majerus/Desktop/R/auto_reporting/test/r_script_pdf.Rmd", #           output_format = "pdf_document",#           output_file = paste("test_report_", car, Sys.Date(), ".pdf", sep=''),#           output_dir = "/Users/majerus/Desktop/R/auto_reporting/test/reports")  }

File 2: RmarkdownsuppressWarnings(suppressPackageStartupMessages(library(ggplot2)))suppressWarnings(suppressPackageStartupMessages(library(dplyr)))suppressWarnings(suppressPackageStartupMessages(library(leaflet)))suppressWarnings(suppressPackageStartupMessages(library(DT)))suppressWarnings(suppressPackageStartupMessages(library(stringr)))library(knitr)library(markdown)library(rmarkdown)# run report for current car listed in loop in R filecars <- mtcars[rownames(mtcars)==car,# create example data x <- sample(1:10, 1)cars <- do.call("rbind", replicate(x, cars, simplify = FALSE))# create hypothetical lat and lon data cars$lat <- sapply(rownames(cars), function(x) round(runif(1, 30, 46), 3))cars$lon <- sapply(rownames(cars), function(x) round(runif(1, -115, -80),3))

Today is 2015-04-22.


Today we sold cars to people in the following locations:

# map of cars soldleaflet(cars) %>%  addTiles() %>%  setView(-93.65, 42.0285, zoom = 3) %>%  addCircles(cars$lon, cars$lat)

Here is a data table of the cars we sold.

# data table of cars sold datatable(cars[,c(1:2, 12:13))

Here is a plot of mpg vs. weight for the sold cars:

# plot ggplot(cars, aes(mpg, wt)) +   geom_point(position = position_jitter(w = 0.1, h = 0.1)) +   stat_smooth(method="lm", se=TRUE)


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2016-6-18 20:35:42
发了不少 R markdown 的帖子,辛苦了。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群