Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Imports:
Depends: R (>= 4.0.0)
License: MIT
Encoding: UTF-8
RoxygenNote: 7.2.1
RoxygenNote: 7.2.3
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
22 changes: 17 additions & 5 deletions R/regression_interactive.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' Interactive visualization function that lets you point-and-click to add data points, while it automatically plots and updates a regression line and associated statistics.
#'
#' @param points An optional \code{dataframe} of *x* and *y* points to plot and estimate the regression. If no \code{points} are provided, the user is free to click and create points on the plot area.
#'
#' @param noRStudioGD Do not use the RStudio graphics device even if specified as the default device (set \code{TRUE} in case of issues with RStudio graphics; will open new OS specific graphics window)
#' @param ... Further arguments passed to the \code{plot_regr()} function that produces the plot.
#'
#' @return A \code{dataframe} containing the points coordinates. Additionally, the following parameters are provided on the plot area:
Expand All @@ -16,7 +16,7 @@
#' \item{R-squared}{The multiple coefficient of determination.}
#'
#' @usage
#' interactive_regression(points, ...)
#' interactive_regression()
#'
#' Click on the plotting area to add points and see a corresponding regression line (hitting ESC will stop the simulation).
#'
Expand All @@ -32,12 +32,19 @@
#' # Providing coordinates beforehand
#' points <- data.frame(x = c(1, 4, 7), y = c(2, 5, 8))
#'
#' # Replotting the coordinates and continuing the interactive regression
#' # Plotting the new coordinates and starting the interactive regression
#' interactive_regression(points)
#'
#' @export
interactive_regression <- function(points=data.frame(), ...) {
cat("Click on the plot to create data points; hit [esc] to stop")
interactive_regression <- function(points=data.frame(), noRStudioGD=FALSE, ...) {
cat("Click on the plot to create data points; hit [esc] to stop\n")
cat("note: if points are not accurately showing, please add noRStudioGD=TRUE\n")

if (noRStudioGD) {
graphics.off()
dev.new(noRStudioGD = noRStudioGD)
}

repeat {
plot_regr(points, ...)

Expand All @@ -51,5 +58,10 @@ interactive_regression <- function(points=data.frame(), ...) {
}
}

if (noRStudioGD) {
graphics.off()
compstatslib::plot_regr(points)
}

return(points)
}
6 changes: 4 additions & 2 deletions man/interactive_regression.Rd

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