Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
First publication of the gpagespeed package
  • Loading branch information
simitpatel committed Apr 2, 2016
1 parent 60c2fe2 commit 1815ce6
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
^.*\.Rproj$
^\.Rproj\.user$
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
inst/doc
24 changes: 24 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Package: gpagespeed
Title: Google Page Speed API
Version: 0.0.0.9000
Authors@R: person("Simit", "Patel", email = "[email protected]", role = c("aut", "cre"))
Description: A way to call the Google Page Speed Insights API directly into your
R console. The package offers two functions that return results the API in the
form of a data frame. The first function, speedfinder, is to call the results
on a single URL, while the second function, speedlist, accepts a list of URLs
and returns a single dataframe with results from the API for all the URLs in the
list.
Depends:
R (>= 3.2.3)
Imports:
RJSONIO,
gtools
Suggests:
dplyr,
knitr,
rmarkdown,
testthat
License: GPL-3
LazyData: true
RoxygenNote: 5.0.1
VignetteBuilder: knitr
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(speedfinder)
export(speedfinder2)
export(speedlist)
51 changes: 51 additions & 0 deletions R/speed-insights-package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
library(RJSONIO)
library(gtools)

#' Speed results for 1 URL
#'
#' The speedfinder function returns the Google Page Speed Insights test results for a single URL as a dataframe.
#'
#' @param url A URL on which to run the Google Page Speed Insights test.
#' @param strategy Whether the URL should be evaluated in a mobile or desktop context. Accordingly, acceptable values
#' are either the string "mobile" or the string "desktop".
#' @param key A unique key obtainable from Google by registering for free as a Google developer.
#' @examples
#' speedfinder("https://www.cars.com","mobile",key)

#' @export
speedfinder <- function(url,strategy,key) {
pid <- fromJSON(paste0("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=",url,"&strategy=",strategy,"&key=",key))
frame1 <- cbind(as.data.frame(pid[2]),as.data.frame(pid[3]),as.data.frame(pid[5]),as.data.frame(pid[6]))
rbind.data.frame(data.frame(), frame1,make.row.names=FALSE)
}

#' Function within speedlist
#'
#' Speedfinder2 is a function utilized by speedlist to allow a list of URLs to be submitted to the Google Page Speed
#' Insights API.

#' @export
speedfinder2 <- function(url,strategy,key) {
pid <- fromJSON(paste0("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=",url,"&strategy=",strategy,"&key=",key))
frame1 <- cbind(as.data.frame(pid[2]),as.data.frame(pid[3]),as.data.frame(pid[5]),as.data.frame(pid[6]))
}

#' Speed results for a list of URLs
#'
#' The speedlist function submits a list of URLs to the Google Page Speed Insights API, and returns a dataframe in which
#' each row contains the results from the API for a unique URL in the list.
#'
#' @param url A list of URLs on which to run the Google Page Speed Insights test.
#' @param strategy Whether the list of URLs should be evaluated in a mobile or desktop context. Accordingly, acceptable values
#' are either the string "mobile" or the string "desktop".
#' @param key A unique key obtainable from Google by registering for free as a Google developer.
#' @examples
#' speedlist(listofURLs,"mobile",key)

#' @export
speedlist <- function(pagelist,strategy,key) {
list1 <- lapply(pagelist,speedfinder2,strategy,key)
suppressWarnings(do.call("smartbind",list1))
}


21 changes: 21 additions & 0 deletions gpagespeed.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Version: 1.0

RestoreWorkspace: No
SaveWorkspace: No
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
23 changes: 23 additions & 0 deletions man/speedfinder.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions man/speedfinder2.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions man/speedlist.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
library(testthat)
library(gpagespeed)

context("gpagespeed functions")

test_check("gpagespeed")

test_that("speedfinder function", {
expect_equal(class(speedfinder("https://www.cars.com","mobile","AIzaSyCj7QgHqxx81JtITkkZaLTfPM2GEvx8U1Q")),
class(data.frame()))
expect_equal(nrow(speedfinder("https://www.cars.com","mobile","AIzaSyCj7QgHqxx81JtITkkZaLTfPM2GEvx8U1Q")), 1)
})

test_that("speedlist function", {
expect_equal(class(speedlist(c("https://www.cars.com","https://www.yahoo.com","https://www.techmeme.com"),
"mobile","AIzaSyCj7QgHqxx81JtITkkZaLTfPM2GEvx8U1Q")),
class(data.frame()))
expect_equal(nrow(speedlist(c("https://www.cars.com","https://www.yahoo.com","https://www.techmeme.com"),
"mobile","AIzaSyCj7QgHqxx81JtITkkZaLTfPM2GEvx8U1Q")), 3)
})
48 changes: 48 additions & 0 deletions vignettes/my-vignette.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: "Introduction to gpagespeed"
author: "Simit Patel"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Introduction to gpagespeed}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---

## gpagespeed Introduction

gpagespeed is a package designed to facilitate utilization of the Google PageSpeed Insights API. The PageSpeed Insights API provides insight into how Google's bots evaluate a given page, and the factors that web developers may wish to consider in building pages that will render faster. See Google's [documentation on the PageSpeed Insights API](https://developers.google.com/speed/docs/insights/about) to learn more about what the API offers as well as practical examples of its usage.

The PageSpeed Insights API returns a numeric summary of the results of each page, as well as recommendations in text format. Currently, this package is designed to support only retrieving the numeric summary, which includes the overall speed score as well as metrics pertaining to the number of file requests and sizes in kilobytes of images, HTML, CSS, and JavaScript. The package offers two functions: __speedfinder__ and __speedlist__. These functions are documented below.

## Functions in gpagespeed

1.__speedfinder.__ The speedfinder function returns a dataframe with a single observation and columsn of numeric data as its response. It accepts three arguments:

*_url_: The url argument refers to the url of the document on which the PageSpeed Insights API should be run and the corresponding summary metrics returned.
*_strategy_: This argument should have one of two potential text string inputs: "desktop" or "mobile" to denote the device context that the PageSpeed Insights API should use in its evaluation.
*_key_: This is the key that is required to use the API. [Visit Google's Developer Console](https://developers.google.com/console/help/using-keys) to obtain your free key.


1.__speedlist.__ The speedlist function is like the speedfinder function, but allows processing of many URLs and returns a dataframe in which each observation corresponds to a unique URL. Like speedfinder, it accepts three arguments:

*_pagelist_: A list object in which each item in the list is a URL listed as a character string.
*_strategy_: This argument should have one of two potential text string inputs: "desktop" or "mobile" to denote the device context that the PageSpeed Insights API should use in its evaluation.
*_key_: This is the key that is required to use the API. [Visit Google's Developer Console](https://developers.google.com/console/help/using-keys) to obtain your free key.

## Example Usage

Below are examples of how the __speedfinder__ and __speedlist__ functions could be called in an R script.

```{r, results='hide'}
#get speed metrics for a single URL and assign it to a variable
df1 <- speedfinder("https://www.cars.com","mobile","atextkeyigotfromgoogle111")
#get speed metrics for a list of URLs and have it all returned in a single dataframe
df2 <- speedlist(c("https://www.cars.com","http://www.yahoo.com","http://www.techmeme.com"),
"mobile","atextkeyigotfromgoogle111")
```

0 comments on commit 1815ce6

Please sign in to comment.