Data does not round trip through `writePreference()` and `readPreference()`
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 38/100
Research direction
Start by reproducing the reprex with rstudioapi::writePreference() and readPreference(), comparing the nested list structure with waldo::compare(). Trace how these functions serialize and reconstruct named lists. Done means a value written as a named list reads back with the same structure, without requiring the JSON-string workaround described in the issue.
Written by the indexing model from the issue text.
Description
Related to #247, I discovered that readPreference() does not preserve the structure of the value as it was provided to writePreference(). At a quick glance, the difference looks a lot like the data is being serialized as JSON but read with auto_unbox = FALSE.
Here's a small reprex list:
value_write <- list(
fruit = "banana",
drink = "coffee"
)
rstudioapi::writePreference("test", value_write)
value_read <- rstudioapi::readPreference("test", default = NULL)
waldo::compare(value_write, value_read)
#> `old$fruit` is a character vector ('banana')
#> `new$fruit` is a list
#>
#> `old$drink` is a character vector ('coffee')
#> `new$drink` is a list
str(value_read)
#> List of 2
#> $ fruit:List of 1
#> ..$ : chr "banana"
#> $ drink:List of 1
#> ..$ : chr "coffee"
I thought I could handle serialization myself with jsonlite::serializeJSON(), but this led to #247.
value_write_json <- jsonlite::serializeJSON(value_write)
rstudioapi::writePreference("test", value_write_json)
#> Error in .rs.writePrefInternal("rs_writeApiPref", prefName, value) :
#> Type mismatch: expected <Object>; got <String>
I can safely roundtrip the data by serializing to a json string and storing in a named list, so I have a workaround, but it means a lot of boilerplate code to work around how the list is handled.
rstudioapi::writePreference("test", list(value = value_write_json))
# because of the first bit, we need $value[[1]] instead of just $value
value_read_json <- rstudioapi::readPreference("test", NULL)$value[[1]]
value_read_json <- jsonlite::unserializeJSON(value_read_json)
waldo::compare(value_write, value_read_json)
#> ✓ No differences
Session Info
> rstudioapi::versionInfo()
$mode
[1] "desktop"
$version
[1] ‘2022.6.0.25’
$long_version
[1] "2022.06.0-daily+25"
$release_name
[1] "Spotted Wakerobin"
─ Session info ─────────────────────────────────────────────────────────────────────
setting value
version R version 3.6.3 (2020-02-29)
os OS X Snow Leopard 11.6.2
system x86_64, darwin15.6.0
ui RStudio
language (EN)
collate en_US.UTF-8
ctype en_US.UTF-8
tz America/New_York
date 2022-01-28
─ Packages ─────────────────────────────────────────────────────────────────────────
package * version date lib source
jsonlite 1.7.3 2022-01-17 [1] CRAN (R 3.6.3)
rstudioapi 0.13.0-9000 2022-01-28 [1] Github (rstudio/rstudioapi@5d0f087)
- Dominant language
- R
- Stars
- 175
- Forks
- 37
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from rstudio/rstudioapi
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
rstudio/rstudioapi#313 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 48/100
rstudio/rstudioapi#321 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
rstudio/rstudioapi#318 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
rstudio/rstudioapi#315 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
rstudio/rstudioapi#314 ·
All issues in rstudio/rstudioapi
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
r-lib/pkgdepends#485 · 3 comments ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
beginners blocker
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enviPathR OpenBuild Error Build OK Build Warning policies-accepted pre-review precheck-passed
Difficulty 1/5 Under an hour Newbie friendliness 84/100
Bioconductor/BiocContributions#207 · 6 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
datacarpentry/semester-biology#1255 ·