Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
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

There's also a slight modification that allows adding multiple rows:

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

Parent/Child Rows in Shiny R with a single dataframe that has a variable number of rows

Parent/Child Rows in R shiny Package

Parent/Child Rows in Shiny R with a single dataframe that has a variable number of rows

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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. The resulting string will be rendered.

This line is necessary in UI for output of rendered HTML:

htmlOutput("txt")

This you get in your browser:

enter image description here

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda