简体   繁体   English

R 数据表中的闪亮父子行

[英]R Shiny parent child rows in datatable

I am trying to reuse this datable example of child/parent rows in a shiny app that I am building:我正在尝试在我正在构建的闪亮应用程序中重用这个子/父行的可数据示例:

https://rstudio.github.io/DT/002-rowdetails.html https://rstudio.github.io/DT/002-rowdetails.html

There's also a slight modification that allows adding multiple rows:还有一个允许添加多行的轻微修改:

https://github.com/rstudio/DT/issues/393#issuecomment-279627237 https://github.com/rstudio/DT/issues/393#issuecomment-279627237

However, my case is a bit different.但是,我的情况有点不同。 I have a dataframe with a main grouping parent variable and child rows.我有一个包含主要分组父变量和子行的数据框。 Child rows can be anything from 0 to n.子行可以是从 0 到 n 的任何值。 I want to show my parent variable once and all it's children hidden beneath it.我想显示一次我的父变量,所有的子变量都隐藏在它下面。

Here's some sample data:以下是一些示例数据:

library(dplyr)
df = data.frame() %>%
  rbind(c("parent1", "childA", "desc1")) %>%
  rbind(c("parent1", "childB", "desc2")) %>%
  rbind(c("parent2", "childC", "desc3")) %>%
  rbind(c("parent3", "childD", "desc4")) %>%
  rbind(c("parent4", "childE", "desc5")) %>%
  rbind(c("parent4", "childF", "desc6")) %>%
  rbind(c("parent4", "childG", "desc7")) %>%
  `colnames<-`(c("parentID", "childID", "childDesc"))

Caveat: I don't know javascript and I have no idea how to adapt such code.警告:我不懂 javascript,也不知道如何修改这些代码。 I've seen multiple examples attempting to address the same issue, however, there is so much code and customization to them.我已经看到多个示例试图解决同一问题,但是,它们有太多的代码和自定义。 I was hoping the simpler example above is easier to modify and someone can walk me through it.我希望上面更简单的例子更容易修改,有人可以引导我完成它。 I also don't need any fancy formatting.我也不需要任何花哨的格式。 Here are some examples I've seen:以下是我见过的一些例子:

Parent/Child Rows in R R 中的父/子行

Parent/Child Rows in Shiny R with a single dataframe that has a variable number of rows Shiny R 中的父/子行具有可变行数的单个数据框

Parent/Child Rows in R shiny Package R 闪亮包中的父/子行

Parent/Child Rows in Shiny R with a single dataframe that has a variable number of rows Shiny R 中的父/子行具有可变行数的单个数据框

You can use this code in server part:您可以在服务器部分使用此代码:

    output$txt <- renderUI({
  df = 
    data.frame() %>%
    rbind(c("parent1", "childA", "desc1")) %>%
    rbind(c("parent1", "childB", "desc2")) %>%
    rbind(c("parent2", "childC", "desc3")) %>%
    rbind(c("parent3", "childD", "desc4")) %>%
    rbind(c("parent4", "childE", "desc5")) %>%
    rbind(c("parent4", "childF", "desc6")) %>%
    rbind(c("parent4", "childG", "desc7")) %>%
    `colnames<-`(c("parentID", "childID", "childDesc")) %>%
    # this has already been done by you
    group_by(parentID) %>% #you group the frame by parents
    summarise(children = paste(childID, collapse  = "</p><p>"))%>% 
    #apply the aggregate function
    mutate(concatenated = paste0("<h3>", parentID, "</h3>", "<p>", children,"</p>"))
    #combine the two columns:
  HTML(paste(df$concatenated, sep = '<br/>'))
  
}) 

The code will group the dataframe and later on aggregate the necessary strings with html tags as separators.该代码将对数据框进行分组,然后使用 html 标签作为分隔符聚合必要的字符串。 The resulting string will be rendered.将呈现结果字符串。

This line is necessary in UI for output of rendered HTML:此行在 UI 中对于渲染 HTML 的输出是必需的:

htmlOutput("txt")

This you get in your browser:这是您在浏览器中获得的:

在此处输入图片说明

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

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