Create a table with clickable hyperlink

谁说胖子不能爱 提交于 2020-01-13 06:11:42

问题


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:


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.



来源:https://stackoverflow.com/questions/32315372/create-a-table-with-clickable-hyperlink

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!