Skip to content

Commit

Permalink
Processing lintr comments from goodpractice::gp()
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperHG90 committed Feb 5, 2018
1 parent 882eed8 commit 2e6d84b
Show file tree
Hide file tree
Showing 19 changed files with 127 additions and 77 deletions.
7 changes: 4 additions & 3 deletions R/getSurvey.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ getSurvey <- function(surveyID,
if(!force_request) {
if(paste0(surveyID, ".rds") %in% list.files(tempdir())) {
data <- readRDS(paste0(tempdir(), "/", surveyID, ".rds"))
if(verbose) message(paste0("Found an earlier download for survey with id ", surveyID,
". Loading this file. Set 'force_request' to TRUE if you want to override this."))
if(verbose) message(paste0("Found an earlier download for survey with id ", surveyID, # nolint
". Loading this file. Set 'force_request' to TRUE if you want to override this.")) # nolint
return(data)
}
}
Expand Down Expand Up @@ -143,7 +143,8 @@ getSurvey <- function(surveyID,
if(convertVariables) {
data <- inferDataTypes(data, surveyID)
}
# Save survey as RDS file in temp folder so that it can be easily retrieved this session.
# Save survey as RDS file in temp folder so that it can be easily
# retrieved this session.
saveRDS(data, paste0(tempdir(), "/", surveyID, ".rds"))
# Remove tmpfiles
if(!is.null(save_dir)) {
Expand Down
4 changes: 3 additions & 1 deletion R/getSurveyQuestions.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ getSurveyQuestions <- function(surveyID) {
quest <- data.frame(qid = names(qi),
qnames = vapply(qi, function(x) x$questionName, ""),
question = vapply(qi,function(x) x$questionText, ""),
force_resp = vapply(qi, function(x) x$validation$doesForceResponse, TRUE),
force_resp = vapply(qi,
function(x) x$validation$doesForceResponse, # nolint
TRUE),
stringsAsFactors = FALSE)

# Row names
Expand Down
17 changes: 11 additions & 6 deletions R/metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ metadata <- function(surveyID,
allowed <- c("metadata","questions","responsecounts",
"blocks","flow","embedded_data","comments")
check <- union(allowed, names(get))
assertthat::assert_that(length(check) <= length(allowed),
msg="One or more options you passed to 'get' are not valid. Please check your input and try again.")
assertthat::assert_that(length(check) <= length(allowed), # nolint
msg="One or more options you passed to 'get' are not valid. Please check your input and try again.") # nolint
# Change standard options if any
standard_list <- list("metadata"=TRUE,
"questions"=TRUE,
Expand Down Expand Up @@ -97,8 +97,12 @@ metadata <- function(surveyID,
"isActive" = resp_filt$isActive,
"creationDate" = resp_filt$creationDate,
"lastModifiedDate"=resp_filt$lastModifiedDate,
"expiration_startdate"=ifelse(is.null(resp_filt$expiration$startDate),NA,resp_filt$expiration$startDate),
"expiration_endDate"=ifelse(is.null(resp_filt$expiration$endDate),NA,resp_filt$expiration$endDate)
"expiration_startdate"=ifelse(is.null(resp_filt$expiration$startDate),
NA,
resp_filt$expiration$startDate),
"expiration_endDate"=ifelse(is.null(resp_filt$expiration$endDate),
NA,
resp_filt$expiration$endDate)
)
# Response counts
responsecounts <- data.frame(
Expand All @@ -114,7 +118,7 @@ metadata <- function(surveyID,
if(all(q_select %in% qnames)) {
questions <- resp_filt$questions[which(qnames %in% q_select)]
} else {
warning(paste0("One or more questions you queried are not present in your survey. Returning all questions instead."))
warning(paste0("One or more questions you queried are not present in your survey. Returning all questions instead.")) # nolint
questions <- resp_filt$questions
}
} else {
Expand All @@ -129,7 +133,8 @@ metadata <- function(surveyID,
"embedded_data"=resp_filt$embeddedData,
"comments"=resp_filt$comments)
# Make subset
met_ss <- met[names(standard_list[vapply(standard_list,function(x) x==TRUE, TRUE)])]
met_ss <- met[names(standard_list[vapply(standard_list,
function(x) x==TRUE, TRUE)])] # nolint
# Return
return(met_ss)
}
2 changes: 1 addition & 1 deletion R/qualtRicsConfigFile.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ qualtRicsConfigFile <- function(api_token = NULL, root_url=NULL, verbose=TRUE,
useLocalTime=FALSE, dateWarning=TRUE) {

msg <- paste0(
"Copy-paste the lines between the dashes into a new plain text file, replace the values for the api_token and root_url if they are not yet filled out. and save it in your working directory as '.qualtRics.yml'. Execute '?qualtRics::qualtRicsConfigFile' to view an explanation of the additional arguments. Visit https://github.com/JasperHG90/qualtRics/blob/master/README.md#using-a-configuration-file for more information.", "\n\n",
"Copy-paste the lines between the dashes into a new plain text file, replace the values for the api_token and root_url if they are not yet filled out. and save it in your working directory as '.qualtRics.yml'. Execute '?qualtRics::qualtRicsConfigFile' to view an explanation of the additional arguments. Visit https://github.com/JasperHG90/qualtRics/blob/master/README.md#using-a-configuration-file for more information.", "\n\n",# nolint
"--------------","\n",
'api_token: ', ifelse(is.null(api_token), '<YOUR-API-TOKEN-HERE>',
paste0(api_token)), "\n",
Expand Down
9 changes: 5 additions & 4 deletions R/readSurvey.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ readSurvey <- function(file_name,
skip = skipNr))
# Need contingency when 0 rows
assertthat::assert_that(nrow(rawdata) > 0,
msg="The survey you are trying to import has no responses.")
msg="The survey you are trying to import has no responses.") # nolint
# Load headers
header <- suppressMessages(readr::read_csv(file = file_name,
col_names = TRUE,
n_max = 1))
# make them data.frame's, else the factor conversion in `inferDataTypes` crashes
# make them data.frame's, else the factor conversion
# in `inferDataTypes` crashes
#rawdata <- as.data.frame(rawdata)
#header <- as.data.frame(header)
# Add names
Expand All @@ -77,8 +78,8 @@ readSurvey <- function(file_name,
# ----------------------------------------------------
if(stripHTML) {
# weird regex to strip HTML tags, leaving only content
# https://www.r-bloggers.com/htmltotext-extracting-text-from-html-via-xpath/
pattern <- "</?\\w+((\\s+\\w+(\\s*=\\s*(?:\".*?\"|'.*?'|[^'\">\\s]+))?)+\\s*|\\s*)/?>"
# https://www.r-bloggers.com/htmltotext-extracting-text-from-html-via-xpath/ # nolint
pattern <- "</?\\w+((\\s+\\w+(\\s*=\\s*(?:\".*?\"|'.*?'|[^'\">\\s]+))?)+\\s*|\\s*)/?>" # nolint
secondrow <- gsub(pattern, "\\4", secondrow)
}
# Scale Question with subquestion:
Expand Down
57 changes: 33 additions & 24 deletions R/registerOptions.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,20 @@ registerOptions <- function(verbose=TRUE,
# Store root_url/api_token. Else give NA as value
root_url <- ifelse("root_url" %in% names(args), args$root_url, NA)
api_token <- ifelse("api_token" %in% names(args), args$api_token, NA)
# If environment variables are already set and user just wants to change verbose argument
if(Sys.getenv("QUALTRICS_ROOT_URL") != "" & Sys.getenv("QUALTRICS_API_KEY") != "") {
# If environment variables are already set and user just wants to
# change verbose argument
if(Sys.getenv("QUALTRICS_ROOT_URL") != "" & Sys.getenv("QUALTRICS_API_KEY") != "") { # nolint
# Check
assertthat::assert_that(assertthat::is.flag(verbose), msg=paste0("'verbose' must be either TRUE or FALSE."))
assertthat::assert_that(assertthat::is.flag(convertVariables), msg=paste0("'convertvariables' must be either TRUE or FALSE."))
assertthat::assert_that(assertthat::is.flag(useLabels), msg=paste0("'uselabels' must be either TRUE or FALSE."))
assertthat::assert_that(assertthat::is.flag(useLabels), msg=paste0("'uselabels' must be either TRUE or FALSE."))
assertthat::assert_that(assertthat::is.flag(dateWarning), msg=paste0("'dateWarning' must be either TRUE or FALSE."))
assertthat::assert_that(assertthat::is.flag(verbose),
msg=paste0("'verbose' must be either TRUE or FALSE.")) # nolint
assertthat::assert_that(assertthat::is.flag(convertVariables),
msg=paste0("'convertvariables' must be either TRUE or FALSE.")) # nolint
assertthat::assert_that(assertthat::is.flag(useLabels),
msg=paste0("'uselabels' must be either TRUE or FALSE.")) # nolint
assertthat::assert_that(assertthat::is.flag(useLabels),
msg=paste0("'uselabels' must be either TRUE or FALSE.")) # nolint
assertthat::assert_that(assertthat::is.flag(dateWarning),
msg=paste0("'dateWarning' must be either TRUE or FALSE.")) # nolint
# If user just wants to pass options, do:
options(
"QUALTRICS_VERBOSE" = verbose,
Expand All @@ -85,65 +91,67 @@ registerOptions <- function(verbose=TRUE,
"QUALTRICS_USELOCALTIME" = useLocalTime
)
# Set warning
invisible(ifelse(dateWarning == FALSE, Sys.setenv("QUALTRICS_WARNING_DATE_GIVEN"=TRUE),
invisible(ifelse(dateWarning == FALSE,
Sys.setenv("QUALTRICS_WARNING_DATE_GIVEN"=TRUE),
TRUE))
# Set root url and api token if not NA
if(!is.na(root_url)) Sys.setenv("QUALTRICS_ROOT_URL" = root_url)
if(!is.na(api_token)) Sys.setenv("QUALTRICS_API_KEY" = api_token)
# Quietly quit
return(invisible(NULL))
}
# If API token and root url are NA, then look for .qualtRics.yml in the current directory
# If API token and root url are NA, then look for .qualtRics.yml
#in the current directory
if(is.na(api_token) & is.na(root_url)) {
ex <- file.exists(".qualtRics.yml")
# Throw error if file does not exist
assertthat::assert_that(ex == TRUE, msg = "No .qualtRics.yml configuration file found in working directory. Either set your api token and root url using the 'registerOptions' function or create a configuration file. Execute 'qualtRicsConfigFile()' to view an example of a configuration file.")
assertthat::assert_that(ex == TRUE, msg = "No .qualtRics.yml configuration file found in working directory. Either set your api token and root url using the 'registerOptions' function or create a configuration file. Execute 'qualtRicsConfigFile()' to view an example of a configuration file.") # nolint
# Load file
cred <- yaml::yaml.load_file(".qualtRics.yml")
# Assert that names are "api_token" and "root_url"
assertthat::assert_that(all(c("api_token", "root_url") %in% names(cred)), msg="Either the 'api_token' or 'root_url' arguments are missing in your .qualtRics.yml configuration file. Execute 'qualtRicsConfigFile()' to view an example of the configuration file. Execute 'file.edit('.qualtRics.yml')' to edit your configuration file.")
assertthat::assert_that(all(c("api_token", "root_url") %in% names(cred)), msg="Either the 'api_token' or 'root_url' arguments are missing in your .qualtRics.yml configuration file. Execute 'qualtRicsConfigFile()' to view an example of the configuration file. Execute 'file.edit('.qualtRics.yml')' to edit your configuration file.") # nolint
# If verbose, print message
if(verbose) message(paste0("Found a .qualtRics.yml configuration file in ", getwd(), ". Using these credentials."))
if(verbose) message(paste0("Found a .qualtRics.yml configuration file in ", getwd(), ". Using these credentials.")) # nolint
# Set vars
api_token <- cred$api_token
root_url <- cred$root_url
# Set optional vars
if("verbose" %in% names(cred)) {
verbose <- cred$verbose
assertthat::assert_that(assertthat::is.flag(verbose), msg=paste0("'verbose' must be either TRUE or FALSE but is ", as.character(verbose), " in your config file."))
assertthat::assert_that(assertthat::is.flag(verbose), msg=paste0("'verbose' must be either TRUE or FALSE but is ", as.character(verbose), " in your config file.")) # nolint
}
# If 'convertStandardColumns' is found in credentials then emit a warning
if('convertstandardcolumns' %in% names(cred) & !'convertvariables' %in% names(cred)) {
message("'convertstandardcolumns' has been deprecated and will be ignored. Please replace it by 'convertvariables' in your '.qualtRics.yml' file. Visit <URL TO INFORMATION> for more information.")
if('convertstandardcolumns' %in% names(cred) & !'convertvariables' %in% names(cred)) { # nolint
message("'convertstandardcolumns' has been deprecated and will be ignored. Please replace it by 'convertvariables' in your '.qualtRics.yml' file. Visit <URL TO INFORMATION> for more information.") # nolint
convertVariables <- TRUE
} else if(all(c('convertstandardcolumns', 'convertvariables') %in% names(cred))) {
message("'convertstandardcolumns' has been deprecated and will be ignored. Please remove it from your '.qualtRics.yml' file. Visit <URL TO INFORMATION> for more information.")
} else if(all(c('convertstandardcolumns', 'convertvariables') %in% names(cred))) { # nolint
message("'convertstandardcolumns' has been deprecated and will be ignored. Please remove it from your '.qualtRics.yml' file. Visit <URL TO INFORMATION> for more information.") # nolint
convertVariables <- cred$convertvariables
} else {
convertVariables <- cred$convertvariables
}
# Check
assertthat::assert_that(assertthat::is.flag(convertVariables),
msg=paste0("'convertvariables' must be either TRUE or FALSE but is ",
msg=paste0("'convertvariables' must be either TRUE or FALSE but is ", # nolint
as.character(convertVariables),
" in your config file."))
if("uselabels" %in% names(cred)) {
useLabels <- cred$uselabels
assertthat::assert_that(assertthat::is.flag(useLabels), msg=paste0("'uselabels' must be either TRUE or FALSE but is ", as.character(useLabels), " in your config file."))
assertthat::assert_that(assertthat::is.flag(useLabels), msg=paste0("'uselabels' must be either TRUE or FALSE but is ", as.character(useLabels), " in your config file.")) # nolint
}
if("uselocaltime" %in% names(cred)) {
useLocalTime <- cred$uselocaltime
assertthat::assert_that(assertthat::is.flag(useLocalTime), msg=paste0("'useLocalTime' must be either TRUE or FALSE but is ", as.character(useLocalTime), " in your config file."))
assertthat::assert_that(assertthat::is.flag(useLocalTime), msg=paste0("'useLocalTime' must be either TRUE or FALSE but is ", as.character(useLocalTime), " in your config file.")) # nolint
}
if("datewarning" %in% names(cred)) {
dateWarning <- cred$datewarning
assertthat::assert_that(assertthat::is.flag(dateWarning), msg=paste0("'dateWarning' must be either TRUE or FALSE but is ", as.character(dateWarning), " in your config file."))
assertthat::assert_that(assertthat::is.flag(dateWarning), msg=paste0("'dateWarning' must be either TRUE or FALSE but is ", as.character(dateWarning), " in your config file.")) # nolint
}
}
# Check arguments
# If either is still NA AND environment is empty, throw error
assertthat::assert_that(!is.na(root_url) | Sys.getenv("QUALTRICS_ROOT_URL") != "", msg="'root_url' parameter must either be specified in the .qualtRics.yml configuration file or passed to the 'registerOptions' function. To view an example of a configuration file, execute 'qualtRicsConfigFile()'.")
assertthat::assert_that(!is.na(api_token) | Sys.getenv("QUALTRICS_API_KEY") != "", msg="'api_token' parameter must either be specified in the .qualtRics.yml configuration file or passed to the 'registerOptions' function. To view an example of a configuration file, execute 'qualtRicsConfigFile()'.")
assertthat::assert_that(!is.na(root_url) | Sys.getenv("QUALTRICS_ROOT_URL") != "", msg="'root_url' parameter must either be specified in the .qualtRics.yml configuration file or passed to the 'registerOptions' function. To view an example of a configuration file, execute 'qualtRicsConfigFile()'.") # nolint
assertthat::assert_that(!is.na(api_token) | Sys.getenv("QUALTRICS_API_KEY") != "", msg="'api_token' parameter must either be specified in the .qualtRics.yml configuration file or passed to the 'registerOptions' function. To view an example of a configuration file, execute 'qualtRicsConfigFile()'.") # nolint
# Set environment variables
if(!is.na(api_token)) Sys.setenv("QUALTRICS_API_KEY" = api_token)
if(!is.na(root_url)) Sys.setenv("QUALTRICS_ROOT_URL" = root_url)
Expand All @@ -155,6 +163,7 @@ registerOptions <- function(verbose=TRUE,
"QUALTRICS_USELOCALTIME" = useLocalTime
)
# Set warning
invisible(ifelse(dateWarning == FALSE, Sys.setenv("QUALTRICS_WARNING_DATE_GIVEN"=TRUE),
invisible(ifelse(dateWarning == FALSE,
Sys.setenv("QUALTRICS_WARNING_DATE_GIVEN"=TRUE),
TRUE))
}

0 comments on commit 2e6d84b

Please sign in to comment.