将不同长度的列表转换成数据表进行html格式的markdown [英] Convert list of different length into data table for markdown for html format

查看:51
本文介绍了将不同长度的列表转换成数据表进行html格式的markdown的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我为了生成降价而做的事情,以便所有的东西都应该放在一个地方.

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

表格看起来像这样

## 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"))
}

更新

我已经设法将这些小标题转换为数据帧

I have manged to covert those tibble into dataframe

使用此解决方案##出局

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

但是当它在 Markdown 中呈现时,列格式就消失了.现在我在列表中有数据框了.

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

**更新 2.0 **更好的方法是将那些保存的输出作为文件列表读取到数据表中,然后将其用作降价,但到目前为止它只使用一个 ID.我的代码.

**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.

如果文件列表来自一个 PMCID,那么它们将全部在一列中,例如如果 PMCID 有 3 个输出,那么它们都应该是同一行.然后是第二个中的下一个 PMCID 等等.

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.

更新新的

我设法将输出对齐为更易读的格式.但似乎默认情况下所有文件都分配给了多个列,因为我将列表用于数据表的想法不起作用,因此我将所有文件一起读取.

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.

如果我可以将每个唯一的 PMCID 一个接一个地推入或堆叠起来,而不是一个接一个地堆叠.好

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")

推荐答案

这可能是您可以适应 R Markdown 的内容.我不确定保存和加载表格的基本原理是什么.相反,您可以直接获取表格并在 html 中显示.

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.

当您使用 HTML 时,请确保在您的块中有 results='asis'.您可以使用 for 循环和 seq_along 来显示每个表.您可以在表格中包含信息caption,例如 PMCID 以及表格编号.

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)))
  }
}
```

这篇关于将不同长度的列表转换成数据表进行html格式的markdown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆