Create a table with clickable hyperlink

2019-04-14 10:20发布

I have an R data frame, which is displayed on RShiny output called by renderDataTable. However, I am unable to implement a simple Java or html tags that helps me to do the following.

Example: (I am inserting the server.ui code, considering that these parameters are to be set at server.ui end. ) For simplification representing only 2 rows. mydataframe

Col1     Col2                  Col3
Google   5 lines description   www.google.com
Yahoo    5 lines description   www.yahoo.com

Goal is to

  1. rederDataTable output on shiny so that, "Google" and "Yahoo" are clickable labels, with their links (Col3) saved into them. Thereby, reducing 3 columns to 2.

Your help and suggestions are highly appreciated.

output$PM_output <- renderDataTable(expr = mydataframe),
                              options = list(autoWidth = T,
                              LengthMenu = c(5, 30, 50),
                              columnDefs = list(list(targets = c(6,7,8,9) - 1,
                              searchable = F)),
                              pageLength = 5,
                              selection = 'multiple'))

1条回答
ら.Afraid
2楼-- · 2019-04-14 10:45

You can use the escape argument to datatables, see https://rstudio.github.io/DT/#escaping-table-content.

shinyApp(
    shinyUI(
        fluidPage(
            dataTableOutput('PM_output')
        )
    ),
    shinyServer(function(input, output, session) {
        require(DT)
        dat <- read.table(text="Col1     Col2                  Col3
          Google   '5 lines description'   www.google.com
          Yahoo    '5 lines description'   www.yahoo.com", header=T, strings=F)
        dat$Col3 <- sapply(dat$Col3, function(x) 
            toString(tags$a(href=paste0("http://", x), x)))

        output$PM_output <- renderDataTable(expr = datatable(dat, escape=FALSE),
          options = list(autoWidth = T))
    })
)

setting escape=3 (the column number) also seems to work, or passing escape argument to renderDataTable.

查看更多
登录 后发表回答