从R Shiny中的本地路径在数据表中显示图像 [英] Display Image in a Data Table from a local path in R Shiny

查看:186
本文介绍了从R Shiny中的本地路径在数据表中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从本地路径在数据表中显示图像.请注意,本地路径与www文件夹不同,在实际情况下,我无法将图像从指定的路径移动到www文件夹.奇怪的是,它是从www位置工作的,而不是从指定路径工作的.寻找解决此问题的技巧.

I am trying to display the Image in a Data Table from a local path. Please note the local path is different from www folder and in real scenario, I can not move the images from the prescribed path to www folder. Strange thing is that it is working from www location and but not from the prescribed path. Looking for any tips to resolve this.

这是代码:

library(shiny)
library(shinyBS)
library(DT)

flag <- data.frame(image=c('<img src="C:/Users/string/100x100/100x100_bigimg.jpg"></img>'))

ui <- shinyUI(pageWithSidebar(
headerPanel("renderImage example"),
sidebarPanel(
actionButton("go","Go")
),
mainPanel(
bsModal("modalExample", "Image", "go", size =    "large",imageOutput("myImage")),

DT::dataTableOutput("dt")

)
))

server <- shinyServer(function(input, output, session) {

output$dt <- DT::renderDataTable({
DT::datatable(flag,escape = FALSE )
})

observeEvent(input$go,{
output$myImage <- renderImage({
# Return a list containing the filename
return(list(src = "C:/Users/string/100x100/100x100_bigimg.jpg",
     contentType = 'image/png',
     width = 550,
     height = 400,
     alt = "This is alternate text"))
}, deleteFile = FALSE)

})

})

shinyApp(ui,server)

此外,从指定的路径 renderImage 可以正常工作,但是在数据表中部分图像无法显示.

Also, from prescribed path renderImage part is working perfectly but in data table part image is not getting displayed.

推荐答案

这可以使用 addResourcePath 实现-适用于Windows以及Shiny Web Server.它添加了静态资源目录. https://shiny.rstudio.com/reference/shiny/latest/addResourcePath. html

This can be achieved using addResourcePath - Applicable to Windows as well as Shiny web Server.It adds directory of static resource. https://shiny.rstudio.com/reference/shiny/latest/addResourcePath.html

这是工作示例:

library(shiny)
library(shinydashboard)
library(shinyBS)
library(DT)

dashboardPage(
dashboardHeader(title = span(tagList(icon("image"), "Example"))),
dashboardSidebar(),
dashboardBody(

div(style="display:inline-block",uiOutput("infoButton")),

DT::dataTableOutput("table2")

 )
)

Server.R

addResourcePath("Images","D/HDImages") # Images are located outside shiny App

LeafNames <- c('Leaf1.jpg','Leaf2.jpg','Leaf3.jpg','Leaf4.jpg','Leaf5.jpg','Leaf6.jpg','Leaf7.jpg','Leaf8.jpg','Leaf9.jpg','Leaf10.jpg')
LeafTable <- data.frame(LeafNames)
LeafTable<- within(LeafTable, thumbnail <- paste0("<img 
src='","Images/",LeafTable$LeafNames,"' height='50'></img>"))

function(input, output) { 

output$table2<-DT::renderDataTable({

responseDataFilter2 <- LeafTable[,c(2,1)]

displayableData<-DT::datatable(data = as.data.frame(responseDataFilter2, stringAsFactors = FALSE, row.names = NULL),

                               escape=FALSE,selection="single",rownames=FALSE,colnames=c(" ","Name"),

                               callback = JS("table.on('dblclick.dt', 'td', function() {
                                             var row=table.cell(this).index().row;
                                             Shiny.onInputChange('rows_home',[row, Math.random()])});
                                             table.on('click.dt', 'td', function() {
                                             var k=table.cell(this).index().row;
                                             if(table.rows('.selected').indexes().toArray()!= '' && table.rows('.selected').indexes().toArray() == k){k=-1;}
                                             Shiny.onInputChange('rows_up_home',[k, Math.random()]);
                                             Shiny.onInputChange('row_path', table.rows(this).data().toArray());
                                             });"),

                               options = list(

                                 paging=FALSE,searching = FALSE,ordering=FALSE,scrollY = 750,scrollCollapse=TRUE,server = TRUE

                               ))

})

output$infoButton = renderUI({
s = input$table2_rows_selected # Row number of selected row 
if (length(s)!= 0) {
  tagList(
    actionButton("info", "",icon("info-circle"),style="color:rgb(57,156,8);border-color:rgb(255,255,255)"),

    # Information Dialog Box
    bsModal("ObjectInfo", LeafTable[s,c(1)], "info", size = "large", # Enables Pop up Screen

            img(src= paste0("Images/",LeafTable[s,c(1)]),width='800',height='600')

    )
  )

}
})

}

这篇关于从R Shiny中的本地路径在数据表中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆