larry77

Untitled

Jan 14th, 2025
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. library(shiny)
  2. library(dplyr)
  3. library(pointblank)
  4.  
  5. # Create the sample dataset
  6. data <- tibble(
  7. a = c(100, 150, 200, 105, 70),
  8. b = c(102, 148, 210, 110, 500)
  9. )
  10.  
  11. # Define the custom function in the global environment
  12. equal_within <- function(x, y, percent) {
  13. abs(x - y) / abs(pmin(x, y)) <= percent / 100
  14. }
  15. assign("equal_within", equal_within, envir = .GlobalEnv) # Explicit assignment
  16.  
  17. # Define the Shiny app
  18. ui <- fluidPage(
  19. titlePanel("Pointblank Agent Report"),
  20. sidebarLayout(
  21. sidebarPanel(
  22. actionButton("generate", "Generate Agent Report"),
  23. downloadButton("download_report", "Download Report")
  24. ),
  25. mainPanel(
  26. h4("Generated Report"),
  27. uiOutput("agent_report")
  28. )
  29. )
  30. )
  31.  
  32. server <- function(input, output, session) {
  33. # Reactive value to store the agent
  34. agent <- reactiveVal(NULL)
  35.  
  36. # Generate the agent and report when button is clicked
  37. observeEvent(input$generate, {
  38. # Generate the agent
  39. generated_agent <-
  40. create_agent(tbl = data) |>
  41. col_vals_gt(columns = a, value = vars(b)) |>
  42. col_vals_lt(columns = a, value = vars(b)) |>
  43. col_vals_equal(columns = a, value = vars(b)) |>
  44. col_vals_expr(expr = expr(equal_within(a, b, 5))) |>
  45. interrogate()
  46.  
  47. # Store the agent in a reactive value
  48. agent(generated_agent)
  49.  
  50. # Save the report to the 'www' directory
  51. report_path <- file.path("www", "agent_report.html")
  52. export_report(agent(), filename = report_path)
  53.  
  54. # Notify the user
  55. showNotification("Report generated successfully!", type = "message")
  56. })
  57.  
  58. # Display the report in the main panel
  59. output$agent_report <- renderUI({
  60. req(agent()) # Ensure the agent exists
  61. tags$iframe(
  62. src = "agent_report.html",
  63. width = "100%",
  64. height = "600px",
  65. frameborder = "0"
  66. )
  67. })
  68.  
  69. # Provide a download handler for the report
  70. output$download_report <- downloadHandler(
  71. filename = function() {
  72. "agent_report.html"
  73. },
  74. content = function(file) {
  75. file.copy(file.path("www", "agent_report.html"), file)
  76. }
  77. )
  78. }
  79.  
  80. shinyApp(ui = ui, server = server)
  81.  
Advertisement
Add Comment
Please, Sign In to add comment