Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tib_df() with row names? #97

Open
krlmlr opened this issue Jun 23, 2022 · 1 comment
Open

tib_df() with row names? #97

krlmlr opened this issue Jun 23, 2022 · 1 comment
Labels

Comments

@krlmlr
Copy link
Collaborator

krlmlr commented Jun 23, 2022

End goal: roundtrip renv.lock 🤩

library(conflicted)
library(tidyverse)
library(tibblify)
# Need this for generated vec_chop()
library(vctrs)

renv <- jsonlite::read_json("https://gist.githubusercontent.com/krlmlr/bb42e1d83991579d839afbe43ebcaee4/raw/075fb781e609f06009c3f539117949be7ce6cdac/renv.lock")

renv$Packages <- renv$Packages[1:2]
renv %>% 
  glimpse()
#> List of 2
#>  $ R       :List of 2
#>   ..$ Version     : chr "4.1.3"
#>   ..$ Repositories:List of 1
#>   .. ..$ :List of 2
#>  $ Packages:List of 2
#>   ..$ BH :List of 12
#>   .. ..$ Package          : chr "BH"
#>   .. ..$ Version          : chr "1.78.0-0"
#>   .. ..$ Source           : chr "Repository"
#>   .. ..$ Repository       : chr "CRAN"
#>   .. ..$ RemoteType       : chr "standard"
#>   .. ..$ RemotePkgRef     : chr "BH"
#>   .. ..$ RemoteRef        : chr "BH"
#>   .. ..$ RemoteRepos      : chr "https://cran.rstudio.com/"
#>   .. ..$ RemotePkgPlatform: chr "aarch64-apple-darwin20"
#>   .. ..$ RemoteSha        : chr "1.78.0-0"
#>   .. ..$ Hash             : chr "4e348572ffcaa2fb1e610e7a941f6f3a"
#>   .. ..$ Requirements     : list()
#>   ..$ DBI:List of 6
#>   .. ..$ Package     : chr "DBI"
#>   .. ..$ Version     : chr "1.1.2"
#>   .. ..$ Source      : chr "Repository"
#>   .. ..$ Repository  : chr "CRAN"
#>   .. ..$ Hash        : chr "dcd1743af4336156873e3ce3c950b8b9"
#>   .. ..$ Requirements: list()

spec <- spec_guess(renv)
spec
#> spec_df(
#>   .names_to = ".names",
#>   Version = tib_chr("Version", required = FALSE),
#>   Repositories = tib_df(
#>     "Repositories",
#>     .required = FALSE,
#>     Name = tib_chr("Name"),
#>     URL = tib_chr("URL"),
#>   ),
#>   BH = tib_row(
#>     "BH",
#>     .required = FALSE,
#>     Package = tib_chr("Package", required = FALSE),
#>     Version = tib_chr("Version", required = FALSE),
#>     Source = tib_chr("Source", required = FALSE),
#>     Repository = tib_chr("Repository", required = FALSE),
#>     RemoteType = tib_chr("RemoteType", required = FALSE),
#>     RemotePkgRef = tib_chr("RemotePkgRef", required = FALSE),
#>     RemoteRef = tib_chr("RemoteRef", required = FALSE),
#>     RemoteRepos = tib_chr("RemoteRepos", required = FALSE),
#>     RemotePkgPlatform = tib_chr("RemotePkgPlatform", required = FALSE),
#>     RemoteSha = tib_chr("RemoteSha", required = FALSE),
#>     Hash = tib_chr("Hash", required = FALSE),
#>     Requirements = tib_unspecified("Requirements", required = FALSE),
#>   ),
#>   DBI = tib_row(
#>     "DBI",
#>     .required = FALSE,
#>     Package = tib_chr("Package", required = FALSE),
#>     Version = tib_chr("Version", required = FALSE),
#>     Source = tib_chr("Source", required = FALSE),
#>     Repository = tib_chr("Repository", required = FALSE),
#>     Hash = tib_chr("Hash", required = FALSE),
#>     Requirements = tib_unspecified("Requirements", required = FALSE),
#>   ),
#> )

# How to achieve this result (while keeping the "row names")?
renv$Packages <- unname(renv$Packages)
spec <- spec_guess(renv)
spec
#> spec_object(
#>   R = tib_row(
#>     "R",
#>     Version = tib_chr("Version"),
#>     Repositories = tib_df(
#>       "Repositories",
#>       Name = tib_chr("Name"),
#>       URL = tib_chr("URL"),
#>     ),
#>   ),
#>   Packages = tib_df(
#>     "Packages",
#>     Package = tib_chr("Package"),
#>     Version = tib_chr("Version"),
#>     Source = tib_chr("Source"),
#>     Repository = tib_chr("Repository"),
#>     RemoteType = tib_chr("RemoteType", required = FALSE),
#>     RemotePkgRef = tib_chr("RemotePkgRef", required = FALSE),
#>     RemoteRef = tib_chr("RemoteRef", required = FALSE),
#>     RemoteRepos = tib_chr("RemoteRepos", required = FALSE),
#>     RemotePkgPlatform = tib_chr("RemotePkgPlatform", required = FALSE),
#>     RemoteSha = tib_chr("RemoteSha", required = FALSE),
#>     Hash = tib_chr("Hash"),
#>     Requirements = tib_unspecified("Requirements"),
#>   ),
#> )

Created on 2022-06-23 by the reprex package (v2.0.1)

@mgirlich
Copy link
Owner

mgirlich commented Jun 24, 2022

I was just about to write that this works when using the first 4 packages. But somehow this sometimes works and sometimes doesn't...

@krlmlr The issue is that spec_guess() has to decide whether the input is an object or a list of objects. In this case it thinks that this is a named list of objects. The easiest for you would be to use spec_guess_object() so that the guesser does not have to decide what fits best.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants