简体   繁体   English

如何在不丢失格式的情况下将 R 中的列表列表保存到文件中?

[英]How can I save list of lists in R to a file without losing the format?

How can I store the list of lists in R into a file so that when I read it again it is in exactly the same format as it was before I stored it?如何将 R 中的列表列表存储到文件中,以便当我再次读取它时它的格式与我存储它之前的格式完全相同? Here is how the object looks before I save it:这是 object 在我保存之前的样子:

$parameters_model_r$cost_type
[1] "FTC"

$parameters_model_r$input_path
[1] "/mnt/OUTPUT/CPM_PET_US/MODEL/DATA"

$parameters_model_r$file_type
[1] "parquet"

$parameters_model_r$delimiter
[1] ","

$parameters_model_r$inferSchema
[1] "true"

$parameters_model_r$label
[1] "CASS_LINE_COST"

And its type: str(config)及其类型: str(config)

 $ parameters_model_r         :List of 22
  ..$ cost_type         : chr "FTC"
  ..$ input_path        : chr "/mnt/OUTPUT/CPM_PET_US/MODEL/DATA"
  ..$ file_type         : chr "parquet"
  ..$ delimiter         : chr ","
  ..$ inferSchema       : chr "true"
  ..$ label             : chr "CASS_LINE_COST"
  ..$ model_output_path : chr "/dbfs/mnt/OUTPUT/CPM_PET_US/MODEL/AGGREGATED_LEVEL/MODEL_OUTPUT"
  ..$ split_percentage  : num 0.75
  ..$ stratify_by       :List of 1
  .. ..$ columns: chr [1:3] "CUSTOMER" "PKGTECH" "REGION"
  ..$ glasso_stratify_by:List of 1
  .. ..$ columns: chr [1:2] "CUSTOMER" "PKGTECH"
  ..$ remove_cols       :List of 1
  .. ..$ columns: chr [1:5] "YEARPERIOD" "STRATIFY_BY" "YEARPERIODWEEK" "SEGMENTATION" ...
  ..$ n_folds           : int 10
  ..$ lambda_lower_bound: num 0.001
  ..$ lambda_upper_bound: num 0.01

Ideally I would need to convert this to Json, but every time I use toJSON() the format changes and then when I read it back in it no longer serves the purpose because in my modelling script everything needs to be passed in specifically in the same way as I show above理想情况下,我需要将其转换为 Json,但每次我使用 toJSON() 时,格式都会发生变化,然后当我读回它时,它不再起作用,因为在我的建模脚本中,所有内容都需要在同一就像我上面展示的那样

I don't know which exact function you used, but with the toJSON and fromJSON functions from the jsonlite package everything seems to work fine:我不知道您使用了哪个确切的 function,但是使用jsonlite package 中的toJSONfromJSON函数,一切似乎都可以正常工作:

library(jsonlite)

# create nested list
my_list <- list(irisA = list(irisA1 = iris),
                irisB = list(irisB1 = iris),
                irisC = list(irisC1 = iris))

str(my_list, max.level = 2)

#> List of 3
#> $ irisA:List of 1
#> ..$ irisA1:'data.frame': 150 obs. of  5 variables
#> $ irisB:List of 1
#> ..$ irisB1:'data.frame': 150 obs. of  5 variables
#> $ irisC:List of 1
#> ..$ irisC1:'data.frame': 150 obs. of  5 variables

# convert to JSON
my_json <- jsonlite::toJSON(my_list)

# convert back
str(jsonlite::fromJSON(my_json), max.level = 2)

#> List of 3
#> $ irisA:List of 1
#> ..$ irisA1:'data.frame': 150 obs. of  5 variables
#> $ irisB:List of 1
#> ..$ irisB1:'data.frame': 150 obs. of  5 variables
#> $ irisC:List of 1
#> ..$ irisC1:'data.frame': 150 obs. of  5 variables

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM