Skip to content

Commit

Permalink
recode: fix some codefactor warnings on return() (#55)
Browse files Browse the repository at this point in the history
* recode: fix some codefactor warnings on return()
* docs: start 5.2.8.9000
* recode: clean code
  • Loading branch information
laresbernardo authored Jun 26, 2024
1 parent 75ed55a commit f81dbb9
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 49 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: lares
Type: Package
Title: Analytics & Machine Learning Sidekick
Version: 5.2.8
Version: 5.2.8.9000
Authors@R: c(
person("Bernardo", "Lares", , "[email protected]", c("aut", "cre")))
Maintainer: Bernardo Lares <[email protected]>
Expand Down
2 changes: 1 addition & 1 deletion R/chatgpt.R
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ gpt_markdown2df <- function(resp) {
}
df <- try(markdown2df(resp))
attr(df, "response") <- df
return(df)
df
}

# DEPRECATED: Now using the new gpt_prompter()
Expand Down
7 changes: 1 addition & 6 deletions R/dalex_explainers.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,10 @@ dalex_local <- function(explainer, observation = NA, row = 1, type = "break_down
#' @export
dalex_residuals <- function(explainer) {
try_require("DALEX")

resids <- model_performance(explainer)

p1 <- plot(resids) + theme_lares(legend = "none")
p2 <- plot(resids, geom = "boxplot") + theme_lares(legend = "none")

p <- p1 + p2 + plot_layout(nrow = 2)

return(p)
p1 + p2 + plot_layout(nrow = 2)
}


Expand Down
29 changes: 14 additions & 15 deletions R/maze.R
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ maze_gridsearch <- function(

# Function to calculate the number of direction changes
count_direction_changes <- function(path_coords) {
num_changes <- 0
if (!is.null(path_coords)) {
# Calculate the direction for each step
directions <- atan2(diff(path_coords$row), diff(path_coords$col)) * (180 / pi)
Expand All @@ -249,37 +250,35 @@ count_direction_changes <- function(path_coords) {
direction_diff <- abs(diff(directions))
# Count the number of direction changes greater than a threshold (e.g., 45 degrees)
num_changes <- sum(direction_diff > 30)
return(num_changes)
} else {
return(0)
}
num_changes
}

# Function to calculate the direction of point in path
calculate_direction <- function(row, col) {
if (is.na(row) || is.na(col) || length(row) == 0 || length(col) == 0) {
return("\u003F") # ?
"\u003F" # ?
}
if (row == 0 && col == 1) {
return("\u2192") # Right
"\u2192" # Right
} else if (row == 0 && col == -1) {
return("\u2190") # Left
"\u2190" # Left
} else if (row == 1 && col == 0) {
return("\u2193") # Down
"\u2193" # Down
} else if (row == -1 && col == 0) {
return("\u2191") # Up
"\u2191" # Up
} else if (row == 1 && col == 1) {
return("\u2198") # Diagonal down-right
"\u2198" # Diagonal down-right
} else if (row == -1 && col == 1) {
return("\u2197") # Diagonal up-right
"\u2197" # Diagonal up-right
} else if (row == 1 && col == -1) {
return("\u2199") # Diagonal down-left
"\u2199" # Diagonal down-left
} else if (row == -1 && col == -1) {
return("\u2196") # Diagonal up-left
"\u2196" # Diagonal up-left
} else if (row == 0 && col == 0) {
return("\u2022") # Center (no movement)
"\u2022" # Center (no movement)
} else {
return("Invalid direction")
"Invalid direction"
}
}

Expand Down Expand Up @@ -309,7 +308,7 @@ linear_extrapolation <- function(x1, y1, x2, y2) {
next_x <- x2 + 1 # Assuming you want to extrapolate to the next integer x value
# Calculate the corresponding y value
next_y <- y2 + slope * (next_x - x2)
return(c(round(next_x), round((next_y))))
c(round(next_x), round((next_y)))
}

# micromouse <- matrix(c(
Expand Down
6 changes: 2 additions & 4 deletions R/model_predict.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,15 @@ h2o_predict_API <- function(df, api, exclude = "tag") {
body = as.list(df),
encode = "json"
)
return(content(x)$probabilityToOne)
content(x)$probabilityToOne
}

batch <- NULL
for (i in seq_len(nrow(df))) {
x <- df[i, ]
score <- post(x, api)
batch <- rbind(batch, score)
}

return(as.vector(batch))
as.vector(batch)
}


Expand Down
26 changes: 12 additions & 14 deletions R/other_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ listfiles <- function(folder = getwd(),
#' @export
myip <- function() {
ipify <- "https://api.ipify.org/"
ip <- content(GET(ipify), encoding = "UTF-8")
return(ip)
content(GET(ipify), encoding = "UTF-8")
}


Expand All @@ -255,8 +254,7 @@ json2vector <- function(json) {
string <- gsub("True", "true", string)
string <- gsub("False", "false", string)
vector <- fromJSON(string)
df <- data.frame(t(unlist(vector)))
return(df)
data.frame(t(unlist(vector)))
}


Expand Down Expand Up @@ -286,7 +284,7 @@ importxlsx <- function(file) {
if (length(sheets) == 1) {
mylist <- mylist[[1]][[1]]
}
return(mylist)
mylist
}


Expand Down Expand Up @@ -348,7 +346,6 @@ haveInternet <- function(thresh = 3, url = "http://www.google.com") {
#' @export
read.file <- function(filename, current_wd = TRUE, sheet = 1, quiet = FALSE) {
if (current_wd) filename <- paste0(getwd(), "/", filename)

if (!file.exists(filename)) {
stop("That file doesn't exist.. try with another!")
} else {
Expand All @@ -358,7 +355,6 @@ read.file <- function(filename, current_wd = TRUE, sheet = 1, quiet = FALSE) {
results <- data.frame(read.csv(filename))
}
if (filetype == "xlsx") {
try_require("openxlsx")
results <- read.xlsx(filename, sheet)
}
if (filetype == "xls") {
Expand Down Expand Up @@ -411,7 +407,7 @@ bind_files <- function(files) {
alldat <- bind_rows(alldat, dfi)
statusbar(i, length(files))
}
return(data.frame(alldat))
as_tibble(alldat)
}


Expand Down Expand Up @@ -486,16 +482,16 @@ autoline <- function(text, top = "auto", rel = 9) {
#' @export
font_exists <- function(font = "Arial Narrow", font_dirs = NULL, quiet = FALSE, ...) {
if (isTRUE(font[1] == "")) {
return(FALSE)
FALSE
}
if (isTRUE(is.na(font[1]))) {
return(FALSE)
FALSE
}
tryCatch(
check_font(font, font_dirs, quiet),
error = function(err) {
if (!quiet) message(paste("Font issue:", err))
return(FALSE)
FALSE
}
)
}
Expand Down Expand Up @@ -560,7 +556,9 @@ check_font <- function(font, font_dirs = NULL, quiet = FALSE) {
}
if (grepl("^mingw", R.version$os)) {
try_require("grDevices")
ret <- font %in% windowsFonts()
win_fonts <- unlist(windowsFonts())
ret <- font %in% win_fonts
# font_names <- c(font_names, win_fonts)
} else {
ret <- font %in% nice_names
}
Expand Down Expand Up @@ -1092,7 +1090,7 @@ chr2num <- function(data) {
data <- as.numeric(data)
}
}
return(data)
data
}
#' @rdname chr2num
#' @export
Expand All @@ -1109,7 +1107,7 @@ chr2logical <- function(data) {
data <- as.logical(toupper(data))
}
}
return(data)
data
}
#' @rdname chr2num
#' @export
Expand Down
8 changes: 4 additions & 4 deletions R/robyn.R
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ robyn_modelselector <- function(
try_require("Robyn")
defpot <- lapply(sols, function(x) {
if (!quiet) statusbar(which(sols == x), length(sols), x)
return(suppressMessages(robyn_allocator(
suppressMessages(robyn_allocator(
InputCollect = InputCollect,
OutputCollect = OutputCollect,
select_model = x,
Expand All @@ -236,7 +236,7 @@ robyn_modelselector <- function(
export = FALSE,
quiet = TRUE,
...
)))
))
})
potOpt <- data.frame(
solID = sols,
Expand Down Expand Up @@ -305,7 +305,7 @@ robyn_modelselector <- function(
left_join(temp, "solID") %>%
ungroup() %>%
left_join(baselines, "solID")

# The following criteria are inverted because the smaller, the better
inv <- c("baseline_dist", "nrmse", "decomp.rssd", "mape")

Expand Down Expand Up @@ -420,7 +420,7 @@ robyn_modelselector <- function(
#' @rdname robyn_modelselector
#' @export
plot.robyn_modelselector <- function(x, ...) {
return(x$plot)
x$plot
}

####################################################################
Expand Down
6 changes: 3 additions & 3 deletions R/stocks.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ stocks_quote <- function(symbols, ...) {
origin = "1970-01-01 00:00:00"
))
row.names(ret) <- NULL
return(ret)
}
ret
}

####################################################################
Expand Down Expand Up @@ -1288,7 +1288,7 @@ stocks_obj <- function(data = stocks_file(),
plots[["History"]] <- splot_growth(p)
plots[["Relative Growth"]] <- splot_change(p, s, rel = TRUE, n_days = NA, keep_old = FALSE)
plots[["Absolute Growth"]] <- splot_change(p, s, rel = FALSE, n_days = NA, keep_old = FALSE)
return(plots)
plots
})
names(plots_relative) <- sprintf("plots_%s", window)
ret[["plots_relative"]] <- plots_relative
Expand Down Expand Up @@ -1471,7 +1471,7 @@ stocks_report <- function(data = NA,
is.numeric, function(x) 0
) %>% mutate(Date = .data$Date - 1))
}
return(new_df)
new_df
}

# # TESTING
Expand Down
2 changes: 1 addition & 1 deletion R/wrangling.R
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ replacefactor <- function(x, original, change) {
}
levels(x)[levels(x) == original[i]] <- change[i]
}
return(x)
x
}

####################################################################
Expand Down

0 comments on commit f81dbb9

Please sign in to comment.