Debian Codegraph @

man/reactivePoll.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/reactives.R
3 \name{reactivePoll}
4 \alias{reactivePoll}
5 \title{Reactive polling}
6 \usage{
7 reactivePoll(intervalMillis, session, checkFunc, valueFunc)
8 }
9 \arguments{
10 \item{intervalMillis}{Approximate number of milliseconds to wait between
11 calls to \code{checkFunc}. This can be either a numeric value, or a
12 function that returns a numeric value.}
13
14 \item{session}{The user session to associate this file reader with, or
15 \code{NULL} if none. If non-null, the reader will automatically stop when
16 the session ends.}
17
18 \item{checkFunc}{A relatively cheap function whose values over time will be
19 tested for equality; inequality indicates that the underlying value has
20 changed and needs to be invalidated and re-read using \code{valueFunc}. See
21 Details.}
22
23 \item{valueFunc}{A function that calculates the underlying value. See
24 Details.}
25 }
26 \value{
27 A reactive expression that returns the result of \code{valueFunc},
28 and invalidates when \code{checkFunc} changes.
29 }
30 \description{
31 Used to create a reactive data source, which works by periodically polling a
32 non-reactive data source.
33 }
34 \details{
35 \code{reactivePoll} works by pairing a relatively cheap "check" function with
36 a more expensive value retrieval function. The check function will be
37 executed periodically and should always return a consistent value until the
38 data changes. When the check function returns a different value, then the
39 value retrieval function will be used to re-populate the data.
40
41 Note that the check function doesn't return \code{TRUE} or \code{FALSE} to
42 indicate whether the underlying data has changed. Rather, the check function
43 indicates change by returning a different value from the previous time it was
44 called.
45
46 For example, \code{reactivePoll} is used to implement
47 \code{reactiveFileReader} by pairing a check function that simply returns the
48 last modified timestamp of a file, and a value retrieval function that
49 actually reads the contents of the file.
50
51 As another example, one might read a relational database table reactively by
52 using a check function that does \verb{SELECT MAX(timestamp) FROM table} and
53 a value retrieval function that does \verb{SELECT * FROM table}.
54
55 The \code{intervalMillis}, \code{checkFunc}, and \code{valueFunc} functions
56 will be executed in a reactive context; therefore, they may read reactive
57 values and reactive expressions.
58 }
59 \examples{
60 function(input, output, session) {
61
62 data <- reactivePoll(1000, session,
63 # This function returns the time that log_file was last modified
64 checkFunc = function() {
65 if (file.exists(log_file))
66 file.info(log_file)$mtime[1]
67 else
68 ""
69 },
70 # This function returns the content of log_file
71 valueFunc = function() {
72 read.csv(log_file)
73 }
74 )
75
76 output$dataTable <- renderTable({
77 data()
78 })
79 }
80 }
81 \seealso{
82 \code{\link[=reactiveFileReader]{reactiveFileReader()}}
83 }