### Working with Protocol Buffer Extensions Source: https://context7.com/eddelbuettel/rprotobuf/llms.txt Demonstrates how to set and get extension fields in protocol buffer messages. It includes using `setExtension` to add extension values and `getExtension` to retrieve them, along with checking the initialization status after setting extensions. ```r # Set an extension field person$setExtension(field = my_extension_descriptor, values = "extension_value") # Get an extension field ext_value <- person$getExtension(field = my_extension_descriptor) # Check number of extensions set person$isInitialized() ``` -------------------------------- ### Install RProtoBuf Package Source: https://github.com/eddelbuettel/rprotobuf/blob/master/README.md Installs the RProtoBuf package from CRAN using the standard R installation command. This is the recommended way to install the package for most users. ```r install.packages("RProtoBuf") ``` -------------------------------- ### Install Protocol Buffer Compiler and Development Libraries (Debian/Ubuntu) Source: https://github.com/eddelbuettel/rprotobuf/blob/master/README.md Installs the necessary system dependencies for Protocol Buffers on Debian/Ubuntu systems. This includes the protobuf-compiler and development libraries required for building RProtoBuf from source or for certain functionalities. ```bash sudo apt-get install protobuf-compiler libprotobuf-dev libprotoc-dev ``` -------------------------------- ### Getting Protocol Buffer Library Version Source: https://context7.com/eddelbuettel/rprotobuf/llms.txt Illustrates how to retrieve the version of the Protocol Buffers library linked with rprotobuf. It provides methods to get the version as an integer or a formatted string. ```r # Get version as integer version_int <- getProtobufLibVersion() # Get formatted version version_str <- getProtobufLibVersion(format = TRUE) print(version_str) # e.g., "3.21.12" ``` -------------------------------- ### Message Introspection and Descriptors in rprotobuf Source: https://context7.com/eddelbuettel/rprotobuf/llms.txt Provides examples of how to introspect protocol buffer messages and their descriptors. It covers retrieving message and file descriptors, accessing descriptor properties like name, field count, and nested types, as well as getting specific field descriptors and their attributes. ```r # Get message descriptor msg_descriptor <- descriptor(person) msg_descriptor <- person$descriptor() # Get file descriptor file_desc <- fileDescriptor(person) # Access descriptor properties desc <- P("tutorial.Person") desc$name() desc$field_count() desc$nested_type_count() desc$enum_type_count() # Get specific field descriptor field_desc <- desc$field(0) # Get first field field_desc <- desc$field("name") # Get field by name # Field descriptor properties field_desc$name() field_desc$type() field_desc$number() field_desc$is_required() field_desc$is_optional() field_desc$is_repeated() field_desc$has_default_value() field_desc$default_value() ``` -------------------------------- ### Load Protocol Buffer Definitions in R Source: https://context7.com/eddelbuettel/rprotobuf/llms.txt This section demonstrates how to load Protocol Buffer definition files (.proto) within the R environment using the RProtoBuf package. It covers loading default definitions, from specific directories or files, and using custom search paths. The `resetDescriptorPool()` function is also shown for resetting the pool and reloading default protos. ```r # Load .proto files from the RProtoBuf package (default) readProtoFiles(package = "RProtoBuf") # Load .proto files from a specific directory readProtoFiles(dir = "/path/to/proto/files") # Load specific .proto files readProtoFiles(files = c("schema1.proto", "schema2.proto")) # Load with a custom search path readProtoFiles2(files = "addressbook.proto", protoPath = c("./proto", "/usr/local/proto")) # Reset the descriptor pool and reload default protos resetDescriptorPool() ``` -------------------------------- ### Loading Protocol Buffer Definitions Source: https://context7.com/eddelbuettel/rprotobuf/llms.txt This section details how to load Protocol Buffer (.proto) definition files into R using RProtoBuf. You can load default definitions, files from a specific directory, individual files, or specify custom search paths. ```APIDOC ## Loading Protocol Buffer Definitions This section details how to load Protocol Buffer (.proto) definition files into R using RProtoBuf. You can load default definitions, files from a specific directory, individual files, or specify custom search paths. ### Method ```r readProtoFiles(package = "RProtoBuf") readProtoFiles(dir = "/path/to/proto/files") readProtoFiles(files = c("schema1.proto", "schema2.proto")) readProtoFiles2(files = "addressbook.proto", protoPath = c("./proto", "/usr/local/proto")) resetDescriptorPool() ``` ### Description - `readProtoFiles()`: Loads .proto files. Can specify `package`, `dir`, or `files`. - `readProtoFiles2()`: Similar to `readProtoFiles` but with an explicit `protoPath` argument. - `resetDescriptorPool()`: Clears the current descriptor pool and reloads default protobuf definitions. ``` -------------------------------- ### Creating Protocol Buffer Messages Source: https://context7.com/eddelbuettel/rprotobuf/llms.txt Demonstrates how to create and populate Protocol Buffer messages in R. This includes creating new messages, updating existing ones, and handling nested and repeated fields. ```APIDOC ## Creating Protocol Buffer Messages Demonstrates how to create and populate Protocol Buffer messages in R. This includes creating new messages, updating existing ones, and handling nested and repeated fields. ### Method ```r library(RProtoBuf) readProtoFiles(package = "RProtoBuf") # Create a new message person <- new(tutorial.Person, name = "John Doe", id = 12345, email = "john.doe@example.com") # Create nested message objects phone <- new(tutorial.Person.PhoneNumber, number = "+1-555-1234", type = "MOBILE") # Update message with new fields person <- update(person, phone = phone) # Create using prototype pattern with repeated fields person2 <- update(new(tutorial.Person), name = "Jane Smith", id = 67890, email = "jane.smith@example.com", phone = list(new(tutorial.Person.PhoneNumber, number = "+1-555-5678", type = "HOME"), new(tutorial.Person.PhoneNumber, number = "+1-555-9999", type = "WORK"))) # Build complex messages with repeated fields addressbook <- new(tutorial.AddressBook, person = list(person, person2)) # Display the message structure print(addressbook) writeLines(as.character(addressbook)) ``` ### Description - `new(MessageType, ...)`: Creates a new instance of a Protocol Buffer message. - `update(message, ...)`: Updates an existing message with new or modified fields. - `as.character(message)`: Converts a message to its string representation. - `writeLines(text)`: Writes lines of text to a connection, useful for viewing string representations. ``` -------------------------------- ### Working with Repeated Fields in Protocol Buffers Source: https://context7.com/eddelbuettel/rprotobuf/llms.txt Illustrates how to manage repeated fields within protocol buffer messages. This includes creating messages with repeated fields, adding elements, checking the size, swapping elements, and accessing individual items. ```r # Create message with repeated field book <- new(tutorial.AddressBook) # Add items to repeated fields book$person <- list(person, person2) # Get size of repeated field book$size("person") # Swap elements in repeated field (1-based indexing) book$swap("person", 1L, 2L) # Access individual elements first_person <- book$person[[1]] second_person <- book$person[[2]] ``` -------------------------------- ### Clearing and Cloning Protocol Buffer Messages Source: https://context7.com/eddelbuettel/rprotobuf/llms.txt Demonstrates how to clone messages for deep copying and how to clear fields. It shows clearing all fields, clearing a specific field, and checking the initialization status of a message after clearing fields. ```r # Clone a message (deep copy) person_copy <- person$clone() # Clear all fields person$clear() writeLines(as.character(person)) # Empty message # Clear specific field person$clear("email") person$has("email") # Returns FALSE # Check if message is initialized (all required fields set) person$isInitialized() ``` -------------------------------- ### Serializing R Objects to Protocol Buffers with rprotobuf Source: https://context7.com/eddelbuettel/rprotobuf/llms.txt Explains how to serialize native R objects (like data frames, lists, matrices) into the Protocol Buffers format using `serialize_pb` and deserialize them back using `unserialize_pb`. It also shows how to check if an object can be efficiently serialized. ```r # Serialize native R objects using rexp.proto format temp_file <- tempfile() serialize_pb(iris, temp_file) # Deserialize back to R object iris_restored <- unserialize_pb(temp_file) identical(iris, iris_restored) # TRUE # Works with various R types data_list <- list( numbers = c(1, 2, 3, 4, 5), strings = c("a", "b", "c"), matrix = matrix(1:9, nrow = 3), dataframe = data.frame(x = 1:3, y = c("A", "B", "C")) ) serialize_pb(data_list, temp_file) restored_list <- unserialize_pb(temp_file) identical(data_list, restored_list) # Check if object can be efficiently serialized can_serialize_pb(iris) # Check serialization capability ``` -------------------------------- ### Accessing Protocol Buffer Descriptors by Type Name Source: https://context7.com/eddelbuettel/rprotobuf/llms.txt Demonstrates how to access protocol buffer message descriptors using type names with the `P()` function. It covers retrieving descriptors for nested types, loading proto files in one call, and creating new messages from descriptors. ```r # Get descriptor using P() function person_desc <- P("tutorial.Person") phone_desc <- P("tutorial.Person.PhoneNumber") # Load proto file and get descriptor in one call desc <- P("tutorial.AddressBook", file = "addressbook.proto") # Create new message from descriptor new_person <- person_desc$new(name = "Test", id = 1) ``` -------------------------------- ### Message Comparison and Equality in rprotobuf Source: https://context7.com/eddelbuettel/rprotobuf/llms.txt Shows how to compare protocol buffer messages for equality using `identical`, `==`, `!=`, and `all.equal`. This allows verifying if two messages have the same content or identifying differences. ```r # Check equality identical(person1, person2) person1 == person2 person1 != person2 # Detailed comparison all.equal(person1, person2) ``` -------------------------------- ### Read Partial Protocol Buffer Messages with readASCII Source: https://context7.com/eddelbuettel/rprotobuf/llms.txt Demonstrates how to read partial messages using the `readASCII` function with the `partial = TRUE` option. This is useful when dealing with incomplete or streaming data, and allows checking initialization status. ```r partial_msg <- readASCII(tutorial.Person, 'name: "Partial"', partial = TRUE) partial_msg$isInitialized() # May return FALSE if required fields missing ``` -------------------------------- ### Serializing Messages to Binary Format Source: https://context7.com/eddelbuettel/rprotobuf/llms.txt Covers the process of serializing R Protocol Buffer messages into the compact binary format. This includes writing to files, raw vectors, and connections. ```APIDOC ## Serializing Messages to Binary Format Covers the process of serializing R Protocol Buffer messages into the compact binary format. This includes writing to files, raw vectors, and connections. ### Method ```r # Serialize to a file serialize(addressbook, "/tmp/addressbook.pb") # Serialize to a raw vector (returns raw bytes) raw_bytes <- serialize(addressbook, NULL) # Serialize to a binary connection conn <- file("/tmp/output.pb", "wb") serialize(addressbook, conn) close(conn) # Get the byte size of a message bytesize(addressbook) length(serialize(addressbook, NULL)) ``` ### Description - `serialize(message, connection)`: Serializes a Protocol Buffer message to a given connection or `NULL` for a raw vector. - `bytesize(message)`: Returns the size of the serialized message in bytes. ``` -------------------------------- ### JSON Format Operations with Protocol Buffers Source: https://context7.com/eddelbuettel/rprotobuf/llms.txt Shows how to convert R objects to JSON strings and read them back using `toJSON`, `writeLines`, and `readJSON`. It covers basic conversion, preserving field names, always printing primitive fields, and reading from both strings and connections. ```r # Convert message to JSON json_str <- person$toJSON() writeLines(json_str) # Convert with formatting options json_formatted <- toJSON(person, preserve_proto_field_names = TRUE, always_print_primitive_fields = TRUE) # Read from JSON string json_input <- '{"name": "Charlie", "id": 333, "email": "charlie@example.com"}' person_from_json <- readJSON(tutorial.Person, json_input) # Read from JSON connection json_conn <- textConnection('{"name":"Dave","id":444}') person_json <- readJSON(tutorial.Person, json_conn) ``` -------------------------------- ### Reading Messages from Binary Format Source: https://context7.com/eddelbuettel/rprotobuf/llms.txt Details how to deserialize binary Protocol Buffer data back into R messages. Supports reading from file paths, connections, and raw byte vectors. ```APIDOC ## Reading Messages from Binary Format Details how to deserialize binary Protocol Buffer data back into R messages. Supports reading from file paths, connections, and raw byte vectors. ### Method ```r # Read from a file path msg1 <- read(tutorial.AddressBook, "/tmp/addressbook.pb") # Alternative syntax using descriptor msg2 <- tutorial.AddressBook$read("/tmp/addressbook.pb") # Read from a binary connection conn <- file("/tmp/addressbook.pb", "rb") msg3 <- read(tutorial.AddressBook, conn) close(conn) # Read from raw bytes raw_data <- readBin("/tmp/addressbook.pb", raw(), 10000) msg4 <- read(tutorial.AddressBook, raw_data) # Verify messages are identical identical(as.character(msg1), as.character(msg2)) ``` ### Description - `read(MessageType, connection)`: Reads a serialized Protocol Buffer message from a connection or file path. - `MessageType$read(connection)`: An alternative syntax for reading messages. - `readBin(file, what, n)`: Reads binary data from a file into a raw vector. - `identical(x, y)`: Checks if two R objects are identical. ``` -------------------------------- ### Serialize RProtoBuf Messages to Binary in R Source: https://context7.com/eddelbuettel/rprotobuf/llms.txt This R code provides methods for serializing Protocol Buffer messages into binary format. It shows how to serialize directly to a file, to a raw vector (byte stream), or to a binary connection. The `bytesize` function is also demonstrated for obtaining the size of a serialized message. ```r # Serialize to a file serialize(addressbook, "/tmp/addressbook.pb") # Serialize to a raw vector (returns raw bytes) raw_bytes <- serialize(addressbook, NULL) # Serialize to a binary connection conn <- file("/tmp/output.pb", "wb") serialize(addressbook, conn) close(conn) # Get the byte size of a message bytesize(addressbook) length(serialize(addressbook, NULL)) ``` -------------------------------- ### Read RProtoBuf Messages from Binary in R Source: https://context7.com/eddelbuettel/rprotobuf/llms.txt This section details how to deserialize Protocol Buffer messages from binary data in R. It covers reading from file paths, binary connections, and raw byte vectors. Various syntaxes for reading, including using the message descriptor directly or via a method call, are presented. An `identical` check is included for verifying deserialized messages. ```r # Read from a file path msg1 <- read(tutorial.AddressBook, "/tmp/addressbook.pb") # Alternative syntax using descriptor msg2 <- tutorial.AddressBook$read("/tmp/addressbook.pb") # Read from a binary connection conn <- file("/tmp/addressbook.pb", "rb") msg3 <- read(tutorial.AddressBook, conn) close(conn) # Read from raw bytes raw_data <- readBin("/tmp/addressbook.pb", raw(), 10000) msg4 <- read(tutorial.AddressBook, raw_data) # Verify messages are identical identical(as.character(msg1), as.character(msg2)) ``` -------------------------------- ### ASCII Text Format Operations Source: https://context7.com/eddelbuettel/rprotobuf/llms.txt Provides functionality for converting Protocol Buffer messages to and from the human-readable ASCII text format. ```APIDOC ## ASCII Text Format Operations Provides functionality for converting Protocol Buffer messages to and from the human-readable ASCII text format. ### Method ```r # Convert message to ASCII text format text_str <- toTextFormat(person) writeLines(text_str) # Read from ASCII text string ascii_input <- 'name: "Alice" id: 111 email: "alice@example.com"' person_from_ascii <- readASCII(tutorial.Person, ascii_input) # Read from ASCII text connection ascii_conn <- textConnection('name: "Bob"\nid: 222\nemail: "bob@test.com"') person_ascii <- readASCII(tutorial.Person, ascii_conn) ``` ### Description - `toTextFormat(message)`: Converts a Protocol Buffer message to its ASCII text representation. - `readASCII(MessageType, connection)`: Reads a Protocol Buffer message from an ASCII text source (string or connection). - `textConnection(object)`: Creates a text mode connection to an R object. ``` -------------------------------- ### Create Protocol Buffer Messages in R Source: https://context7.com/eddelbuettel/rprotobuf/llms.txt This snippet illustrates how to create and manipulate Protocol Buffer messages in R using RProtoBuf. It shows creating new messages from descriptors, including nested messages and repeated fields. The `update` function is used to add or modify fields, and messages can be displayed or converted to character format. ```r library(RProtoBuf) readProtoFiles(package = "RProtoBuf") # Create a new message using the descriptor person <- new(tutorial.Person, name = "John Doe", id = 12345, email = "john.doe@example.com") # Create nested message objects phone <- new(tutorial.Person.PhoneNumber, number = "+1-555-1234", type = "MOBILE") # Update message with new fields person <- update(person, phone = phone) # Create using prototype pattern person2 <- update(new(tutorial.Person), name = "Jane Smith", id = 67890, email = "jane.smith@example.com", phone = list( new(tutorial.Person.PhoneNumber, number = "+1-555-5678", type = "HOME"), new(tutorial.Person.PhoneNumber, number = "+1-555-9999", type = "WORK") )) # Build complex messages with repeated fields addressbook <- new(tutorial.AddressBook, person = list(person, person2)) # Display the message structure print(addressbook) writeLines(as.character(addressbook)) ``` -------------------------------- ### RProtoBuf ASCII Text Format Operations in R Source: https://context7.com/eddelbuettel/rprotobuf/llms.txt This R code snippet demonstrates operations related to the ASCII text format for Protocol Buffer messages using RProtoBuf. It shows how to convert a message to its ASCII text representation and how to parse messages from ASCII text strings or connections. ```r # Convert message to ASCII text format text_str <- toTextFormat(person) writeLines(text_str) # Read from ASCII text string ascii_input <- 'name: "Alice" id: 111 email: "alice@example.com"' person_from_ascii <- readASCII(tutorial.Person, ascii_input) # Read from ASCII text connection ascii_conn <- textConnection('name: "Bob"\nid: 222\nemail: "bob@test.com"') person_ascii <- readASCII(tutorial.Person, ascii_conn) ``` -------------------------------- ### Access and Modify Protocol Buffer Message Fields in R Source: https://context7.com/eddelbuettel/rprotobuf/llms.txt This R code demonstrates how to access and modify fields of Protocol Buffer messages. It covers retrieving field descriptors, using the '$' and '[[' operators for access and assignment, handling repeated fields, checking for field existence, and obtaining field counts and names. Conversion to an R list is also shown. ```r # Get descriptor for a message type desc <- P("tutorial.Person") # Access fields using $ operator person$name person$id person$email # Access repeated fields person$phone[[1]]$number person$phone[[2]]$type # Modify fields using $ assignment person$name <- "Updated Name" person$email <- "newemail@example.com" # Using [[ operator for field access by name or index person[["name"]] person[[1]] # Access first field # Using [[<- for assignment person[["id"]] <- 99999 # Check if a field has been set person$has("email") person$has("phone") # Get field count length(person) # Get field names names(person) # Convert to list person_list <- as.list(person) ``` -------------------------------- ### Accessing and Modifying Message Fields Source: https://context7.com/eddelbuettel/rprotobuf/llms.txt Explains how to access and modify fields within Protocol Buffer messages using various methods, including the `$` operator, `[[ ]]` operator, and specific helper functions. ```APIDOC ## Accessing and Modifying Message Fields Explains how to access and modify fields within Protocol Buffer messages using various methods, including the `$` operator, `[[ ]]` operator, and specific helper functions. ### Method ```r # Get descriptor for a message type desc <- P("tutorial.Person") # Access fields using $ operator person$name person$id person$email # Access repeated fields person$phone[[1]]$number person$phone[[2]]$type # Modify fields using $ assignment person$name <- "Updated Name" person$email <- "newemail@example.com" # Using [[ operator for field access by name or index person[["name"]] person[[1]] # Access first field # Using [[<- for assignment person[[ "id" ]] <- 99999 # Check if a field has been set person$has("email") person$has("phone") # Get field count length(person) # Get field names names(person) # Convert to list person_list <- as.list(person) ``` ### Description - `P(MessageType)`: Retrieves the descriptor for a given message type. - `$`: Accesses or assigns values to message fields by name. - `[[ ]]`: Accesses or assigns values to message fields by name or index. - `$has(field_name)`: Checks if a specific field has been set. - `length(message)`: Returns the number of set fields in a message. - `names(message)`: Returns a character vector of the names of the set fields. - `as.list(message)`: Converts the message to an R list. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.