README.md

shinybrowser

🌐 Find out information about a user's web browser in Shiny apps Demo · by Dean Attali

R build status CRAN version

{shinybrowser} lets you easily detect a user's web browser information in Shiny apps.

The available information is: browser name (such as "Chrome" or "Safari") and version, device type (mobile or desktop), operating system (such as "Windows" or "Mac" or "Android") and version, and browser dimensions. See the demo Shiny app online for an example.

Need Shiny help? I'm available for consulting. If you find {shinybrowser} useful, please consider supporting my work! ❀

This package is part of a larger ecosystem of packages with a shared vision: solving common Shiny issues and improving Shiny apps with minimal effort, minimal code changes, and clear documentation. Other packages for your Shiny apps:

| Package | Description | Demo | |---|---|---| | shinyjs | πŸ’‘ Easily improve the user experience of your Shiny apps in seconds | πŸ”— | | shinyalert | πŸ—―οΈ Easily create pretty popup messages (modals) in Shiny | πŸ”— | | shinyscreenshot | πŸ“· Capture screenshots of entire pages or parts of pages in Shiny apps | πŸ”— | | timevis | πŸ“… Create interactive timeline visualizations in R | πŸ”— | | shinycssloaders | βŒ› Add loading animations to a Shiny output while it's recalculating | πŸ”— | | colourpicker | 🎨 A colour picker tool for Shiny and for selecting colours in plots | πŸ”— | | shinydisconnect | πŸ”Œ Show a nice message when a Shiny app disconnects or errors | πŸ”— | | shinytip | πŸ’¬ Simple flexible tooltips for Shiny apps | WIP | | shinymixpanel | πŸ” Track user interactions with Mixpanel in Shiny apps or R scripts | WIP | | shinyforms | πŸ“ Easily create questionnaire-type forms with Shiny | WIP |

Table of contents

The Problem

When building a Shiny app for other users, sometimes you might want to know some information about the user's browser and system. A few scenarios where this can be useful:

  1. Your Shiny app is using an advanced browser functionality that isn't supported in Internet Explorer. If a user views your app on Internet Explorer, you want to show them a warning message.
  2. Your app displays some text to the user and you want to show them a message with the keyboard shortcut for copying the text to their clipboard. You need to know whether the user is on Windows or Mac to decide what keyboard shortcut to use (ctrl+C vs command+C).
  3. Your app contains a plot with a legend. If the user is on a wide enough screen, you want the legend to appear beside the plot, but if the screen is too narrow then you want the legend to appear below the plot.

In these situations, you want to detect information about the user's browser type (scenario 1), operating system (scenario 2), or browser dimensions (scenario 3).

{shinybrowser} gives you easy access to this information.

Sponsors πŸ†

Developing and maintaining many packages takes a lot of time. Please consider sponsoring this work!

There are no sponsors yet

Become the first sponsor for {shinybrowser}!

How to use

First you need to call detect() anywhere in the UI. This will initialize {shinybrowser}, run the script that attempts to detect all the user information, and send it to Shiny.

Then you can use any of the {shinybrowser} functions to inquire about the user. You can use get_browser()/get_browser_version() to get the browser name and version, get_os()/get_os_version() to get the operating system, get_device() to find out if the user is on mobile or desktop, and get_width()/get_height() to find out the browser dimensions.

All these values are reactive, so they must be accessed inside an observe()/reactive() or similar.

Example

To see what {shinybrowser} returns for your system, check the demo Shiny app.

A simple example of using {shinybrowser} is below:

library(shiny)

ui <- fluidPage(
  shinybrowser::detect()
)

server <- function(input, output, session) {
  observe({
    str(shinybrowser::get_all_info())
  })
}

shinyApp(ui, server)

This app will print all the information retrieved by {shinybrowser} to your console. Notice that it's calling get_all_info(), which is a way to get all the information in one list instead of using the individual get_* functions.

On my current machine, the output from the app is:

List of 5
 $ device    : chr "Desktop"
 $ browser   :List of 2
  ..$ name   : chr "Chrome"
  ..$ version: chr "87"
 $ os        :List of 2
  ..$ name   : chr "Windows"
  ..$ version: chr "10"
 $ dimensions:List of 2
  ..$ width : int 1920
  ..$ height: int 937
 $ user_agent: chr "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"

Note that {shinybrowser} also returns the "user_agent" string, which you can access using get_user_agent(), but you generally shouldn't need to use this string.

You can also use {shinybrowser} in Rmarkdown documents, as long as they use runtime: shiny.

Convenience checker functions

{shinybrowser} has a few convenience functions for very common checks. For example, there are many browsers, but often Internet Explorer is the problematic one. If you want to check for it you can use is_browser_ie(), which is just a shorthand for get_browser() == "Internet Explorer". There are a few other similar is_* functions that can be used as a shortcut.

Installation

For most users: To install the stable CRAN version:

install.packages("shinybrowser")

For advanced users: To install the latest development version from GitHub:

install.packages("remotes")
remotes::install_github("daattali/shinybrowser")

Limitations

Accuracy

It's important to understand there is no reliable way to detect the information in {shinybrowser} with 100% accuracy.

{shinybrowser} makes a best effort at identifying the most accurate information, but some browser/operating system combinations may be difficult to identify. Users can also use a variety of tools to deliberately spoof this information.

With that in mind, {shinybrowser} should detect the correct information in most cases.

Supported values

Only major browsers and operating systems are supported, which means that the RStudio Viewer may result in an "UNKNOWN" browser, and unpopular operating systems may also result in "UNKNOWN". An operating system version is only detectable for Windows and for Mac OS X; all other operating systems will have an "UNKNOWN" version.

Mobile vs desktop vs tablet

For device type, {shinybrowser} attempts to detect whether a device is "mobile" or "desktop". The distinction between mobile and desktop is not always clear, so if what you actually care about is the size of the device, it might be better to use get_width().

Tablets return ambiguous results; some tablets self-report as mobile devices while others as desktop.

Width and height

The width and height of the browser window are only reported once, when the detect() function is initially called. If the user resizes the browser window, the new dimensions are not reported until the page is refreshed.

Credits

Logo design by Alfredo HernΓ‘ndez.



daattali/shinybrowser documentation built on Aug. 21, 2024, 6:12 a.m.