A drop-in replacement for utils::View(). In an interactive R session
it behaves identically to utils::View(), opening the RStudio /
R Console data viewer. In a non-interactive context such as an
R Markdown or Quarto document, where utils::View() typically errors,
it instead renders an interactive DT::datatable() inline so users
can still inspect the data without their document failing to render.
Arguments
- x
an R object which can be coerced to a data frame.
- title
title for the viewer window. Defaults to the name of
xprefixed byData:, matching the convention used byutils::View().- n
maximum number of rows to show in the inline
DT::datatable(). Whenxhas more thannrows andfull = FALSE, a random sample ofnrows is shown instead of the whole data frame. Default1000.- full
if
TRUE, show every row ofxin the inline table instead of sampling (may be slow, andDTmay warn, for very large data). DefaultFALSE.- seed
optional integer seed making the random sample reproducible. The global random-number stream is left unchanged regardless of whether
seedis supplied. DefaultNULL(a fresh sample each call).- quiet
if
TRUE, suppress the message announcing that a random sample is being shown. DefaultFALSE.
Value
In an interactive session, invisibly returns NULL (called for
the side effect of displaying x in a viewer). In a non-interactive
session, returns a DT::datatable() htmlwidget – except inside webR
(where pandoc is unavailable), where it renders a self-contained static
HTML table through the browser viewer and invisibly returns NULL.
Details
For large data frames a client-side DT::datatable() is slow and warns
that the data is too big. So in non-interactive contexts, when x has more
than n rows and full = FALSE, View() shows a random sample of n
rows and emits a message saying so. This keeps "just look at the data" fast
for beginners while making the size limitation explicit. The interactive
utils::View() path is unaffected (RStudio's viewer handles large data
natively), so n / full / seed / quiet apply only to the inline DT
rendering.
Examples
if (FALSE) { # \dontrun{
# In an interactive R session, behaves like utils::View():
View(un_member_states_2024)
# In an R Markdown or Quarto document, renders an interactive
# DT::datatable() inline instead of erroring:
View(un_member_states_2024)
# For large data a random sample is shown by default; override with:
View(some_big_data, full = TRUE) # all rows (may be slow)
View(some_big_data, n = 500) # sample 500 rows instead of 1000
View(some_big_data, seed = 42) # reproducible random sample
View(some_big_data, quiet = TRUE) # no "random sample" message
} # }