【问题标题】:Extend width of column with renderDataTable in Shiny在 Shiny 中使用 renderDataTable 扩展列的宽度
【发布时间】:2020-05-14 05:08:18
【问题描述】:

我无法理解使用 Shiny 的 renderDataTable 函数的行为。
我正在尝试扩展 一个 特定 宽度
当我不使用 Shiny 并且只是试图可视化表格的输出时,我编写以下内容并在图中得到预期的输出(Amazon Title 列已扩展):

Category <- c("Tools & Home Improvement", "Tools & Home Improvement")
AmazonTitle <- c("0.15,Klein Tools NCVT-2 Non Contact Voltage Tester- Dual Range Pen Voltage Detector for Standard and Low Voltage with 3 m Drop Protection", " ABCDFGEGEEFE")
ASIN_url <- c("<a href='https://www.amazon.com/dp/B004FXJOQO'>https://www.amazon.com/dp/B004FXJOQO</a>", "<a href='https://www.amazon.com/dp/B004FXJOQO'>https://www.amazon.com/dp/B0043XJOQO</a>")
ASIN <- c("B004FXJOQO", "B0043XJOQO")

All_ASIN_Information <- data.frame(Category, AmazonTitle, ASIN_url, ASIN)

DT::datatable(All_ASIN_Information, escape=FALSE, 
              options = list(
                pageLength = 20, autoWidth = TRUE,
                columnDefs = list(list( targets = 2, width = '600px'))
              )
)

但是当我在 Shiny 的 DT::renderDataTable 函数中使用这个确切的块时,结果不同并且列宽没有扩展.... 使用以下代码查看 Shiny 的行为:

library(shiny)
library(DT)


ui <- fluidPage(

  mainPanel(

    DT::dataTableOutput("Table_ASIN")))


server <- function(input, output){

  output$Table_ASIN <- DT::renderDataTable(

    DT::datatable(All_ASIN_Information, escape=FALSE, 
                  options = list(
                    pageLength = 20, autoWidth = TRUE,
                    columnDefs = list(list( targets = 2, width = '600px'))
                  )))

}

shinyApp(ui, server)

我不知道这种行为是否是由在“ASIN_url”列中创建的超链接引起的,但无论如何我真的需要它们。

对此非常感谢!

【问题讨论】:

    标签: html r datatable shiny dt


    【解决方案1】:

    一种选择是像这样缩短链接:

    ASIN_url <- c("<a href='https://www.amazon.com/dp/B004FXJOQO'>Link</a>", "<a href='https://www.amazon.com/dp/B004FXJOQO'>Link</a>")
    
    

    另一种方法是通过在选项列表中包含scrollX = TRUE 来添加滚动条

    【讨论】:

    • 选项 scrollX=TRUE 使它工作!尽管这对我来说没有意义,因为初始表格不需要滚动条就可以放入主页。谢谢!
    猜你喜欢
    • 2017-10-28
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多