IDEMSInternational / R-Instat

A statistics software package powered by R
http://r-instat.org/
GNU General Public License v3.0
38 stars 102 forks source link

Why can't I import a json file into R-Instat #9076

Open rdstern opened 1 month ago

rdstern commented 1 month ago

I asked @jkmusyoka for an example of a json file and he supplied the following: definitions_chipata2024.20240718123607 (1).json

I am impressed I can share this file on github, without zipping it! I would like to look at the contents in R-Instat - then I would be able to understand the materials in our google buckets work better that @lilyclements is constructing.

So I would like to useFile > Import Data to import the file into R-Instat. I found that rio imports (and exports) json files, using the jsonlite package. And jsonlite is already in R-Instat. My understanding is that they would come into a set of data frames, because the json data could be nested? Nice - so a data book!

So why can't I simply import it, using File > Import Data. It says:

image

And, because it says it can't handle that type of file, I can't see the script? Could someone initially supply the script, so we can import? And that should enable the dialog to be able to handle json files?

For completeness could we also extend the File > Export > Export Data. The current Export does export a single dataframe into a json file. Great. I assume it should also be an option for multiple data frames into a single file? It isn't yet.

lilyclements commented 1 month ago

To generally read in a json file, you can use jsonlite::read_json, e.g.

jsonlite::read_json("C:/Users/lclem/Downloads/definitions_chipata2024.20240718123607.1.json")

However, that format is a bit messy. We have written functions that can read in from the bucket, and reformat it to a data frame format:

epicsawrap::gcs_auth_file("path/to/your/token.json")

Then there's a few options:

You can get for all stations by the following.

epicsawrap::station_metadata(country = "zm_workshop", include_definitions = TRUE, format = "wide")
epicsawrap::station_metadata(country = "zm_workshop", include_definitions = TRUE, format = "long")

Or just for your station:

epicsawrap::station_metadata(country = "zm_workshop", station_id = "chipata", include_definitions = TRUE, format = "long")
lilyclements commented 1 month ago

@rdstern to add: In R-Instat you can read this in then by the following:

epicsawrap::gcs_auth_file(file = "path/to/your/epicsa_token.json")
my_definitions <- epicsawrap::station_metadata(country = "zm_workshop", station_id = "chipata", include_definitions = TRUE, format = "long")
data_book$import_data(data_tables=list(my_definitions=my_definitions))
rdstern commented 1 month ago

@jkmusyoka the above looks great in being able - with a simple script - to see, in R-Instat, what's in the google bucket. This fits well with having some scripts in the R-Instat script library, perhaps in an epicsa directory. a) Can you send me directly, the epicsa_token? b) Do you have the R script from @lilyclements to update the google-buckets directly. I'd really also like to have that script in the R-Instat library and to test using that from within R-Instat. c) @lilyclements and @jkmusyoka I would still like to be able to load a json file, if possible also into the script window as (another?) way of looking at it in R-Instat? Here is what I would like to have:

image

Is there anything wrong with wanting that?
If not, then I had to change the extension to txt and then it reads fine. So maybe @derekagorhom could add the json extension as an allowed format in the load and save code?

lilyclements commented 1 month ago

c) @lilyclements and @jkmusyoka I would still like to be able to load a json file, if possible also into the script window as (another?) way of looking at it in R-Instat?

jsonlite::read_json is a way to read a json file in from the script window - so using your example at the top of this issue, and addding some changes to put into a data frame:

jsonlite::read_json("C:/Users/lclem/Downloads/definitions_chipata2024.20240718123607.1.json")

x <- jsonlite::read_json("C:/Users/lclem/Downloads/definitions_chipata2024.20240718123607.1.json")

x <- data.frame(unlist(x))

data_book$import_data(data_tables=list(x=x))