Rev. | 4752cb7f5726b6f6aa8767a2d5b1223ed67b9c46 |
---|---|
大小 | 2,130 字节 |
时间 | 2025-01-15 02:06:34 |
作者 | Lorenzo Isella |
Log Message | A code showing a shiny interface which generates a pointblank validation table. |
library(shiny)
library(dplyr)
library(pointblank)
# Create the sample dataset
data <- tibble(
a = c(100, 150, 200, 105, 70),
b = c(102, 148, 210, 110, 500)
)
# Define the custom function in the global environment
equal_within <- function(x, y, percent) {
abs(x - y) / abs(pmin(x, y)) <= percent / 100
}
assign("equal_within", equal_within, envir = .GlobalEnv) # Explicit assignment
# Define the Shiny app
ui <- fluidPage(
titlePanel("Pointblank Agent Report"),
sidebarLayout(
sidebarPanel(
actionButton("generate", "Generate Agent Report"),
downloadButton("download_report", "Download Report")
),
mainPanel(
h4("Generated Report"),
uiOutput("agent_report")
)
)
)
server <- function(input, output, session) {
# Reactive value to store the agent
agent <- reactiveVal(NULL)
# Generate the agent and report when button is clicked
observeEvent(input$generate, {
# Generate the agent
generated_agent <-
create_agent(tbl = data) |>
col_vals_gt(columns = a, value = vars(b)) |>
col_vals_lt(columns = a, value = vars(b)) |>
col_vals_equal(columns = a, value = vars(b)) |>
col_vals_expr(expr = expr(equal_within(a, b, 5))) |>
interrogate()
# Store the agent in a reactive value
agent(generated_agent)
# Save the report to the 'www' directory
report_path <- file.path("www", "agent_report.html")
export_report(agent(), filename = report_path)
# Notify the user
showNotification("Report generated successfully!", type = "message")
})
# Display the report in the main panel
output$agent_report <- renderUI({
req(agent()) # Ensure the agent exists
tags$iframe(
src = "agent_report.html",
width = "100%",
height = "600px",
frameborder = "0"
)
})
# Provide a download handler for the report
output$download_report <- downloadHandler(
filename = function() {
"agent_report.html"
},
content = function(file) {
file.copy(file.path("www", "agent_report.html"), file)
}
)
}
shinyApp(ui = ui, server = server)