Reputation: 427
I have an HTML output file and there is a column named "Description" in this file. I want to link locally saved text files to some of the entries of this column when a value is Report data does not match
.
Snapshot the HTML file is following:
So, there are dedicated texts files to row no: 12, 16, 17, 18, 19, 20
, which I want to link them to the Description
column.
Line of Codes to generating HTML file is:
library(xtable)
extract1 <- result[,list(TestCaseID, breadcrumb, Discription),]
print(xtable(extract1), type = "html", file = "extracted.html")
How to do the linking of text files. Please let me know if any modification required in the question. Thanks in Advance!!!
Upvotes: 1
Views: 573
Reputation: 7260
I recommend that you perform a pre-processing according to your requirements. Because the names of the text files may change later, they should be provided as a separate column.
If the text file link is not required think of a conditional processing for NA's later.
The sample below is based on a main list. Text files reside in a subfolder.
The trick is the use of HTML tag href
and use of the sanitize.text.function
as shown below for your test cases.
You'll need to create some dummy text files like gauge-D00.txt, gauge-D01.txt, etc. in a subfolder to try the example.
# --------------------------------------------------------
# gauge main ID list
#---------------------------------------------------------
# ID,location,description,textfile
# D00,nature reserve,Otternhagener Moor,../gauge-D00.txt
# D01,nature reserve,Helstorfer Moor,../gauge-D01.txt
# FER,benchmark,Negenborner Weg,../gauge-FER.txt
#----------------------------------------------------------
# text files reside in /data-develop-text-file-link/
# ---------------------------------------------------------
library (xtable)
gaugelist <- structure(list(
ID = structure(1:3, .Label = c("D00", "D01", "FER"), class = "factor"),
location = structure(c(2L, 2L, 1L), .Label = c("benchmark", "nature reserve"), class = "factor"),
description = structure(c(3L, 1L, 2L), .Label = c("Helstorf", "Negenborn", "Otternhagen"), class = "factor"),
textfile = structure(c(2L, 3L, 1L), .Label = c("../gauge-FER.txt", "../gauge-D00.txt", "../gauge-D01.txt"), class = "factor")),
class = "data.frame", row.names = c(NA, -3L))
head(gaugelist)
# set HTML tag for linking to local file --------------------------------------------
gaugelist$description <- paste("<a href=", gaugelist$textfile, ">", gaugelist$description, "</a>")
head(gaugelist)
# remove textfile column from data.frame --------------
gaugelist$textfile <- NULL
head(gaugelist)
# print HTML table and sanitize by using your own function (add subfolder) ---------------------------------------
print(xtable(gaugelist), type = "html",
sanitize.text.function = function(str) gsub("..", "./data-develop-text-file-link", str, fixed = TRUE),
file = "gauge-list.html")
Edit:
It is slightly better to reference the current directory ./data-develop-text-file-link
with ./
. I edited this for gsub
handling but this makes no difference.
The structure of HTML and text files described in my answer above and only exemplarily hinted at as a thought is based on a website structure. The HTML table is located at any root node and the text files are located in a directory below it. So there is later the possibility to upload the file to a server or to leave it locally on the PC.
That's why I worked with relative links, which work for me in all browsers.
Please note that absolute paths to text files seem to be a problem with Microsoft Edge and Internet Explorer. TEST: Copy the link with the right mouse button and paste it into Edge's address text box and the text file will open. I couldn't find any problems with Firefox and Chrome when testing with e.g. C:\Users\%USERNAME%\Documents or D:_working\ e.g.:
# print HTML table and sanitize by using your own function (add subfolder) ---------------------------------------
print(xtable(gaugelist), type = "html",
sanitize.text.function = function(str) gsub("..", "file:///C:/Users/webma/Documents/data-develop-text-file-link", str, fixed = TRUE),
file = "gauge-list.html")
Upvotes: 1