Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function for changing the Rstudio viewer settings across an R session #28

Open
barrettk opened this issue Jul 24, 2023 · 0 comments
Open

Comments

@barrettk
Copy link

The following function can change the default viewer settings between using the A) the Rstudio viewer, and B) opening a separate window. All HTML plots, Rmarkdown reports, shiny apps, etc. will be affected for the remainder of the R session:

Function

#' Set viewer option
#'
#' @param .view Method for viewing the R object
#'
#' @keywords internal
set_window_viewer <- function(.view = c("window", "viewer")) {
  .view <- match.arg(.view)

  view_call <- switch(.view,
                      viewer = rlang::quo(function(url, height) {
                        invisible(.Call("rs_viewer", url, height, PACKAGE = "(embedding)"))
                      }),
                      window = rlang::quo(function(url, height) {
                        invisible(.Call("rs_showPageViewer", url, title = "RStudio", self_contained = FALSE))
                      })
  )

  view_fn <- function(url, height = NULL) {
    if (!is.character(url) || length(url) != 1)
      stop("url must be a single element character vector.", call. = FALSE)

    if (identical(height, "maximize"))
      height <- -1

    if (!is.null(height) && (!is.numeric(height) || length(height) != 1))
      stop("height must be a single element numeric vector or 'maximize'.", call. = FALSE)

    # Evaluate the quoted expression in the environment of view_fn
    eval_tidy(view_call)(url, height)
  }

  options(viewer = view_fn)
}

Use

> set_window_viewer("window")
> result # opens as a new window
> set_window_viewer("viewer")
> result # opens in viewer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant