Convert list of different length into data table for markdown for html format
Asked Answered
G

1

0

This is what Im doing to generate a markdown so that all the things should be in one place.

How can i put these output into a datatable form which are more readable and easier to search.The list which is made are of different length. Each list has a series of table under it.

If there a way to convert these differing length list to data table format that would be really helpful

The table looks like this image

## Prepare for analyses
```{r,warning=FALSE,message=FALSE}
set.seed(1234)
library(europepmc)
library(tidypmc)
library(tidyverse)
#library(dplyr)

```




```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```


##Cytarabine cytogenetically normal aml adult clinical trial Randomized Controlled Trial. 828 records found, showing 10

    ```{r,include=FALSE}
b <-epmc_search(query = 'cytarabine cytogenetically normal aml adult clinical trial Randomized Controlled Trial  OPEN_ACCESS:Y',limit = 10)
pmcids <- b$pmcid[b$isOpenAccess=="Y"]
docs <- map(pmcids, epmc_ftxt)
my_tables <- map(docs, pmc_table)

    ```
    
```{r}
names(my_tables) <- pmcids

```
    
    
The code chunk input and output is then displayed as follows:

```{r basicconsole}
source("flat.R")

L1 <- flattenlist(my_tables)

l.f <- Filter(function(a) any(!is.na(a)), L1)
l.f


#tibble:::print.tbl_df(head(df))

#n <- paste0("Valporic_", names(l.f), ".txt")

for (i in 1:length(l.f)) {
  write.table(l.f[i], sep = "\t",row.names = FALSE,col.names = TRUE,file=paste0(names(l.f)[i], ".txt"))
}

UPDATE

I have manged to covert those tibble into dataframe

using this solution ##Outout

```{r}
abc <- mapply(cbind, l.f)
abc

But when it is rendered in the markdown the column formatting is gone. Now i have now dataframe inside list.

But still im not sure how to put that into a data table

**UPDATE 2.0 ** The better approach is to read those saved output as list of files into data table and then use it as markdown but so far it is taking only one ID only. My code.

tbl_fread <- 
  list.files(pattern = "*.txt") %>% 
  map_df(~fread(.))

knitr::kable(head(tbl_fread), "pipe")

Is it possible to put these files as such.

if a list of file are from one PMCID then those would be all in one column such as if PMCID one has 3 output then all of them should be one the same row. Then the next PMCID in the second one etc etc.

UPDATE new

I have managed to align the output into more readable format. But It seems that by default all the files assigned to multiple columns which would be the case given that im reading all the files together since my idea of using the list to data table didn't work.

If i can push or stack each unique PMCID over one another instead of all in one after another that would be. Good

knitr::kable(tbl_fread, align = "lccrr")
Gillespie answered 31/1, 2021 at 12:12 Comment(3)
What output are you using? html, pdf, Word, other?Matchlock
html ..i m using so it can have a search bar to find feature we are looking intoGillespie
Hello @Matchlock file link for you to test tempdrive.io/us/testfilesGillespie
M
0

This may be something you can adapt for R Markdown. I'm not sure what the rationale is to save and load the tables. Instead, you could obtain the tables and show in html directly.

As you are using HTML, make sure to have results='asis' in your chunk. You can use a for loop and seq_along to show each table. You can include information in your table caption, such as the PMCID as well as table number.

---
title: "test13121"
author: "Ben"
date: "1/31/2021"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Libraries

```{r}
library(tidypmc)
library(tidyverse)
library(europepmc)
library(kableExtra)
```

# Get Articles

```{r, echo = FALSE}
b <-epmc_search(query = 'cytarabine aml OPEN_ACCESS:Y',limit = 6)
pmcids <- b$pmcid[b$isOpenAccess=="Y"]
docs <- map(pmcids, epmc_ftxt)
my_tables <- map(docs, pmc_table)
names(my_tables) <- pmcids
```

# Show Tables

```{r, echo=F, results='asis'}
for (i in seq_along(my_tables)) {
  for (j in seq_along(my_tables[[i]])) {
    print(kable(x = my_tables[[i]][[j]], caption = paste0(names(my_tables)[i], ": Table ", j)))
  }
}
```
Matchlock answered 31/1, 2021 at 16:30 Comment(3)
let me run real quick "I'm not sure what the rationale is to save and load the tables" this i was doing since i didn;t succeed in doing it inside the markdown in the first placeGillespie
you saved another 10 -20 hours of stack search of trial and error. I m not sure how to thank you. One upvote for your answer is not enough. But if im able to communicate the paper whoever helped form stack cite them all. First of all my r skills are as good as my requirement such as simple data analysis, differential gene expression then other exploratory stuffs. but when comes to these data parsing kind of stuffs I have real hard time.Gillespie
when it comes to loop and that with list my head can;t just wrap aroundGillespie

© 2022 - 2024 — McMap. All rights reserved.