@@ -307,271 +307,3 @@ ALLSMALLER <- as.symbol("ALLSMALLER")
307
307
# ' @rdname selectors
308
308
# ' @export
309
309
MATCH <- as.symbol(" MATCH" )
310
-
311
-
312
- # Dash 2 Syntax Functions
313
-
314
- # ' Create a Dash application
315
- # '
316
- # ' This is a convenience function that returns a [`dash::Dash`] R6 object.
317
- # ' For advanced usage, you can use the object as an R6 object directly instead
318
- # ' of the functions provided by the `{dash2}` package.
319
- # '
320
- # ' @param title _(character)_ The browser window title.
321
- # ' @param update_title _(character)_ The browser window title while a callback
322
- # ' is being processed. Set to `NULL` or `”"` if you don't want Dash to
323
- # ' automatically update the window title.
324
- # ' @param assets_folder _(character)_ Path (relative to the current working
325
- # ' directory) containing extra files to be served by the browser. All files
326
- # ' with ".js" or ".css" extensions will automatically be included on the page,
327
- # ' unless excluded with `assets_ignore`. Any other files, such as images, will
328
- # ' only be served if explicitly requested.
329
- # ' @param assets_url_path _(character)_ URL path for serving assets. For
330
- # ' example, a value of "www" means that any request path that begins with
331
- # ' "/www" will be mapped to the `assets_folder`. If your assets are hosted
332
- # ' online, you can provide a CDN URL, such as "http://your-assets-website".
333
- # ' @param assets_ignore _(character)_ Regular expression for ".js" and ".css"
334
- # ' files that should not be automatically included. Ignored files will still
335
- # ' be served if explicitly requested. Note that you cannot use this to
336
- # ' prevent access to sensitive files since ignored files are accessible
337
- # ' by users.
338
- # ' @param eager_loading _(logical)_ Whether asynchronous resources are
339
- # ' prefetched (`TRUE`) or loaded on-demand (`FALSE`).
340
- # ' @param serve_locally _(logical)_ Whether to serve HTML dependencies locally
341
- # ' or remotely (via URL).
342
- # ' @param pathname_url_base _(character)_ Local URL prefix to use app-wide.
343
- # ' @param pathname_routes_prefix _(character)_ Prefix applied to the backend
344
- # ' routes. Defaults to `pathname_url_base`.
345
- # ' @param pathname_requests_prefix _(character)_ Prefix applied to request
346
- # ' endpoints made by Dash's front-end. Defaults to `pathname_url_base`.
347
- # ' @param compress _(logical)_ Whether to try to compress files and data. If
348
- # ' `TRUE`, then `brotli` compression is attempted first, then `gzip`, then the
349
- # ' `deflate` algorithm, before falling back to identity.
350
- # ' @param suppress_callback_exceptions _(logical)_ Whether to relay warnings
351
- # ' about possible layout mis-specifications when registering a callback.
352
- # ' @param show_undo_redo _(logical)_ If `TRUE`, the app will have undo and redo
353
- # ' buttons for stepping through the history of the app state.
354
- # ' @seealso [`run_app()`]
355
- # ' @export
356
- dash_app <- function (title = NULL ,
357
- update_title = " Updating..." ,
358
- assets_folder = " assets" ,
359
- assets_url_path = " /assets" ,
360
- assets_ignore = NULL ,
361
- eager_loading = FALSE ,
362
- serve_locally = TRUE ,
363
- pathname_url_base = " /" ,
364
- pathname_routes_prefix = NULL ,
365
- pathname_requests_prefix = NULL ,
366
- compress = TRUE ,
367
- suppress_callback_exceptions = FALSE ,
368
- show_undo_redo = FALSE ) {
369
-
370
- if (is.null(assets_ignore )) {
371
- assets_ignore <- " "
372
- }
373
-
374
- app <- dash :: Dash $ new(
375
- assets_folder = assets_folder ,
376
- assets_url_path = assets_url_path ,
377
- assets_ignore = assets_ignore ,
378
- eager_loading = eager_loading ,
379
- serve_locally = serve_locally ,
380
- url_base_pathname = pathname_url_base ,
381
- routes_pathname_prefix = pathname_routes_prefix ,
382
- requests_pathname_prefix = pathname_requests_prefix ,
383
- compress = compress ,
384
- suppress_callback_exceptions = suppress_callback_exceptions ,
385
- show_undo_redo = show_undo_redo ,
386
- update_title = update_title
387
- )
388
-
389
- if (! is.null(title )) {
390
- app $ title(title )
391
- }
392
-
393
- invisible (app )
394
- }
395
-
396
-
397
- # ' Add `<meta>` tags to a Dash app
398
- # '
399
- # ' @param app A dash application created with [`dash_app()`].
400
- # ' @param meta A single meta tag or a list of meta tags. Each meta tag is a
401
- # ' named list with two elements representing a meta tag. See examples below.
402
- # ' @examples
403
- # ' app <- dash_app()
404
- # '
405
- # ' # Add a single meta tag
406
- # ' app %>% add_meta(list(name = "description", content = "My App"))
407
- # '
408
- # ' # Add multiple meta tags
409
- # ' app %>% add_meta(list(
410
- # ' list(name = "keywords", content = "dash, analysis, graphs"),
411
- # ' list(name = "viewport", content = "width=device-width, initial-scale=1.0")
412
- # ' ))
413
- # ' @export
414
- add_meta <- function (app , meta ) {
415
- assert_dash(app )
416
- if (! is.list(meta [[1 ]])) {
417
- meta <- list (meta )
418
- }
419
- app $ .__enclos_env__ $ private $ meta_tags <- c(app $ .__enclos_env__ $ private $ meta_tags , meta )
420
- invisible (app )
421
- }
422
-
423
-
424
- # ' Add external (CSS) stylesheets to a Dash app
425
- # '
426
- # ' @param app A dash application created with [`dash_app()`].
427
- # ' @param stylesheet A single stylesheet or a list of stylesheets. Each
428
- # ' stylesheet is either a string (the URL), or a named list with `href` (the
429
- # ' URL) and any other valid `<link>` tag attributes. See examples below.
430
- # ' Note that this is only used to add **external** stylesheets, not local.
431
- # ' @examples
432
- # ' app <- dash_app()
433
- # '
434
- # ' # Add a single stylesheet with URL
435
- # ' app %>% add_stylesheet("https://cdn.jsdelivr.net/npm/[email protected] /dist/css/bootstrap.min.css")
436
- # '
437
- # ' # Add multiple stylesheets with URL
438
- # ' app %>% add_stylesheet(list(
439
- # ' "https://cdn.jsdelivr.net/npm/[email protected] /dist/css/bootstrap.min.css",
440
- # ' "https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"
441
- # ' ))
442
- # '
443
- # ' # Add a single stylesheet with a list
444
- # ' app %>% add_stylesheet(
445
- # ' list(
446
- # ' href = "https://cdn.jsdelivr.net/npm/[email protected] /dist/css/bootstrap.min.css",
447
- # ' integrity = "sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x"
448
- # ' )
449
- # ' )
450
- # '
451
- # ' # Add multiple stylesheets with both URL and list
452
- # ' app %>% add_stylesheet(
453
- # ' list(
454
- # ' "https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css",
455
- # ' "https://fonts.googleapis.com/css?family=Lora",
456
- # ' list(
457
- # ' href = "https://cdn.jsdelivr.net/npm/[email protected] /dist/css/bootstrap.min.css",
458
- # ' integrity = "sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x"
459
- # ' )
460
- # ' )
461
- # ' )
462
- # ' @export
463
- add_stylesheet <- function (app , stylesheet ) {
464
- assert_dash(app )
465
- if (! is.list(stylesheet ) || ! is.null(names(stylesheet ))) {
466
- stylesheet <- list (stylesheet )
467
- }
468
- app $ .__enclos_env__ $ self $ config $ external_stylesheets <- c(app $ .__enclos_env__ $ self $ config $ external_stylesheets , stylesheet )
469
- invisible (app )
470
- }
471
-
472
-
473
- # ' Add external (JavaScript) scripts to a Dash app
474
- # '
475
- # ' @param app A dash application created with [`dash_app()`]
476
- # ' @param script A single script or a list of scripts. Each script is either
477
- # ' a string (the URL), or a named list with `src` (the URL) and any other valid
478
- # ' `<script>` tag attributes. See examples below.
479
- # ' Note that this is only used to add **external** scripts, not local.
480
- # ' @examples
481
- # ' app <- dash_app()
482
- # '
483
- # ' # Add a single script with URL
484
- # ' app %>% add_script("https://stackpath.bootstrapcdn.com/bootstrap/4.4.0/js/bootstrap.min.js")
485
- # '
486
- # ' # Add multiple scripts with URL
487
- # ' app %>% add_script(list(
488
- # ' "https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js",
489
- # ' "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"
490
- # ' ))
491
- # '
492
- # ' # Add a single script with a list
493
- # ' app %>% add_script(
494
- # ' list(
495
- # ' href = "https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js",
496
- # ' integrity = "sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6"
497
- # ' )
498
- # ' )
499
- # '
500
- # ' # Add multiple scripts with both URL and list
501
- # ' app %>% add_script(
502
- # ' list(
503
- # ' "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js",
504
- # ' "https://cdn.jsdelivr.net/npm/[email protected] /dist/umd/popper.min.js",
505
- # ' list(
506
- # ' href = "https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js",
507
- # ' integrity = "sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6"
508
- # ' )
509
- # ' )
510
- # ' )
511
- # ' @export
512
- add_script <- function (app , script ) {
513
- assert_dash(app )
514
- if (! is.list(script ) || ! is.null(names(script ))) {
515
- script <- list (script )
516
- }
517
- app $ .__enclos_env__ $ self $ config $ external_scripts <- c(app $ .__enclos_env__ $ self $ config $ external_scripts , script )
518
- invisible (app )
519
- }
520
-
521
- # ' Set the layout of a Dash app
522
- # ' @param app A dash application created with [`dash_app()`]
523
- # ' @param ... Dash components to create the user interface, provided either as
524
- # ' comma-separated components or a list of components. You can also provide a
525
- # ' function returning a Dash component if you want the layout to re-render on
526
- # ' every page load.
527
- # '
528
- # ' @examples
529
- # ' app <- dash_app()
530
- # '
531
- # ' app %>% set_layout("hello", "Dash")
532
- # ' app %>% set_layout(htmlDiv("hello"), "Dash")
533
- # ' app %>% set_layout(list(htmlDiv("hello"), "Dash"))
534
- # ' app %>% set_layout(htmlDiv(children = list(htmlDiv("hello"), "Dash")))
535
- # ' app %>% set_layout(function() { htmlDiv(children = list(htmlDiv("hello"), "Dash")) })
536
- # ' @export
537
- set_layout <- function (app , ... ) {
538
- assert_dash(app )
539
-
540
- tags <- list (... )
541
- if (length(tags ) > 0 && ! is.null(names(tags ))) {
542
- stop(" dash2: layout cannot have any named parameters" )
543
- }
544
- if (length(tags ) == 1 ) {
545
- if (is.function(tags [[1 ]])) {
546
- layout <- tags [[1 ]]
547
- } else {
548
- layout <- componentify(tags [[1 ]])
549
- }
550
- } else {
551
- layout <- componentify(tags )
552
- }
553
-
554
- app $ layout(layout )
555
- invisible (app )
556
- }
557
-
558
- # ' Run a Dash app
559
- # ' @param app A dash application created with [`dash_app()`]
560
- # ' @param host Hostname to run the app.
561
- # ' @param port Port number to run the app.
562
- # ' @param browser Whether or not to launch a browser to the app's URL.
563
- # ' @export
564
- run_app <- function (app ,
565
- host = Sys.getenv(" DASH_HOST" , Sys.getenv(" HOST" , " 127.0.0.1" )),
566
- port = Sys.getenv(" DASH_PORT" , Sys.getenv(" PORT" , 8050 )),
567
- browser = interactive()) {
568
- assert_dash(app )
569
- if (browser ) {
570
- url <- paste0(host , " :" , port )
571
- if (! grepl(" ^(http|https)://" , host )) {
572
- url <- paste0(" http://" , url )
573
- }
574
- utils :: browseURL(url )
575
- }
576
- app $ run_server(host = host , port = port )
577
- }
0 commit comments