Search code examples
rdataframeshinydatatable

R shiny - Datatable displays [object Object]


Data output from API

             arthor_uuid rank score                                                                   smiles compound identifier                             
1 c8975496-4cb7-5fa6-a181-4044f1478eef    0 1.000 c1cc2cc(c(nc2c(c1)Cl)c3ccc[n+](c3)[O-])[C@@H](C(F)(F)F)Nc4c5c(cccn5)ncn4      AC000005306130
2 7b5b0d02-9b98-53dc-8366-08cb05f624bb    1 1.000  c1cc2cc(c(nc2c(c1)Cl)c3ccc[n+](c3)[O-])[C@H](C(F)(F)F)Nc4c5c(cccn5)ncn4      AC000005306132
                                                                                                                                                         vendor entries
1 GOSTAR, InChI, 3610820, LNLJHGXOFYUARS-NRFANRHFSA-N, false, false, 29-SEP-2022 10:39:33 +01:00, 29-SEP-2022 10:39:33 +01:00, Virtual, Virtual, Commercial, Commercial
2 GOSTAR, InChI, 3610727, LNLJHGXOFYUARS-OAQYLSRUSA-N, false, false, 29-SEP-2022 10:39:33 +01:00, 29-SEP-2022 10:39:33 +01:00, Virtual, Virtual, Commercial, Commercial

I want to display all data - including the "vendor entries" subset of data - in a datatable. Instead of the "vendor entries" data, "[object Object],[object Object]" is displayed.

If it is complicate to get the "vendor entries" data in a datatable, I would like at least to know how to get the "LNLJHGXOFYUARS-NRFANRHFSA-N" and "LNLJHGXOFYUARS-OAQYLSRUSA-N" values from the vendor entries column.

I have tried:

Attempt 1.

filtered_data <- as.matrix(unlist(data["vendor entries"]))

result:

                                    [,1]                         
vendor entries.vendor name1         "GOSTAR"                     
vendor entries.vendor name2         "InChI"                      
vendor entries.vendors identifier1  "3610820"                    
vendor entries.vendors identifier2  "LNLJHGXOFYUARS-NRFANRHFSA-N"
vendor entries.discontinued1        "false"                      
vendor entries.discontinued2        "false"                      
vendor entries.timestamp1           "29-SEP-2022 10:39:33 +01:00"
vendor entries.timestamp2           "29-SEP-2022 10:39:33 +01:00"
vendor entries.source labels1       "Virtual"                    
vendor entries.source labels2       "Virtual"                    
vendor entries.compound categories1 "Commercial"                 
vendor entries.compound categories2 "Commercial"                 
vendor entries.vendor name1         "GOSTAR"                     
vendor entries.vendor name2         "InChI"                      
vendor entries.vendors identifier1  "3610727"                    
vendor entries.vendors identifier2  "LNLJHGXOFYUARS-OAQYLSRUSA-N"
vendor entries.discontinued1        "false"                      
vendor entries.discontinued2        "false"                      
vendor entries.timestamp1           "29-SEP-2022 10:39:33 +01:00"
vendor entries.timestamp2           "29-SEP-2022 10:39:33 +01:00"
vendor entries.source labels1       "Virtual"                    
vendor entries.source labels2       "Virtual"                    
vendor entries.compound categories1 "Commercial"                 
vendor entries.compound categories2 "Commercial"  

then

   filtered_data["vendor entries.vendors identifier2"]

however, only the first value of "vendor entries.vendors identifier2" is displayed. The second value is missing. Is there a way to get all values from the "vendor entries.vendors identifier2" rows?

Attempt 2.

    api_json <- jsonlite::toJSON(structure_data, pretty = TRUE)

result

[
  {
    "arthor_uuid": "c8975496-4cb7-5fa6-a181-4044f1478eef",
    "rank": 0,
    "score": "1.000",
    "smiles": "c1cc2cc(c(nc2c(c1)Cl)c3ccc[n+](c3)[O-])[C@@H](C(F)(F)F)Nc4c5c(cccn5)ncn4",
    "compound identifier": "AC000005306130",
    "vendor entries": [
      {
        "vendor name": "GOSTAR",
        "vendors identifier": "3610820",
        "discontinued": "false",
        "timestamp": "29-SEP-2022 10:39:33 +01:00",
        "source labels": ["Virtual"],
        "compound categories": ["Commercial"]
      },
      {
        "vendor name": "InChI",
        "vendors identifier": "LNLJHGXOFYUARS-NRFANRHFSA-N",
        "discontinued": "false",
        "timestamp": "29-SEP-2022 10:39:33 +01:00",
        "source labels": ["Virtual"],
        "compound categories": ["Commercial"]
      }
    ]
  },
  {
    "arthor_uuid": "7b5b0d02-9b98-53dc-8366-08cb05f624bb",
    "rank": 1,
    "score": "1.000",
    "smiles": "c1cc2cc(c(nc2c(c1)Cl)c3ccc[n+](c3)[O-])[C@H](C(F)(F)F)Nc4c5c(cccn5)ncn4",
    "compound identifier": "AC000005306132",
    "vendor entries": [
      {
        "vendor name": "GOSTAR",
        "vendors identifier": "3610727",
        "discontinued": "false",
        "timestamp": "29-SEP-2022 10:39:33 +01:00",
        "source labels": ["Virtual"],
        "compound categories": ["Commercial"]
      },
      {
        "vendor name": "InChI",
        "vendors identifier": "LNLJHGXOFYUARS-OAQYLSRUSA-N",
        "discontinued": "false",
        "timestamp": "29-SEP-2022 10:39:33 +01:00",
        "source labels": ["Virtual"],
        "compound categories": ["Commercial"]
      }
    ]
  }
] 

enter image description here


Solution

  • Example of what you're after here:

    x <- '[
      {
        "arthor_uuid": "c8975496-4cb7-5fa6-a181-4044f1478eef",
        "rank": 0,
        "score": "1.000",
        "smiles": "c1cc2cc(c(nc2c(c1)Cl)c3ccc[n+](c3)[O-])[C@@H](C(F)(F)F)Nc4c5c(cccn5)ncn4",
        "compound identifier": "AC000005306130",
        "vendor entries": [
          {
            "vendor name": "GOSTAR",
            "vendors identifier": "3610820",
            "discontinued": "false",
            "timestamp": "29-SEP-2022 10:39:33 +01:00",
            "source labels": ["Virtual"],
            "compound categories": ["Commercial"]
          },
          {
            "vendor name": "InChI",
            "vendors identifier": "LNLJHGXOFYUARS-NRFANRHFSA-N",
            "discontinued": "false",
            "timestamp": "29-SEP-2022 10:39:33 +01:00",
            "source labels": ["Virtual"],
            "compound categories": ["Commercial"]
          }
        ]
      },
      {
        "arthor_uuid": "7b5b0d02-9b98-53dc-8366-08cb05f624bb",
        "rank": 1,
        "score": "1.000",
        "smiles": "c1cc2cc(c(nc2c(c1)Cl)c3ccc[n+](c3)[O-])[C@H](C(F)(F)F)Nc4c5c(cccn5)ncn4",
        "compound identifier": "AC000005306132",
        "vendor entries": [
          {
            "vendor name": "GOSTAR",
            "vendors identifier": "3610727",
            "discontinued": "false",
            "timestamp": "29-SEP-2022 10:39:33 +01:00",
            "source labels": ["Virtual"],
            "compound categories": ["Commercial"]
          },
          {
            "vendor name": "InChI",
            "vendors identifier": "LNLJHGXOFYUARS-OAQYLSRUSA-N",
            "discontinued": "false",
            "timestamp": "29-SEP-2022 10:39:33 +01:00",
            "source labels": ["Virtual"],
            "compound categories": ["Commercial"]
          }
        ]
      }
    ] '
    
    jsonlite::parse_json(x) |> 
      tibble::tibble() |> 
      tidyr::unnest_wider(1) |> 
      tidyr::unnest_longer(`vendor entries`) |> 
      tidyr::unnest_wider(`vendor entries`) |> 
      tidyr::unnest_longer(c(`source labels`, `compound categories`))
    

    This flattens the list elements to look like this:

    # A tibble: 4 × 11
      arthor_uuid                   rank score smiles compo…¹ vendo…² vendo…³ disco…⁴ times…⁵ sourc…⁶ compo…⁷
      <chr>                        <int> <chr> <chr>  <chr>   <chr>   <chr>   <chr>   <chr>   <chr>   <chr>  
    1 c8975496-4cb7-5fa6-a181-404…     0 1.000 c1cc2… AC0000… GOSTAR  3610820 false   29-SEP… Virtual Commer…
    2 c8975496-4cb7-5fa6-a181-404…     0 1.000 c1cc2… AC0000… InChI   LNLJHG… false   29-SEP… Virtual Commer…
    3 7b5b0d02-9b98-53dc-8366-08c…     1 1.000 c1cc2… AC0000… GOSTAR  3610727 false   29-SEP… Virtual Commer…
    4 7b5b0d02-9b98-53dc-8366-08c…     1 1.000 c1cc2… AC0000… InChI   LNLJHG… false   29-SEP… Virtual Commer…
    # … with abbreviated variable names ¹​`compound identifier`, ²​`vendor name`, ³​`vendors identifier`,
    #   ⁴​discontinued, ⁵​timestamp, ⁶​`source labels`, ⁷​`compound categories`
    

    for more, you should look at the new chapter in R4DS about rectangling: https://r4ds.hadley.nz/rectangling.html