Skip to content

Commit

Permalink
Get package up and running post database changes #19 #20
Browse files Browse the repository at this point in the history
  • Loading branch information
lukesonnet committed Aug 1, 2017
1 parent b3c72fe commit 155e17c
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 637 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
^\.Rproj\.user$
^appveyor\.yml$
^\.travis\.yml$
^README\.Rmd$
^README-.*\.png$
4 changes: 2 additions & 2 deletions R/download.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ build_votelist <- function(votelist, ids, perrequest) {
use.names = F))))
} else { # Not an error in R

if (!is.null(votes$errormessage)) stop(votes$errormessage)

# If there are rollcalls to add, add them. This chunk prevents errors
# when votes$rollcalls doesn't work because votes are all errors
if(nrow(votes$rollcalls)) {
Expand Down Expand Up @@ -472,7 +470,9 @@ voteview2rollcall <- function(data, keeplong = T) {
#'
#' ## Complete download. Wont work because 'fakeid' will always be invalid,
#' ## however this is just to show usage
#' \dontrun{
#' complete_rc <- complete_download(rc)
#' }
#' @export
#'
complete_download <- function(rc) {
Expand Down
1 change: 0 additions & 1 deletion R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Mode <- function(x) {

# Internal function that returns the URL of the current server
baseurl <- function() {
#return("http://voteview.polisci.ucla.edu")
#return("http://128.97.229.160")
return("https://voteview.com")
}
8 changes: 4 additions & 4 deletions R/rollcallUtilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,14 @@ melt_rollcall <- function(rc,
#'
#' @examples
#'
#' ## Search for rollcalls about Rhodesia in 95th and 96th Congress
#' res <- voteview_search("Rhodesia", congress = c(95, 96))
#' ## Search for rollcalls about Rhodesia in the 96th Congress
#' res <- voteview_search("Rhodesia", congress = c(96))
#'
#' rc1 <- voteview_download(res$id)
#' summary(rc1)
#'
#' ## Search for rollcalls about Rhodesia in 90th and 92nd Congress
#' res <- voteview_search("Rhodesia", congress = c(90, 92))
#' ## Search for rollcalls about Rhodesia in the 90th Congress
#' res <- voteview_search("Rhodesia", congress = c(90))
#'
#' rc2 <- voteview_download(res$id)
#' summary(rc2)
Expand Down
36 changes: 20 additions & 16 deletions R/search.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ voteview_search <- function(q = NULL,

## Replace single quotes ' with double quotes for parser, try to avoid
## apostrophes and also replace the escape slashes that the stri_escape_unicode places around quotes
query_string <- gsub("(?=[^:\\s])\\\\\\'", '"', query_string, perl=TRUE)
query_string <- gsub("\\\\\\'(?=[\\s$])", '"', query_string, perl=TRUE)
query_string <- gsub("(?=[^:\\s])\\'", '"', query_string, perl=TRUE)
query_string <- gsub("\\'(?=[\\s$])", '"', query_string, perl=TRUE)
} else {
query_string <- "()" # This ensures string does not start with boolean
}
Expand Down Expand Up @@ -201,21 +201,25 @@ voteview_search <- function(q = NULL,
message(sprintf("Query '%s' returned %i rollcalls...\n", query_string, resjson$recordcount))

if(!is.null(resjson$errormessage)) warning(resjson$errormessage)
if(resjson$recordcount == 0) stop("No rollcalls found")

res <- resjson$rollcalls
if(resjson$recordcount == 0) {
warning("No rollcalls found")
return(NULL)
} else {
res <- resjson$rollcalls

orderCols <- c("id", "congress", "chamber", "rollnumber", "date", "bill",
"yea_count", "nay_count", "percent_support", "vote_result", "description",
"short_description", "question", "text")
dropCols <- c("result", "vote_counts", "vote_document_text", "vote_desc",
"vote_title", "vote_question", "amendment_author")
renameCols <- list(c("yea_count", "yea"), c("nay_count", "nay"),
c("percent_support", "support"))
res <- cleanDf(res, orderCols, dropCols, renameCols)
attr(res, "qstring") <- query_string

return( res )
}

orderCols <- c("id", "congress", "chamber", "rollnumber", "date", "bill",
"yea_count", "nay_count", "percent_support", "vote_result", "description",
"short_description", "question", "text")
dropCols <- c("result", "vote_counts", "vote_document_text", "vote_desc",
"vote_title", "vote_question", "amendment_author")
renameCols <- list(c("yea_count", "yea"), c("nay_count", "nay"),
c("percent_support", "support"))
res <- cleanDf(res, orderCols, dropCols, renameCols)
attr(res, "qstring") <- query_string

return( res )
}

#' Query the Voteview Database for Members
Expand Down
62 changes: 14 additions & 48 deletions README.Rmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
---
output:
md_document:
variant: markdown_github
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->

Expand All @@ -14,21 +12,23 @@ knitr::opts_chunk$set(
```

# Rvoteview
[![Travis-CI Build Status](https://travis-ci.org/JeffreyBLewis/Rvoteview.svg?branch=master)](https://travis-ci.org/JeffreyBLewis/Rvoteview) [![AppVeyor Build status](https://ci.appveyor.com/api/projects/status/n13u8s0tnfsau1o6?svg=true)](https://ci.appveyor.com/project/lukesonnet/rvoteview)

**WARNING: This package is under construction. Please be patient and feel free to contact [Luke Sonnet](mailto:luke.sonnet@gmail.com) with any questions.**

This is a package that enables you to query the Voteview database for roll calls and work with data frames or a `pscl` `rollcall` object.

To install this package, ensure you have `devtools` installed. If you do not, run `install.packages("devtools")` before doing the following:
```R
To install this package, run the following (note you have to have devtools installed first):
```{r github-install, eval = F}
# install.packages('devtools')
devtools::install_github("JeffreyBLewis/Rvoteview")
```

For more thorough documentation, see the help files for individual functions and the vignette [at this link here](https://github.com/JeffreyBLewis/Rvoteview/tree/master/vignettes). Lastly, there is complete documentation for the searches you can make [at the wiki here](https://github.com/JeffreyBLewis/Rvoteview/wiki/Query-Documentation).
For more thorough documentation, see the help files for individual functions and the vignette [at this link here](https://github.com/JeffreyBLewis/Rvoteview/tree/master/vignettes).

## Quick Start: Using Rvoteview

To use `Rvoteview`, you generally want to search the database to get a list of vote ids and then use those to return the individual votes. We query the database with a search term and some parameters to constrain the search. The default behavior is to search any words as key words, returning the roll calls that best match any key words you enter. Again, for full documentation for the search [see here](https://github.com/JeffreyBLewis/Rvoteview/wiki/Query-Documentation). There are further examples in the [vignette](https://github.com/JeffreyBLewis/Rvoteview/tree/master/vignettes).
To use `Rvoteview`, you generally want to search the database to get a list of vote ids and then use those to return the individual votes. We query the database with a search term and some parameters to constrain the search. The default behavior is to search any words as key words, returning the roll calls that best match any key words you enter. Again, there are further examples in the [vignette](https://github.com/JeffreyBLewis/Rvoteview/tree/master/vignettes).

So let's start with a search for roll calls with the key word "Iraq".
```{r search}
Expand All @@ -38,7 +38,7 @@ res <- voteview_search("Iraq")
names(res)
## I will drop description since it is a very long field
head(res[, -1])
res[1:5, 1:5]
```
Using `res$id` we can get a `rollcall` object (from the [`pscl` package](https://cran.r-project.org/web/packages/pscl/index.html)) that contains the full set of votes and data for each roll call.

Expand All @@ -51,36 +51,19 @@ rc <- voteview_download(res$id[1:10])
summary(rc)
```

We have also begun extending the `pscl` `rollcall` object by allowing you to do a full outer join on two `rollcall` objects that were built using this package. For example:

```{r download2, results='hide'}
rc2 <- voteview_download(res$id[c(5, 100)])
```
```{r pscl-add}
## Now we can merge this with 'rc'
rcAll <- rc %+% rc2
rc$m
rc2$m
rcAll$m # rc$m + rc2$m - 1 because of the one vote overlap
```

You also have lots of metadata on roll calls and legislators in various data frames. For example, we can see some legislator metadata:

```{r metadata}
head(rcAll$legis.long.dynamic)
rc$legis.long.dynamic[1:5, 1:5]
```

You can also search by start and end date, congress, and chamber. Please see the help files for each function after you install the package to see a little more about how they work.

```{r search-options}
## Voteview search with options
res <- voteview_search("Iraq", chamber = "House", congress = 110,
startdate = 2008, enddate = "2008-04-20")
head(res[, -1])
res <- voteview_search("Iraq", congress = 109:112)
head(res[, -1])
res <- voteview_search("Iraq", chamber = "House", congress = 110:112,
startdate = 2008, enddate = "2013-04-20")
res[1:5, 1:5]
```

We can print out the exact query that the function builds using all of these arguments by retrieving the 'qstring' attribute of the returned data frame:
Expand All @@ -94,16 +77,7 @@ We can assemble and use these complex queries ourselves. Here's one example wher
```{r search-adv}
## Voteview search with options
res <- voteview_search("(alltext:estate death tax congress:[100 to 114])")
head(res[, -1])
tail(res[, -1])
```

We can also search for all bills with the keywords "war" and "terror" on or after 2008.

```{r search-adv2}
res <- voteview_search("(alltext:war terror startdate:2008-01-01)")
head(res[, -1])
tail(res[, -1])
res[1:5, 1:5]
```

You can also search for member data using the `member_search` function.
Expand All @@ -112,13 +86,5 @@ You can also search for member data using the `member_search` function.
res <- member_search("Paul",
state = "KY")
## Drop the bio field because it is quite long
head(res[, names(res) != "bio"])
res <- member_search("Paul",
state = "KY",
distinct = 1)
## Drop the bio field because it is quite long
head(res[, names(res) != "bio"])
res[1:5, 1:5]
```
Loading

0 comments on commit 155e17c

Please sign in to comment.