闪亮

2017-10-12 182 views
0

超链接的多页图像比方说,我有如下一个输入data frame闪亮

dat <- data.frame(
    country = c('USA', 'China', 'UK', "Germany", "France"),            
    flag = c('<img src="http://upload.wikimedia.org/wikipedia/en/thumb/a/a4/Flag_of_the_United_States.svg/200px-Flag_of_the_United_States.svg.png" width="100" height="80"></img>', '<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Flag_of_the_People%27s_Republic_of_China.svg/200px-Flag_of_the_People%27s_Republic_of_China.svg.png"width="100" height="80"></img>', '<img src="https://upload.wikimedia.org/wikipedia/en/a/ae/Flag_of_the_United_Kingdom.svg"width="100" height="80"></img>', '<img src="https://upload.wikimedia.org/wikipedia/en/b/ba/Flag_of_Germany.svg"width="100" height="80"></img>', '<img src="https://upload.wikimedia.org/wikipedia/en/c/c3/Flag_of_France.svg"width="100" height="80"></img>'),              
    infolink = c('https://en.wikipedia.org/wiki/United_States', 'https://en.wikipedia.org/wiki/China', 'https://en.wikipedia.org/wiki/United_Kingdom', 'https://en.wikipedia.org/wiki/Germany', 'https://en.wikipedia.org/wiki/France')) 

我原来data frame大。数据帧有3列country,flaginfolink。列country有国家名称。列flag有一个将显示标志的图像源。并且,列infolink具有国家维基百科页面的网址。我的目标是使用renderDataTable显示datatable格式的国家/地区的国家/地区的国旗。在哪里,每个国家的国旗应该超链接到它在infolink专栏中的各自wikipedia网址。

有多种SO已经问上超链接的图像问题:

Making an Image Hyperlink in R Shiny header

how to create a hyperlink interactively in shiny app?

然而,这些问题具有超链接相应的图像信号源没有多个图像的超链接。

以下是我闪亮的应用程序代码:

library(shiny) 
# UI for application 
ui <- fluidPage(

    # Application title 
    titlePanel("Image Hyperlinking Test"), 

    mainPanel(
    a(dataTableOutput("imagetest"), href=dat$infolink, target="_blank") 
    ) 
) 

# Server for application 
server <- function(input, output) { 
    a(dat$flag, href=dat$infolink) 
    dat2 <- data.frame(dat[,c("country", "flag")]) 

    output$imagetest <- renderDataTable({ 
    return(dat2) 
    },escape = FALSE) 
} 

shinyApp(ui = ui, server = server) 

运行这个程序是产生两个警告:

Warning in if (!is.na(attribValue)) { : 
the condition has length > 1 and only the first element will be used Warning in charToRaw(enc2utf8(text)) : 
argument should be a character vector of length 1 all but the first element will be ignored 

据我所知,这些警告是由href在UI造成的,因为我分配超链接的vector大于1,并且它只分配所有图像的第一个超链接。这是下面给我一个应用程序仪表板:

enter image description here

然而,每一个标志是在矢量超链接到第一hyperlink,因为它是在警告提示。所以,如果我点击每个国旗,它会将我重定向到美国的维基百科页面,这是我的第一个hyperlink infolink vector中的我的数据data frame

我尝试了几件事来修复每个标志的警告和超链接到各自国家的维基百科页面,但没有任何工作。如果我错过了任何涵盖此内容的SO帖子,我会很高兴如果你将它重定向到它。无论如何,我会很感激任何指导来解决这个问题。

回答

1

你可以做这样的事情:

dat <- data.frame(
     country = c('USA', 'China', 'UK', "Germany", "France"),            
     flag = c('<img src="http://upload.wikimedia.org/wikipedia/en/thumb/a/a4/Flag_of_the_United_States.svg/200px-Flag_of_the_United_States.svg.png" width="100" height="80"></img>', '<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Flag_of_the_People%27s_Republic_of_China.svg/200px-Flag_of_the_People%27s_Republic_of_China.svg.png"width="100" height="80"></img>', '<img src="https://upload.wikimedia.org/wikipedia/en/a/ae/Flag_of_the_United_Kingdom.svg"width="100" height="80"></img>', '<img src="https://upload.wikimedia.org/wikipedia/en/b/ba/Flag_of_Germany.svg"width="100" height="80"></img>', '<img src="https://upload.wikimedia.org/wikipedia/en/c/c3/Flag_of_France.svg"width="100" height="80"></img>'),              
     infolink = c('https://en.wikipedia.org/wiki/United_States', 'https://en.wikipedia.org/wiki/China', 'https://en.wikipedia.org/wiki/United_Kingdom', 'https://en.wikipedia.org/wiki/Germany', 'https://en.wikipedia.org/wiki/France'), 
     stringsAsFactors = FALSE) 

    library(shiny) 
    # UI for application 
    ui <- fluidPage(

     # Application title 
     titlePanel("Image Hyperlinking Test"), 

     mainPanel(
     DT::dataTableOutput("imagetest")#, href=dat$infolink, target="_blank") 
    ) 
    ) 

    # Server for application 
    server <- function(input, output) { 
     hlink <- apply(dat,1, function(x){ 
     as.character(a(HTML(x[["flag"]]), href=x[["infolink"]], target="_blank")) 
     })  

     dat2$link <- hlink 
     output$imagetest <- DT::renderDataTable({ 
     DT::datatable(dat2, escape = FALSE) 
     }) 
    } 

    shinyApp(ui = ui, server = server) 
+0

曾经爱你的HLINK做了什么。我尝试过类似的事情,但没有成功获得语义上正确的HTML语法。在你的代码中,'dat2'需要'dat'才能使应用程序正常工作。另外,如果你可以简单地解释这个部分,它会很酷:'HTML(x [[“flag”]]),href = x [[“infolink”]]'。谢谢你,我正在接受答案。 – Santosh

+0

基本上'a'标签定义了一个超链接。所以'href'指定链接到的页面的URL是'x [[“infolink”]]'显示的参数。在我们的例子中,这是用于显示图像因此HTML功能的HTML。希望它是明确的。 – SBista

+0

明白了。当我编写类似的功能时,我没有使用HTML功能,结果它显示图像源而不是图像。再次感谢。 – Santosh