【问题标题】:How to embed an image in a cell a table using R and Shiny?如何使用 R 和 Shiny 在表格的单元格中嵌入图像?
【发布时间】:2019-09-04 08:29:18
【问题描述】:

我正在尝试创建一个书籍目录,需要帮助在表格中的单元格中渲染图像。我正在使用的代码如下,我从闪亮应用程序的代码中获得的输出是一个带有列“图像”但在其单元格中包含图像的链接而不是图像。我该如何纠正这个问题?请帮助我 数据集中的 URL 格式如下:https://images.gr-assets.com/books/1447303603s/2767052.jpg

数据是这样的

title authors ratings_count average_rating image_url HP JK 10 4 https://images.gr-assets.com/books/1447303603s/2767052.jpg

ui <- fluidPage(

  ####Heading##
  titlePanel(div(HTML("<b> Interested In books? </b>"))),

  ###Creating tabs###
  tabsetPanel(


    ####First tab for crime####
    tabPanel(" Book Directory ",
             sidebarLayout(

               sidebarPanel(

                 #First Input##
                 selectizeInput(inputId = "Book",
                                label = " Choose a Book",
                                choices = book_names)),

               ##Output
               mainPanel = (tableOutput("View")
               )
             )
    )
  )
)



###Server app
server <- function(input, output) {
  output$View <- renderTable({
    books1 <- books[books$title%in% input$Book,]
    books1  %>% 
      mutate(image = paste0('<img src="', image_url, '"></img>')) %>%  
      select(image,title,authors,average_rating,ratings_count) 
      })
}

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny shinydashboard shiny-server shiny-reactivity


    【解决方案1】:

    我之前用 tableHTML 包做过类似的事情,事实上你也可以用它给你的表格添加各种格式,试试这个例子:

    库和示例数据

    library(tableHTML)
    library(shiny)
    library(dplyr)
    books <- read.table(text = "title authors ratings_count average_rating        image_url
     HP     JK        10            4                https://images.gr-assets.com/books/1447303603s/2767052.jpg", header=TRUE)
    
    books_names <- unique(books$title)
    

    用户界面(相同的用户界面):

    ui <- fluidPage(
      titlePanel(div(HTML("<b> Interested In books? </b>"))),
      tabsetPanel(
        tabPanel(" Book Directory ",
                 sidebarLayout(
                   sidebarPanel(
                     selectizeInput(inputId = "Book",
                                    label = " Choose a Book",
                                    choices = books_names)),
                   mainPanel = (tableOutput("View"))
                 )
        )
      )
    )
    

    服务器:

    server <- function(input, output) {
      output$View <- render_tableHTML({
        books[books$title%in% input$Book,] %>% 
          mutate(image = paste0('<img src="', image_url, '"></img>')) %>%  
          select(image,title,authors,average_rating,ratings_count) %>% 
          tableHTML(escape = FALSE, 
                    rownames = FALSE, 
                    widths = c(40, 40, 65, 120, 120)) %>% 
          # align the text like this
          add_css_table(css = list('text-align', 'center'))
          # you can also add a theme 
          # add_theme('scientific')
      })
    }
    

    运行应用:

    shinyApp(ui = ui, server = server)
    

    您可以使用add_css_... 系列函数以任何您喜欢的方式格式化您的表格,例如add_css_table(css = list('text-align', 'center')) 将文本与表格的中心对齐。

    查看包的vignettes 以了解该包提供的其他功能

    【讨论】:

    • 我试过了,它可以工作。但是我的表格输出没有对齐。例如表格中单元格的内容完全左对齐。我该如何解决这个问题?
    • >感谢您的回答。但是,这会对齐我的文本而不是表格的标题。我应该添加什么来对齐列名(中心)?另外如何增加单元格中显示字段的字体大小?请建议可以做什么
    • 你也可以简单地为标题添加一个css,尝试添加%&gt;% add_css_header(css = list(c('text-align', 'font-size'), c('center', '15px')), headers = 1:ncol(books)),我真的建议你看一下包的示例和文档
    • 这很完美。我会看看文档。谢谢
    • 很高兴为您提供帮助!如果我的解决方案解决了您的问题,请接受它作为答案。
    猜你喜欢
    • 2015-08-20
    • 2017-12-26
    • 1970-01-01
    • 2018-03-14
    • 2021-01-30
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    相关资源
    最近更新 更多