You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The goal of **lzstring-r** is to provide an R wrapper for the [lzstring C++ library](https://github.com/andykras/lz-string-cpp). [lzstring](https://github.com/pieroxy/lz-string) is originally a JavaScript library that provides fast and efficient string compression and decompression using a [LZ-based algorithm](https://en.wikipedia.org/wiki/Lempel–Ziv–Welch).
29
+
30
+
Credit goes to [Winston Chang](https://github.com/wch) for spotting this missing R package and guiding me over at the R Shinylive repo—check out his awesome contributions which this repo is based on [here](https://github.com/posit-dev/r-shinylive/issues/70) and [here](https://github.com/posit-dev/r-shinylive/pull/71). Also, shoutout to Andy Kras for his implementation in C++ of lzstring, which you can find right [here](https://github.com/andykras/lz-string-cpp), and [pieroxy](https://github.com/pieroxy), the original brain behind lzstring in JavaScript—peek at his work over [here](https://github.com/pieroxy/lz-string).
29
31
30
-
The goal of lzstring-r is to provide an R wrapper for the [lzstring C++ library](https://github.com/andykras/lz-string-cpp). [lzstring](https://github.com/pieroxy/lz-string) is originally a JavaScript library that provides fast and efficient string compression and decompression using a [LZ-based algorithm](https://en.wikipedia.org/wiki/Lempel–Ziv–Welch). Credit goes to [Winston Chang](https://github.com/wch) for spotting this missing R package and guiding me over at the R Shinylive repo—check out his awesome contributions which this repo is based on [here](https://github.com/posit-dev/r-shinylive/issues/70) and [here](https://github.com/posit-dev/r-shinylive/pull/71). Also, shoutout to Andy Kras for his implementation in C++ of lzstring, which you can find right [here](https://github.com/andykras/lz-string-cpp), and [pieroxy](https://github.com/pieroxy), the original brain behind lzstring in JavaScript—peek at his work over [here](https://github.com/pieroxy/lz-string).
32
+
---
31
33
32
34
## Installation
33
35
34
-
You can install the released version of lzstringr from [CRAN](https://CRAN.R-project.org/package=lzstring) with:
36
+
You can install the released version of lzstring from [CRAN](https://CRAN.R-project.org/package=lzstring) with:
35
37
36
-
```r
38
+
```r
37
39
install.packages("lzstring")
38
40
```
39
41
40
-
You can install the development version of lzstringr from [GitHub](https://github.com/) with:
42
+
Or the development version from [GitHub](https://github.com/parmsam/lzstring-r):
41
43
42
-
```r
44
+
```r
43
45
# install.packages("devtools")
44
46
devtools::install_github("parmsam/lzstring-r")
45
47
```
46
48
47
-
## Example
49
+
---
50
+
51
+
## Usage
48
52
49
-
This is a basic example which shows you how to solve a common problem:
53
+
### Basic Example
50
54
51
55
```{r example}
52
56
library(lzstring)
53
57
54
-
# text data
55
-
message = "The quick brown fox jumps over the lazy dog!";
56
-
57
-
compressed = lzstring::compressToBase64(message)
58
+
# Text data
59
+
message <- "The quick brown fox jumps over the lazy dog!"
x <- lzstring::decompressFromEncodedURIComponent("NobwRAdghgtgpmAXGKAHVA6VBPMAaMAYwHsIAXOcpMAMwCdiYACAZwAsBLCbDOAD1R04LFkw4xUxOmTERUAVzJ4mQiABM4dZfI4AdCPp0YuCsgH0WAGw4a6ACl2RHyxwDlnTAAzKAjJ+9MAEyeAJT64RAAAqq2GBR8ZPoaNExkCXYhiPpMOSpwZPJ0EEw0jhAAVIFioiAmihgQGUzlQQC+jvpgrQC6QA")
125
149
y <- jsonlite::fromJSON(x)
126
150
cat(y$name)
127
151
cat(y$content)
128
152
```
153
+
154
+
---
155
+
156
+
## Encoding and Limitations
157
+
158
+
> **Important:**
159
+
> - lzstring operates on strings. For non-string or binary data, encode as JSON or base64 first.
160
+
> - Always ensure your input is UTF-8 encoded.
161
+
> - If you compress an R object directly (without serialization), the result may not decompress as expected.
162
+
163
+
---
164
+
165
+
## Troubleshooting
166
+
167
+
-**Why do I get an empty string after decompressing?**
168
+
This may happen if the input was not properly encoded, or if the compressed string is corrupted.
169
+
170
+
-**Why does my decompressed JSON fail to parse?**
171
+
Ensure you serialize your R object to JSON (or use `serializeJSON`) before compressing.
172
+
173
+
-**Can I compress binary data?**
174
+
Encode it as base64 or hex first, then compress the resulting string.
175
+
176
+
---
177
+
178
+
## Use Cases
179
+
180
+
- Sharing Shiny app code via URL (see [Shinylive](https://shinylive.io/r/app/))
181
+
- Compact storage of large JSON blobs
182
+
- Embedding compressed data in web apps
183
+
-**Automatic Shinylive links in documentation:**
184
+
The [roxy.shinylive](https://github.com/insightsengineering/roxy.shinylive) package uses lzstring to provide a [roxygen2](https://roxygen2.r-lib.org/) extension that automatically takes the code from the `@examples` tag and creates a URL to the [shinylive.io](https://shinylive.io/r/app/) service. During documentation build, a new section is added to the function manual containing the link and an iframe to the application itself.
0 commit comments