### Install shinylogs Package Development Version from GitHub Source: https://github.com/dreamrs/shinylogs/blob/master/README.md Installs the development version of the shinylogs package directly from its GitHub repository. This requires the 'remotes' package and provides access to the latest features and bug fixes. ```r # install.packages("remotes") remotes::install_github("dreamRs/shinylogs") ``` -------------------------------- ### Install shinylogs Package from CRAN Source: https://github.com/dreamrs/shinylogs/blob/master/README.md Installs the released version of the shinylogs package from the Comprehensive R Archive Network (CRAN). This is the standard way to install stable package versions. ```r install.packages("shinylogs") ``` -------------------------------- ### Install Node Modules with packer Source: https://github.com/dreamrs/shinylogs/blob/master/README.md Installs the necessary Node.js modules required for the shinylogs package's JavaScript assets, managed by the 'packer' package. This command should be run in the project's root directory. ```r packer::npm_install() ``` -------------------------------- ### Custom Log Storage Function with shinylogs Source: https://context7.com/dreamrs/shinylogs/llms.txt Enables custom log storage by defining a function to handle log data, allowing integration with any backend. This example shows sending logs to an API or a database. It requires 'shiny' and 'shinylogs', and potentially 'httr' or database connection packages. ```r library(shiny) library(shinylogs) # Define custom storage function my_logger <- function(logs) { # logs is a list with: $inputs, $outputs, $errors, $session # Example: Send to API endpoint httr::POST( url = "https://api.example.com/logs", body = jsonlite::toJSON(logs, auto_unbox = TRUE), httr::content_type_json() ) # Example: Write to database # con <- dbConnect(...) # dbWriteTable(con, "logs", logs$session) # dbDisconnect(con) invisible() } server <- function(input, output, session) { # Use custom storage function track_usage( storage_mode = store_custom( FUN = my_logger, # Additional parameters passed to FUN api_key = "secret123" ) ) # Application logic here } # Example: Print structure without saving track_usage( storage_mode = store_custom(FUN = function(logs) { str(logs, max.level = 3) invisible() }) ) ``` -------------------------------- ### Initialize shinylogs Tracking in Shiny Server Source: https://github.com/dreamrs/shinylogs/blob/master/README.md Activates the shinylogs tracking functionality within the server component of a Shiny application. It specifies the storage mode and path for log files, using JSON format for storage. ```r library(shinylogs) track_usage(storage_mode = store_json(path = "logs/")) ``` -------------------------------- ### Monitor Shiny App User Activity with shinylogs (R) Source: https://context7.com/dreamrs/shinylogs/llms.txt This R code demonstrates how to use the shinylogs package to track user activity in a Shiny application. It initializes usage tracking, and then displays various reactive inputs that capture the last input change, all input changes, browser data, errors, and output regenerations. This provides real-time insights into user interactions and application behavior. ```r library(shiny) library(shinylogs) ui <- fluidPage( sliderInput("slider", "Slider:", 0, 100, 50), selectInput("select", "Select:", letters[1:5]), tags$h3("Monitoring"), verbatimTextOutput("last_input"), verbatimTextOutput("all_inputs"), verbatimTextOutput("browser_data"), verbatimTextOutput("errors"), verbatimTextOutput("outputs") ) server <- function(input, output, session) { track_usage(storage_mode = store_null(console = FALSE)) # Last input changed by user output$last_input <- renderPrint({ # Returns: list(name = "slider", timestamp = "2019-04-26...", value = 75) input$.shinylogs_lastInput }) # All inputs that have changed output$all_inputs <- renderPrint({ # Returns: list of all input changes with name, timestamp, value input$.shinylogs_input }) # Browser and session information output$browser_data <- renderPrint({ # Returns: list(user_agent, screen_res, browser_res, pixel_ratio, ...) input$.shinylogs_browserData }) # All errors generated by outputs output$errors <- renderPrint({ # Returns: list of errors with name, timestamp, error message input$.shinylogs_error }) # All outputs that have been regenerated output$outputs <- renderPrint({ # Returns: list of outputs with name, timestamp, binding input$.shinylogs_output }) } shinyApp(ui, server) ``` -------------------------------- ### Bundle JavaScript Assets with packer Source: https://github.com/dreamrs/shinylogs/blob/master/README.md Bundles the JavaScript assets for the shinylogs package using the 'packer' package. This command should be run after modifying JavaScript files (e.g., srcjs/exts/shinylogs.js) to incorporate changes into the package. ```r packer::bundle() ``` -------------------------------- ### Store Shiny Logs as JSON Files with shinylogs (R) Source: https://context7.com/dreamrs/shinylogs/llms.txt The store_json function configures shinylogs to save application usage data into individual JSON files per session. This method is suitable for analyzing session-specific activity. It requires the 'shiny' and 'shinylogs' libraries and specifies a path for log file storage. ```r library(shiny) library(shinylogs) # Create temporary directory for logs tmp <- tempdir() # Open logs directory when app stops onStop(function() { browseURL(url = tmp) }) server <- function(input, output, session) { # Store logs as JSON files track_usage( storage_mode = store_json(path = tmp) ) # Application logic here } # Example output file: shinylogs_myapp_1556273095707758000.json # Content structure: # { # "inputs": [ # { # "name": "xcol", # "timestamp": "2019-04-26T12:04:57+02:00", # "value": "Sepal.Width", # "type": "", # "binding": "shiny.selectInput" # } # ], # "outputs": [ # { # "name": "plot1", # "timestamp": "2019-04-26T12:04:58+02:00", # "binding": "shiny.imageOutput" # } # ], # "session": { # "app": "myapp", # "user": "user", # "server_connected": "2019-04-26T12:04:55+0200", # "sessionid": "2def513d3bb6b7e1a907196624d1446e", # "user_agent": "Mozilla/5.0...", # "screen_res": "1920x1080", # "browser_res": "910x928" # } # } ``` -------------------------------- ### Google Drive Log Storage with shinylogs Source: https://context7.com/dreamrs/shinylogs/llms.txt Uploads application logs as JSON files to Google Drive. This requires prior authentication with the 'googledrive' package and a specified path. It's useful for cloud-based log management and team collaboration. Requires 'shiny', 'shinylogs', and 'googledrive'. ```r library(shiny) library(shinylogs) library(googledrive) # In global.R or before app launch # Authenticate with Google Drive (one-time setup) drive_auth(path = "/path/to/service-account-token.json") # See: https://gargle.r-lib.org/articles/articles/managing-tokens-securely.html server <- function(input, output, session) { # Store logs in Google Drive folder track_usage( storage_mode = store_googledrive(path = "my-logs/") ) # Application logic here } # Note: Share the Google Drive folder with your service account email # Files created: shinylogs_myapp_1556273095707758000.json # Retrieve logs from Google Drive drive_ls("my-logs/") drive_download("my-logs/shinylogs_myapp_1556273095707758000.json") ``` -------------------------------- ### UI-Side Tracking Dependencies with shinylogs Source: https://context7.com/dreamrs/shinylogs/llms.txt Manually loads tracking dependencies in the UI using `use_tracking()`, providing more control, especially in complex applications. This allows disabling automatic dependency injection in `track_usage`. Requires 'shiny' and 'shinylogs'. ```r library(shiny) library(shinylogs) ui <- fluidPage( # Load tracking dependencies in UI use_tracking( what = c("session", "input", "output", "error"), exclude_input_regex = NULL, exclude_input_id = NULL, on_unload = FALSE, app_name = "myapp" ), splitLayout( cellArgs = list(style = "height: 250px"), radioButtons("radio", "Radio:", names(iris)), checkboxGroupInput("checkbox", "Checkbox:", names(iris)), selectInput("select", "Select:", names(iris)) ), tags$p("Last input used:"), verbatimTextOutput("last") ) server <- function(input, output, session) { # Disable automatic dependency loading since we used use_tracking() in UI track_usage( storage_mode = store_json(path = "logs/"), dependencies = FALSE ) output$last <- renderPrint({ input$.shinylogs_lastInput }) } shinyApp(ui, server) ``` -------------------------------- ### Store Shiny Logs in SQLite Database with shinylogs (R) Source: https://context7.com/dreamrs/shinylogs/llms.txt The store_sqlite function enables shinylogs to store all application logs within a single SQLite database file. This facilitates centralized logging and querying across multiple sessions, organizing data into 'session', 'inputs', 'outputs', and 'errors' tables. It requires 'shiny', 'shinylogs', 'DBI', and 'RSQLite' libraries. ```r library(shiny) library(shinylogs) library(DBI) library(RSQLite) tmp <- tempdir() server <- function(input, output, session) { # Store logs in SQLite database track_usage( storage_mode = store_sqlite(path = tmp) ) # Application logic here } # Database file created: shinylogs.sqlite # Tables created: session, inputs, outputs, errors # Query logs after collection con <- dbConnect(RSQLite::SQLite(), dbname = file.path(tmp, "shinylogs.sqlite")) sessions <- dbReadTable(con, "session") inputs <- dbReadTable(con, "inputs") outputs <- dbReadTable(con, "outputs") errors <- dbReadTable(con, "errors") dbDisconnect(con) # Example query: Get all input changes for a specific session # SELECT * FROM inputs WHERE sessionid = '2def513d3bb6b7e1a907196624d1446e' ``` -------------------------------- ### Track Shiny App Usage with shinylogs (R) Source: https://context7.com/dreamrs/shinylogs/llms.txt The track_usage function is the primary method for enabling logging in Shiny applications. It captures session information, input changes, output regenerations, and errors. Configuration includes storage mode, data to track, and exclusion patterns. It requires the 'shiny' and 'shinylogs' libraries. ```r library(shiny) library(shinylogs) ui <- fluidPage( headerPanel("Iris k-means clustering"), sidebarLayout( sidebarPanel( selectInput( inputId = "xcol", label = "X Variable", choices = names(iris) ), selectInput( inputId = "ycol", label = "Y Variable", choices = names(iris), selected = names(iris)[[2]] ), numericInput( inputId = "clusters", label = "Cluster count", value = 3, min = 1, max = 9 ) ), mainPanel( plotOutput("plot1") ) ) ) server <- function(input, output, session) { # Enable tracking with JSON storage track_usage( storage_mode = store_json(path = "logs/"), what = c("session", "input", "output", "error"), exclude_input_regex = NULL, exclude_input_id = NULL, app_name = "iris-cluster" ) selectedData <- reactive({ iris[, c(input$xcol, input$ycol)] }) clusters <- reactive({ kmeans(selectedData(), input$clusters) }) output$plot1 <- renderPlot({ palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999")) par(mar = c(5.1, 4.1, 0, 1)) plot(selectedData(), col = clusters()$cluster, pch = 20, cex = 3) points(clusters()$centers, pch = 4, cex = 4, lwd = 4) }) } shinyApp(ui, server) ``` -------------------------------- ### Read JSON Log Files with shinylogs Source: https://context7.com/dreamrs/shinylogs/llms.txt Reads and parses JSON log files from specified paths or directories. It returns structured data.tables for session metadata, input changes, errors, and output regenerations. Supports reading single files, multiple files, or all files within a directory. Dependencies include the 'shinylogs' and 'data.table' packages. ```r library(shinylogs) # Read all JSON logs from a directory logs <- read_json_logs(path = "logs/") # Structure returned: # $session - data.table with session metadata # $inputs - data.table with all input changes # $errors - data.table with all errors # $outputs - data.table with all output regenerations # Explore session data head(logs$session) # Columns: app, user, server_connected, sessionid, server_disconnected, # user_agent, screen_res, pixel_ratio, browser_connected # Explore input data head(logs$inputs) # Columns: sessionid, name, timestamp, value, type, binding # Explore output data head(logs$outputs) # Columns: sessionid, name, timestamp, binding # Explore error data head(logs$errors) # Columns: sessionid, name, timestamp, error # Read a single JSON file single_file <- "logs/shinylogs_myapp_1556273095707758000.json" logs <- read_json_logs(path = single_file) # Read multiple specific files files <- c( "logs/shinylogs_myapp_1556273095707758000.json", "logs/shinylogs_myapp_1556273104851394000.json" ) logs <- read_json_logs(path = files) # Example analysis: Count inputs by type library(data.table) logs$inputs[, .N, by = binding][order(-N)] # Example: Find most active users logs$session[, .N, by = user][order(-N)] # Example: Calculate session duration logs$session[, duration := as.numeric( difftime(server_disconnected, server_connected, units = "secs") )] ``` -------------------------------- ### Store RDS Logs with shinylogs Source: https://context7.com/dreamrs/shinylogs/llms.txt Stores application logs as R-specific RDS files, with one file generated per session. This format is ideal for preserving complex R object types within the logs. It requires the 'shiny' and 'shinylogs' libraries. ```r library(shiny) library(shinylogs) server <- function(input, output, session) { # Store logs as RDS files track_usage( storage_mode = store_rds(path = "logs/") ) # Application logic here } # Example: Read RDS logs later logs <- readRDS("logs/shinylogs_myapp_1556273095707758000.rds") # Returns a list with: $inputs, $outputs, $errors, $session str(logs, max.level = 2) ``` -------------------------------- ### Console Logging Only with shinylogs Source: https://context7.com/dreamrs/shinylogs/llms.txt Disables persistent log storage and optionally prints logs to the R console. This is useful for debugging purposes or when persistent storage is not required. It requires the 'shiny' and 'shinylogs' libraries. ```r library(shiny) library(shinylogs) ui <- fluidPage( tags$h2("Record inputs change"), fluidRow( column( width = 3, selectInput( inputId = "select", label = "Select input", choices = month.name ), numericInput( inputId = "numeric", label = "Numeric input", value = 4, min = 0, max = 20 ) ), column( width = 9, tags$b("Last input triggered:"), verbatimTextOutput(outputId = "last_input"), tags$b("All inputs:"), verbatimTextOutput(outputId = "all_inputs") ) ) ) server <- function(input, output, session) { # Don't store on disk, just print to console track_usage( storage_mode = store_null(console = TRUE) ) # Access special reactive inputs created by track_usage output$last_input <- renderPrint({ input$.shinylogs_lastInput }) output$all_inputs <- renderPrint({ input$.shinylogs_input }) } shinyApp(ui, server) ``` -------------------------------- ### Filter Users and Customize User Identification in shinylogs Source: https://context7.com/dreamrs/shinylogs/llms.txt Allows for excluding specific users from log tracking and defining custom logic for identifying users. This is useful for excluding administrative or test accounts from analytics or for integrating with custom authentication systems. Dependencies include the 'shiny' and 'shinylogs' packages. ```r library(shiny) library(shinylogs) # Custom function to get username get_username <- function(session) { # Try multiple sources if (!is.null(session$user)) { return(session$user) } # ShinyProxy if (!is.null(session$request$HTTP_X_SP_USERID)) { return(session$request$HTTP_X_SP_USERID) } # Custom header if (!is.null(session$request$HTTP_X_USERNAME)) { return(session$request$HTTP_X_USERNAME) } # Default return("anonymous") } server <- function(input, output, session) { # Exclude specific users from logging track_usage( storage_mode = store_json(path = "logs/"), exclude_users = c("admin", "test_user", "developer"), get_user = get_username ) } # Using environment variables server <- function(input, output, session) { track_usage( storage_mode = store_json(path = "logs/"), exclude_users = c(Sys.getenv("ADMIN_USER")), get_user = function(session) { # Custom logic for your environment Sys.getenv("SHINYPROXY_USERNAME", unset = session$user) } ) } ``` -------------------------------- ### Exclude Inputs from Tracking in shinylogs Source: https://context7.com/dreamrs/shinylogs/llms.txt Configures `track_usage` to exclude specific inputs from logging. This can be done using regular expressions to match input IDs or by providing explicit lists of input IDs. This is useful for preventing sensitive data or high-frequency, low-value inputs from being logged. Dependencies include the 'shiny' and 'shinylogs' packages. ```r library(shiny) library(shinylogs) server <- function(input, output, session) { # Exclude inputs by regex pattern track_usage( storage_mode = store_json(path = "logs/"), exclude_input_regex = "^password|secret|token", what = c("session", "input", "output", "error") ) # Exclude specific input IDs track_usage( storage_mode = store_json(path = "logs/"), exclude_input_id = c("user_password", "api_token", "credit_card"), what = c("session", "input", "output", "error") ) # Combine both methods track_usage( storage_mode = store_json(path = "logs/"), exclude_input_regex = "^temp_", exclude_input_id = c("debug_info", "internal_state"), what = c("session", "input", "output", "error") ) } ``` -------------------------------- ### Read RDS Log Files with shinylogs Source: https://context7.com/dreamrs/shinylogs/llms.txt Reads and parses RDS log files, providing the same structured data.table output as `read_json_logs`. This function is useful when logs have been stored using `store_rds`. It supports reading single files, multiple files, or all files within a directory. Dependencies include the 'shinylogs' and 'data.table' packages. ```r library(shinylogs) # Read all RDS logs from a directory logs <- read_rds_logs(path = "logs/") # Same structure as read_json_logs # $session, $inputs, $errors, $outputs # Read a single RDS file logs <- read_rds_logs(path = "logs/shinylogs_myapp_1556273095707758000.rds") # Read multiple specific files files <- c( "logs/shinylogs_myapp_1556273095707758000.rds", "logs/shinylogs_myapp_1556273104851394000.rds" ) logs <- read_rds_logs(path = files) # Example analysis: Track user journey through inputs library(data.table) session_id <- logs$session$sessionid[1] user_journey <- logs$inputs[sessionid == session_id][order(timestamp)] print(user_journey[, .(name, timestamp, value)]) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.