larry77

Untitled

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