Skip to content

Commit

Permalink
Merge pull request #27 from bradlindblad/bug/interactive
Browse files Browse the repository at this point in the history
Bug/interactive fixed, prepping for 0.2.3
  • Loading branch information
bradlindblad authored Nov 2, 2019
2 parents 347f001 + f9e030c commit 4b989e2
Show file tree
Hide file tree
Showing 28 changed files with 85 additions and 75 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Type: Package
Package: tidyUSDA
Title: A Minimal Tool Set for Gathering USDA Quick Stat Data for
Analysis and Visualization
Version: 0.2.2.9000
Version: 0.2.3
Authors@R:
c(person(given = "Brad",
family = "Lindblad",
Expand Down Expand Up @@ -38,7 +38,8 @@ Imports:
sf,
stringi,
tigris,
usethis
usethis,
crayon
Suggests:
knitr,
rgeos,
Expand Down
7 changes: 6 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# tidyUSDA (development version)
# tidyUSDA 0.2.3


# tidyUSDA 0.2.3
* Fixed interacting plotting issue with builds
* Added crayon highlighting to error trapping code

# tidyUSDA 0.2.2
* Added messages for macOS users before they try plotUSDA (graphics issue with underlying geom_sf())
Expand Down
68 changes: 29 additions & 39 deletions R/getQuickstat.R
Original file line number Diff line number Diff line change
Expand Up @@ -294,79 +294,79 @@ getQuickstat <- function(key=NULL, program=NULL, data_item=NULL, sector=NULL, gr

# Install rgeos if not already installed
if (!"rgeos" %in% utils::installed.packages()) {
stop("Package \"rgeos\" needed for this function to work. Please install it with install.packages(\"rgeos\")",
stop(crayon::cyan("Package \"rgeos\" needed for this function to work. Please install it with install.packages(\"rgeos\")"),
call. = FALSE)
}

# Logic to handle improper inputs

# Key
if(is.null(key)){
message("API key missing; enter an API key.")
message(crayon::cyan("API key missing; enter an API key."))
}

# Program
if(is.null(program)){} else if(!toupper(program) %in% tidyUSDA::allProgram){
recommendation <- fuzzyMatch(toupper(program), tidyUSDA::allProgram);
message(paste0("Your value for PROGRAM is not valid. Did you mean ",
recommendation <- crayon::red(fuzzyMatch(toupper(program), tidyUSDA::allProgram));
message(crayon::cyan(paste0("Your value for PROGRAM is not valid. Did you mean ",
recommendation,
"? If not, check tidyUSDA::allProgram for a vector of all acceptable values."))
"? If not, check tidyUSDA::allProgram for a vector of all acceptable values.")))
}

# Data item
if(is.null(data_item)){} else if(!toupper(data_item) %in% tidyUSDA::allDataItem){
recommendation <- fuzzyMatch(toupper(data_item), tidyUSDA::allDataItem);
message(paste0("Your value for DATA_ITEM is not valid. Did you mean ",
recommendation <- crayon::red(fuzzyMatch(toupper(data_item), tidyUSDA::allDataItem));
message(crayon::cyan(paste0("Your value for DATA_ITEM is not valid. Did you mean ",
recommendation,
"? If not, check tidyUSDA::allDataItem for a vector of all acceptable values."))
"? If not, check tidyUSDA::allDataItem for a vector of all acceptable values.")))
}

# Sector
if(is.null(sector)){} else if (!is.null(sector) & !toupper(sector) %in% tidyUSDA::allSector){
recommendation <- fuzzyMatch(toupper(sector), tidyUSDA::allSector);
message(paste0("Your value for SECTOR is not valid. Did you mean ",
recommendation <- crayon::red(fuzzyMatch(toupper(sector), tidyUSDA::allSector));
message(crayon::cyan(paste0("Your value for SECTOR is not valid. Did you mean ",
recommendation,
"? If not, check tidyUSDA::allSector for a vector of all acceptable values."))
"? If not, check tidyUSDA::allSector for a vector of all acceptable values.")))
}

# Group
if(is.null(group)){} else if(!toupper(group) %in% tidyUSDA::allGroup){
recommendation <- fuzzyMatch(toupper(group), tidyUSDA::allGroup);
message(paste0("Your value for GROUP is not valid. Did you mean ",
recommendation <- crayon::red(fuzzyMatch(toupper(group), tidyUSDA::allGroup));
message(crayon::cyan(paste0("Your value for GROUP is not valid. Did you mean ",
recommendation,
"? If not, check tidyUSDA::allGroup for a vector of all acceptable values."))
"? If not, check tidyUSDA::allGroup for a vector of all acceptable values.")))
}

# Commodity
if(is.null(commodity)){} else if(!toupper(commodity) %in% tidyUSDA::allCommodity){
recommendation <- fuzzyMatch(toupper(commodity), tidyUSDA::allCommodity);
message(paste0("Your value for COMMODITY is not valid. Did you mean ",
recommendation <- crayon::red(fuzzyMatch(toupper(commodity), tidyUSDA::allCommodity));
message(crayon::cyan(paste0("Your value for COMMODITY is not valid. Did you mean ",
recommendation,
"? If not, check tidyUSDA::allCommodity for a vector of all acceptable values."))
"? If not, check tidyUSDA::allCommodity for a vector of all acceptable values.")))
}

# Category
if(is.null(category)){} else if(!toupper(category) %in% tidyUSDA::allCategory){
recommendation <- fuzzyMatch(toupper(category), tidyUSDA::allCategory);
message(paste0("Your value for CATEGORY is not valid. Did you mean ",
recommendation <- crayon::red(fuzzyMatch(toupper(category), tidyUSDA::allCategory));
message(crayon::cyan(paste0("Your value for CATEGORY is not valid. Did you mean ",
recommendation,
"? If not, check tidyUSDA::allCategory for a vector of all acceptable values."))
"? If not, check tidyUSDA::allCategory for a vector of all acceptable values.")))
}

# Domain
if(is.null(domain)){} else if(!toupper(domain) %in% tidyUSDA::allDomain){
recommendation <- fuzzyMatch(toupper(domain), tidyUSDA::allDomain);
message(paste0("Your value for DOMAIN is not valid. Did you mean ",
recommendation <- crayon::red(fuzzyMatch(toupper(domain), tidyUSDA::allDomain));
message(crayon::cyan(paste0("Your value for DOMAIN is not valid. Did you mean ",
recommendation,
"? If not, check tidyUSDA::allDomain for a vector of all acceptable values."))
"? If not, check tidyUSDA::allDomain for a vector of all acceptable values.")))
}

# Geographic level
if(is.null(geographic_level)){} else if(!toupper(geographic_level) %in% tidyUSDA::allGeogLevel){
recommendation <- fuzzyMatch(toupper(geographic_level), tidyUSDA::allGeogLevel);
message(paste0("Your value for GEOGRAPHIC_LEVEL is not valid. Did you mean ",
recommendation <- crayon::red(fuzzyMatch(toupper(geographic_level), tidyUSDA::allGeogLevel));
message(crayon::cyan(paste0("Your value for GEOGRAPHIC_LEVEL is not valid. Did you mean ",
recommendation,
"? If not, check tidyUSDA::allGeogLevel for a vector of all acceptable values."))
"? If not, check tidyUSDA::allGeogLevel for a vector of all acceptable values.")))
}


Expand Down Expand Up @@ -403,19 +403,9 @@ if (!"rgeos" %in% utils::installed.packages()) {
state=state, county=county, year=year, geometry = geometry, lower48 = lower48)
}

# Reset variables to a default value if not used
# if(is.null(program)){program <- 'NON'}
# if(is.null(sector)){sector <- 'NON'}
# if(is.null(group)){group <- 'NON'}
# if(is.null(commodity)){commodity <- 'NON'}
# if(is.null(category)){category <- 'NON'}
# if(is.null(data_item)){data_item <- 'NON'}
# if(is.null(domain)){domain <- 'NON'}

if(is.null(geographic_level)){geographic_level <- 'NON'}
# if(is.null(state)){state <- 'NON'}
# if(is.null(county)){county <- 'NON'}
# if(is.null(year)){year <- 'NON'}
#


# STATE
if(geometry & geographic_level == 'STATE'){
Expand Down Expand Up @@ -462,6 +452,6 @@ if (!"rgeos" %in% utils::installed.packages()) {

return(mydata)
}



18 changes: 11 additions & 7 deletions R/plotting.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,27 @@

plotUSDA <- function(df, fill_by = 'Value'){


# Install rgeos if not already installed
if (!"rgeos" %in% utils::installed.packages()) {
stop("Package \"rgeos\" needed for this function to work. Please install it with install.packages(\"rgeos\")",
stop(crayon::cyan("Package \"rgeos\" needed for this function to work. Please install it with install.packages(\"rgeos\")"),
call. = FALSE)
}


# Logic to warn if using Mac OsX - issue with plotting
mac <- FALSE
quartz <- FALSE
stopit <- FALSE
if(stringi::stri_detect(str = utils::osVersion, regex = "mac")) {mac <- TRUE}
if(mac) {


if(identical(getOption("bitmapType"), "quartz")) {quartz <- TRUE}
if(quartz) {

stopit <- usethis::ui_yeah("It appears you are using Mac. Mac OsX has a graphics device that doesn't handle the underlying ggplot2::geom_sf() very well. We recommend you use a package like tmap or leaflet to plot this dataframe. You can still use plotUSDA, it will just take 10 minutes possibly. Do you want to skip plotting using plotUSDA?")
stopit <- message(cat(crayon::cyan("It appears your session is using a", crayon::red("quartz graphics device"), crayon::cyan("- probably macOS. The quartz graphics device doesn't handle the underlying ggplot2::geom_sf() very well. We recommend you either switch to another device, like X11, or use a package like tmap or leaflet to plot this dataframe. You can still use plotUSDA with quartz, it just may take 10 minutes."))))

# if(stopit) {message("Quitting, gonna try another mapping package like tmap or leaflet.")}
}

if(stopit) {stop("Quitting since using Mac, gonna try another mapping package like tmap or leaflet.")}

z <- ggplot2::ggplot(df) +
ggplot2::geom_sf(ggplot2::aes_string(fill = fill_by)) +
ggplot2::coord_sf(datum=NA) +
Expand All @@ -49,3 +52,4 @@ plotUSDA <- function(df, fill_by = 'Value'){

}


2 changes: 1 addition & 1 deletion cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Fixed a few minor bugs.
Per Prof Brian Ripley's request: the plotUSDA() function was testing for the OS, where Professor Ripley rightly pointed out that I need to test the device instead. This version fixes that.

## Test environments
* local Linux Mint 19.2, R 3.6.1
Expand Down
2 changes: 1 addition & 1 deletion docs/404.html

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

2 changes: 1 addition & 1 deletion docs/LICENSE-text.html

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

2 changes: 1 addition & 1 deletion docs/articles/index.html

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

4 changes: 2 additions & 2 deletions docs/articles/using_tidyusda.html

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

2 changes: 1 addition & 1 deletion docs/authors.html

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

2 changes: 1 addition & 1 deletion docs/index.html

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

14 changes: 12 additions & 2 deletions docs/news/index.html

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

2 changes: 1 addition & 1 deletion docs/reference/allCategory.html

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

2 changes: 1 addition & 1 deletion docs/reference/allCommodity.html

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

2 changes: 1 addition & 1 deletion docs/reference/allCounty.html

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

2 changes: 1 addition & 1 deletion docs/reference/allDataItem.html

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

2 changes: 1 addition & 1 deletion docs/reference/allDomain.html

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

2 changes: 1 addition & 1 deletion docs/reference/allGeogLevel.html

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

2 changes: 1 addition & 1 deletion docs/reference/allGroup.html

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

2 changes: 1 addition & 1 deletion docs/reference/allProgram.html

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

2 changes: 1 addition & 1 deletion docs/reference/allSector.html

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

Loading

0 comments on commit 4b989e2

Please sign in to comment.