rstudioapi

repository·main·Indexed 41 days ago

https://github.com/rstudio/rstudioapi

An R package providing conditional access to the RStudio API from CRAN packages. It includes wrapper functions like isAvailable(), verifyAvailable(), callFun(), and findFun() to allow developers to interact with RStudio features without causing issues during R CMD check when RStudio is not present.

Tokens
714
Snippets
4
Records
4
Agent score
2%

What's inside rstudioapi

  1. Install rstudioapi

    main

    You can install the released version of rstudioapi from CRAN or the development version from GitHub.

    To install the CRAN version:

    install.packages("rstudioapi")

    To install the development version using devtools:

    # install.packages("devtools")
    devtools::install_github("rstudio/rstudioapi")
    install.packages("rstudioapi")
  2. Find RStudio functions by name

    main

    Use rstudioapi::findFun() to retrieve an RStudio function. This method will throw an error if the function does not exist. You can also specify a version requirement to ensure the function is available in the target RStudio version.

    # Throws an error if the function does not exist
    rstudioapi::findFun("viewer")
    
    # Check version requirement with findFun
    rstudioapi::findFun("viewer", 0.99)
    
    # Check version requirement with hasFun
    rstudioapi::hasFun("viewer", 0.99)
  3. Check if RStudio is available

    main

    Use rstudioapi::isAvailable() to check if the RStudio API is currently accessible. This returns a logical value (TRUE or FALSE). You can optionally pass a version requirement string to check for a minimum version.

    To ensure RStudio is available and halt execution if it is not, use rstudioapi::verifyAvailable().

    # Returns T/F
    rstudioapi::isAvailable()
    
    # Returns error if not available
    rstudioapi::verifyAvailable()
    
    # Optional argument allows you to specify version requirement
    rstudioapi::isAvailable("0.99")
    rstudioapi::verifyAvailable("0.99")
  4. Call RStudio functions safely

    main

    The rstudioapi package is designed to never be attached to your search path. Always prefix function calls with rstudioapi::.

    To execute an RStudio function by name, use rstudioapi::callFun(). Note that callFun will raise an error if RStudio is not running or if the function is not found. To avoid errors, check for the function's existence first using rstudioapi::hasFun().

    # Call an rstudio function
    rstudioapi::callFun("viewer", "http://localhost:8080")
    
    # Safe pattern: check if function exists before calling
    if (rstudioapi::hasFun("viewer")) {
      rstudioapi::callFun("viewer", "http://localhost:8080")
    } else {
      browseURL("http://localhost:8080")
    }