cjerzak commited on
Commit
ac4d567
·
verified ·
1 Parent(s): 0898316

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +120 -107
app.R CHANGED
@@ -13,6 +13,7 @@
13
  # ----------------------------
14
  # Load required packages
15
  # ----------------------------
 
16
  library(shiny)
17
  library(shinydashboard)
18
  library(DT) # For data tables
@@ -281,15 +282,113 @@ ui <- dashboardPage(
281
  target = "_blank",
282
  style = "color: white; text-decoration: underline; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;"
283
  )
284
- )
285
  ),
286
 
287
  # ========== Sidebar ================
288
  dashboardSidebar(
289
  sidebarMenu(
290
- menuItem("Data & Covariates", tabName = "datatab", icon = icon("database")),
291
- menuItem("Generate Randomizations", tabName = "gennet", icon = icon("random")),
292
- menuItem("Randomization Test", tabName = "randtest", icon = icon("flask"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293
  )
294
  ),
295
 
@@ -331,8 +430,10 @@ ui <- dashboardPage(
331
 
332
  conditionalPanel(
333
  condition = "input.data_source == 'simulate'",
334
- numericInput("sim_n", "Number of units (rows)", value = 100, min = 2),
335
- numericInput("sim_p", "Number of covariates (columns)", value = 50, min = 1),
 
 
336
  actionButton("simulate_btn", "Simulate X")
337
  )
338
  ),
@@ -353,7 +454,8 @@ ui <- dashboardPage(
353
  box(width = 4, title = "Rerandomization Parameters",
354
  status = "primary", solidHeader = TRUE,
355
 
356
- numericInput("n_treated", "Number Treated (n_treated)", value = 10, min = 1),
 
357
  selectInput("random_type", "Randomization Type:",
358
  choices = c("Monte Carlo" = "monte_carlo",
359
  "Exact" = "exact"),
@@ -459,103 +561,7 @@ ui <- dashboardPage(
459
  )
460
  )
461
 
462
- ), # end tabItems
463
-
464
- # ---- Here is the minimal "Share" button HTML + JS inlined in Shiny ----
465
- # We wrap it in tags$div(...) and tags$script(HTML(...)) so it is recognized
466
- # by Shiny. You can adjust the styling or placement as needed.
467
- tags$div(
468
- style = "text-align: left; margin: 1em 0 1em 0em;",
469
- HTML('
470
- <button id="share-button"
471
- style="
472
- display: inline-flex;
473
- align-items: center;
474
- justify-content: center;
475
- gap: 8px;
476
- padding: 5px 10px;
477
- font-size: 16px;
478
- font-weight: normal;
479
- color: #000;
480
- background-color: #fff;
481
- border: 1px solid #ddd;
482
- border-radius: 6px;
483
- cursor: pointer;
484
- box-shadow: 0 1.5px 0 #000;
485
- ">
486
- <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor"
487
- stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
488
- <circle cx="18" cy="5" r="3"></circle>
489
- <circle cx="6" cy="12" r="3"></circle>
490
- <circle cx="18" cy="19" r="3"></circle>
491
- <line x1="8.59" y1="13.51" x2="15.42" y2="17.49"></line>
492
- <line x1="15.41" y1="6.51" x2="8.59" y2="10.49"></line>
493
- </svg>
494
- <strong>Share</strong>
495
- </button>
496
- '),
497
- # Insert the JS as well
498
- tags$script(
499
- HTML("
500
- (function() {
501
- const shareBtn = document.getElementById('share-button');
502
- // Reusable helper function to show a small “Copied!” message
503
- function showCopyNotification() {
504
- const notification = document.createElement('div');
505
- notification.innerText = 'Copied to clipboard';
506
- notification.style.position = 'fixed';
507
- notification.style.bottom = '20px';
508
- notification.style.right = '20px';
509
- notification.style.backgroundColor = 'rgba(0, 0, 0, 0.8)';
510
- notification.style.color = '#fff';
511
- notification.style.padding = '8px 12px';
512
- notification.style.borderRadius = '4px';
513
- notification.style.zIndex = '9999';
514
- document.body.appendChild(notification);
515
- setTimeout(() => { notification.remove(); }, 2000);
516
- }
517
- shareBtn.addEventListener('click', function() {
518
- const currentURL = window.location.href;
519
- const pageTitle = document.title || 'Check this out!';
520
- // If browser supports Web Share API
521
- if (navigator.share) {
522
- navigator.share({
523
- title: pageTitle,
524
- text: '',
525
- url: currentURL
526
- })
527
- .catch((error) => {
528
- console.log('Sharing failed', error);
529
- });
530
- } else {
531
- // Fallback: Copy URL
532
- if (navigator.clipboard && navigator.clipboard.writeText) {
533
- navigator.clipboard.writeText(currentURL).then(() => {
534
- showCopyNotification();
535
- }, (err) => {
536
- console.error('Could not copy text: ', err);
537
- });
538
- } else {
539
- // Double fallback for older browsers
540
- const textArea = document.createElement('textarea');
541
- textArea.value = currentURL;
542
- document.body.appendChild(textArea);
543
- textArea.select();
544
- try {
545
- document.execCommand('copy');
546
- showCopyNotification();
547
- } catch (err) {
548
- alert('Please copy this link:\\n' + currentURL);
549
- }
550
- document.body.removeChild(textArea);
551
- }
552
- }
553
- });
554
- })();
555
- ")
556
- )
557
- ),
558
- # ---- End: Minimal Share button snippet ----
559
 
560
  ) # end dashboardBody
561
  ) # end dashboardPage
@@ -594,8 +600,13 @@ server <- function(input, output, session) {
594
  # Show X in table
595
  output$covariates_table <- renderDT({
596
  req(X_data())
597
- datatable(as.data.frame(signif(X_data())),
598
- options = list(scrollX = TRUE, pageLength = 5))
 
 
 
 
 
599
  })
600
 
601
  # -------------------------------------------------------
@@ -729,7 +740,9 @@ server <- function(input, output, session) {
729
  HTML(paste(
730
  "<strong>System Hardware Info:</strong><br/>",
731
  "Number of CPU cores detected:", num_cores, "<br/>",
732
- "With additional CPU or GPU, greater speedups can be expected."
 
 
733
  ))
734
  })
735
 
 
13
  # ----------------------------
14
  # Load required packages
15
  # ----------------------------
16
+ options(error=NULL)
17
  library(shiny)
18
  library(shinydashboard)
19
  library(DT) # For data tables
 
282
  target = "_blank",
283
  style = "color: white; text-decoration: underline; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;"
284
  )
285
+ )
286
  ),
287
 
288
  # ========== Sidebar ================
289
  dashboardSidebar(
290
  sidebarMenu(
291
+ menuItem("1. Data & Covariates", tabName = "datatab", icon = icon("database")),
292
+ menuItem("2. Generate Randomizations", tabName = "gennet", icon = icon("random")),
293
+ menuItem("3. Randomization Test", tabName = "randtest", icon = icon("flask")),
294
+
295
+
296
+ # ---- Here is the minimal "Share" button HTML + JS inlined in Shiny ----
297
+ # We wrap it in tags$div(...) and tags$script(HTML(...)) so it is recognized
298
+ # by Shiny. You can adjust the styling or placement as needed.
299
+ tags$div(
300
+ style = "text-align: left; margin: 1em 0 1em 1em;",
301
+ HTML('
302
+ <button id="share-button"
303
+ style="
304
+ display: inline-flex;
305
+ align-items: center;
306
+ justify-content: center;
307
+ gap: 8px;
308
+ padding: 5px 10px;
309
+ font-size: 16px;
310
+ font-weight: normal;
311
+ color: #000;
312
+ background-color: #fff;
313
+ border: 1px solid #ddd;
314
+ border-radius: 6px;
315
+ cursor: pointer;
316
+ box-shadow: 0 1.5px 0 #000;
317
+ ">
318
+ <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor"
319
+ stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
320
+ <circle cx="18" cy="5" r="3"></circle>
321
+ <circle cx="6" cy="12" r="3"></circle>
322
+ <circle cx="18" cy="19" r="3"></circle>
323
+ <line x1="8.59" y1="13.51" x2="15.42" y2="17.49"></line>
324
+ <line x1="15.41" y1="6.51" x2="8.59" y2="10.49"></line>
325
+ </svg>
326
+ <strong>Share</strong>
327
+ </button>
328
+ '),
329
+ # Insert the JS as well
330
+ tags$script(
331
+ HTML("
332
+ (function() {
333
+ const shareBtn = document.getElementById('share-button');
334
+ // Reusable helper function to show a small “Copied!” message
335
+ function showCopyNotification() {
336
+ const notification = document.createElement('div');
337
+ notification.innerText = 'Copied to clipboard';
338
+ notification.style.position = 'fixed';
339
+ notification.style.bottom = '20px';
340
+ notification.style.right = '20px';
341
+ notification.style.backgroundColor = 'rgba(0, 0, 0, 0.8)';
342
+ notification.style.color = '#fff';
343
+ notification.style.padding = '8px 12px';
344
+ notification.style.borderRadius = '4px';
345
+ notification.style.zIndex = '9999';
346
+ document.body.appendChild(notification);
347
+ setTimeout(() => { notification.remove(); }, 2000);
348
+ }
349
+ shareBtn.addEventListener('click', function() {
350
+ const currentURL = window.location.href;
351
+ const pageTitle = document.title || 'Check this out!';
352
+ // If browser supports Web Share API
353
+ if (navigator.share) {
354
+ navigator.share({
355
+ title: pageTitle,
356
+ text: '',
357
+ url: currentURL
358
+ })
359
+ .catch((error) => {
360
+ console.log('Sharing failed', error);
361
+ });
362
+ } else {
363
+ // Fallback: Copy URL
364
+ if (navigator.clipboard && navigator.clipboard.writeText) {
365
+ navigator.clipboard.writeText(currentURL).then(() => {
366
+ showCopyNotification();
367
+ }, (err) => {
368
+ console.error('Could not copy text: ', err);
369
+ });
370
+ } else {
371
+ // Double fallback for older browsers
372
+ const textArea = document.createElement('textarea');
373
+ textArea.value = currentURL;
374
+ document.body.appendChild(textArea);
375
+ textArea.select();
376
+ try {
377
+ document.execCommand('copy');
378
+ showCopyNotification();
379
+ } catch (err) {
380
+ alert('Please copy this link:\\n' + currentURL);
381
+ }
382
+ document.body.removeChild(textArea);
383
+ }
384
+ }
385
+ });
386
+ })();
387
+ ")
388
+ )
389
+ )
390
+ # ---- End: Minimal Share button snippet ----
391
+
392
  )
393
  ),
394
 
 
430
 
431
  conditionalPanel(
432
  condition = "input.data_source == 'simulate'",
433
+ numericInput("sim_n", "Number of units (rows)",
434
+ value = 64, min = 10),
435
+ numericInput("sim_p", "Number of covariates (columns)",
436
+ value = 32, min = 2),
437
  actionButton("simulate_btn", "Simulate X")
438
  )
439
  ),
 
454
  box(width = 4, title = "Rerandomization Parameters",
455
  status = "primary", solidHeader = TRUE,
456
 
457
+ numericInput("n_treated", "Number Treated (n_treated)",
458
+ value = 10, min = 1),
459
  selectInput("random_type", "Randomization Type:",
460
  choices = c("Monte Carlo" = "monte_carlo",
461
  "Exact" = "exact"),
 
561
  )
562
  )
563
 
564
+ ) # end tabItems
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
565
 
566
  ) # end dashboardBody
567
  ) # end dashboardPage
 
600
  # Show X in table
601
  output$covariates_table <- renderDT({
602
  req(X_data())
603
+
604
+ # Round all numeric columns to 3 significant digits
605
+ df <- as.data.frame(X_data())
606
+ numeric_cols <- sapply(df, is.numeric)
607
+ df[numeric_cols] <- lapply(df[numeric_cols], signif, digits = 3)
608
+
609
+ datatable(df, options = list(scrollX = TRUE, pageLength = 10))
610
  })
611
 
612
  # -------------------------------------------------------
 
740
  HTML(paste(
741
  "<strong>System Hardware Info:</strong><br/>",
742
  "Number of CPU cores detected:", num_cores, "<br/>",
743
+ "With additional CPU or GPU, greater speedups can be expected.
744
+ Speedups greatest in high-dimensional or large-N settings.
745
+ "
746
  ))
747
  })
748