convert column values to html tags

Is there a way to convert column values to html tags? For example, I have these columns in a dataframe.

dput(df)
structure(list(ColA = c("1", "1,2"), ColB = c("asd", "asd,dfdsf"
)), class = "data.frame", row.names = c(NA, -2L))
df
ColA   ColB 
1      asd
1,2    asd,dfdsf

Expected output

ColA   ColB                 Colc 
1      asd               <table border=1><tr><th>Ratings</th><th>Text</th></tr><tr><td>1</td><td>asd</td></tr><tr></tr></table
1,2    asd,dfdsf         <table border=1><tr><th>Ratings</th><th>Text</th></tr><tr><td>1</td><td>asd</td></tr><tr><td>2</td><td>dfdsf</td></tr></table>

The values in Colc are the values in ColA and ColB. However, I am trying to make a table from it. Can anyone help me?

Try something like this


df <- structure(list(ColA = c("1", "1,2"), ColB = c("asd", "asd,dfdsf"
)), class = "data.frame", row.names = c(NA, -2L))

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(purrr)
library(magrittr)
#> 
#> Attaching package: 'magrittr'
#> The following object is masked from 'package:purrr':
#> 
#>     set_names
library(glue)
#> 
#> Attaching package: 'glue'
#> The following object is masked from 'package:dplyr':
#> 
#>     collapse

df2 <- 
  dplyr::mutate(df,ColC = purrr::map2(ColA,ColB,
            function(ca,cb){
              # glue::glue("ca: {ca}, cb: {cb}")
              ca <- strsplit(ca,',')[[1]] 
              cb <- strsplit(cb,',')[[1]] 
              ca <- (c(ca,''))[1:2]
              cb <- (c(cb,''))[1:2]
              # glue::glue("ca: {ca}, cb: {cb}")
              p1 <- "<table border=1><tr><th>Ratings</th><th>Text</th></tr>"
              p2 <- paste(glue::glue("<tr><td>{ca}</td><td>{cb}</td></tr>"),collapse = '')
              p3 <- "</table>"
              paste(p1,p2,p3,sep='')
            }))
df2$ColC  
#> [[1]]
#> [1] "<table border=1><tr><th>Ratings</th><th>Text</th></tr><tr><td>1</td><td>asd</td></tr><tr><td></td><td></td></tr></table>"
#> 
#> [[2]]
#> [1] "<table border=1><tr><th>Ratings</th><th>Text</th></tr><tr><td>1</td><td>asd</td></tr><tr><td>2</td><td>dfdsf</td></tr></table>"
Created on 2021-06-17 by the reprex package (v2.0.0)

Very thanks. It worked......

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.