How do I add a hyperlink to the text in a cell of a flextable? The hyperlink should be underlined and open in a new tab.
In the R script below, This is a link to google. should be the hyperlink.
I included the hyperlink_text example from the documentation which results in an error for me.
library(tidyverse)
library(flextable)
df <- tibble(
desc = c("R1", "R2", "R3"),
A = c("This is a long sentence that should go over into B's column. This is a link to google.", "just one cell in colA", "just one cell in colA"),
B = c("", "just one cell in colB", "just one cell in colB"),
C = c("just one cell in colC", "just one cell in colC", "just one cell in colC")
)
flextable(df) |>
merge_at(i = 1, j = 2:3)
# hyperlink_text p.103
# From documentation: https://cran.r-project.org/web/packages/flextable/flextable.pdf
dat <- data.frame(
col = "Google it",
href = "https://www.google.fr/search?source=hp&q=flextable+R+package",
stringsAsFactors = FALSE)
ftab <- flextable(dat)
ftab <- compose( x = ftab, j = "col",
value = as_paragraph(
"This is a link: ",
hyperlink_text(x = col, url = href ) ) )
# Error in data.frame(x = x, url = url, stringsAsFactors = FALSE) :
# object 'href' not found
Related
I have a data frame that stores names of saved data frame objects in my R session:
col1 <- c("what is this", "how are you", "hello im good")
df <- data.frame(col1)
The what is this object looks like this:
title <- c("this is a cat", "this is a dog", "this is a dog")
body <- c("the cat is very cute", "who cares", "here is a bone")
`what is this` <- data.frame(title, body)
I want to be able to loop through all three object names in the df data frame using the textnets package and stack the text_centrality results on top of each other. I wrote the following code:
for (row in 1:nrow(df)) {
prepped_header <- PrepText(df[row, "col1"], groupvar = "title", textvar = "body", node_type = "groups", tokenizer = "words", pos = "nouns", remove_stop_words = TRUE, compound_nouns = TRUE)
header_text_network <- CreateTextnet(prepped_header)
text_centrality <- TextCentrality(header_text_network)
}
I am getting the following error message:
Error in PrepText(df[row, "col1"], groupvar = "title", textvar = "body", :
unused arguments (groupvar = "title", textvar = "body", node_type = "groups", tokenizer = "words", pos = "nouns", remove_stop_words = TRUE, compound_nouns = TRUE)
Any guidance would be appreciated!
I am trying to add a column with images to the table :
In this example, I use the same image in each row, but my actual data has a different image.
library(flextable)
library(officer)
img.file <- file.path( R.home("doc"), "html", "logo.jpg" )
data = iris %>% mutate(path= img.file)
myft <- flextable( head(data))
I am following examples listed in fleaxtable table ,
myft <- compose( myft, j = "path",
value = as_paragraph(
as_image(src = path, width = .20, height = .15),
" blah blah ",
as_chunk(Sepal.Length, props = fp_text(color = "red"))
),
part = "body")
when I run it, I get error :
I try to read the documentation, but I can't find any explanation of what to do to fix the error. In the code I provided, it actually adds an image and text "blah blah" to the cell, but all I want is to render a path as an image. I tried to shorten the code to the following:
myft <- compose( myft, j = "path",
value = as_paragraph(
as_image(src = path, width = .20, height = .15))
),
part = "body")
But it did not work.
Really appreciate your help!
Used a wrong function! The colformat_image() do teh trick
myft <- colformat_image( myft, j = "path", width = .20, height = .15)
I want to add several placeholders to a Power Point slide, in different (fixed) positions, using a predefined template of my company. I use that template only to get the font style, the header and footer of the slides,but not for the content of each slide. For this, I can add text and images wherever I want.
Then I need to add, in each of these placeholders, some text paragraphs.
I can do this, for example, with the code below. But the bad thing is that if I want to add a paragraph to the first placeholder, I have to specify the id_chr of the placeholder (in this example are "2" and "3") which is known only after I add the empty placeholders in the presentation, at runtime.
Is there a way to add an empty placeholder specifying also an ID that can be used later to add content to it using that ID instead of the id_chr that is known only at runtime? Maybe using also ph_label?
require(magrittr)
require(officer)
TARGET_FILE = "ph_add_fpar.pptx"
setwd(file.path("E:", "Work", "Reporting"))
file.remove(TARGET_FILE)
MASTER_LAYOUT = "Blank"
PICTURE_AND_TEXT_PAGE_LAYOUT = "Internal Slide - Picture and Text"
bold_face <- shortcuts$fp_bold(font.size = 30)
bold_redface <- update(bold_face, color = "red")
fpar_1 <- fpar(ftext("Hello ", prop = bold_face),
ftext("World", prop = bold_redface ),
ftext(", \r\nhow are you?", prop = bold_face ) )
fpar_2 <- fpar(ftext("Hello ", prop = bold_face),
ftext("World", prop = bold_redface ),
ftext(", \r\nhow are you again?", prop = bold_face ) )
doc <- read_pptx(path = file.path(getwd(), "Template.pptx")) %>%
add_slide(layout = PICTURE_AND_TEXT_PAGE_LAYOUT, master = MASTER_LAYOUT) %>%
ph_empty_at(left = 1,top = 1,width = 3,height = 2,template_type = "body",template_index = 4) %>%
ph_empty_at(left = 4,top = 1,width = 3,height = 2,template_type = "body",template_index = 4) %>%
ph_add_fpar(value = fpar_1, type = "body", level = 1, id_chr = "2") %>%
ph_add_fpar(value = fpar_2, type = "body", level = 1, id_chr = "3")
print(doc, target = TARGET_FILE)
system("cmd.exe", input = TARGET_FILE)
Thank you
I am working with the timevis library in R. I want a gannt diagram with one line per item. The following example will stack items after another if there is room. Is there a way to force the diagram to have as many rows as there are items?
library(timevis)
data <- data.frame(
id = 1:4,
content = c("Item one" , "Item two" ,"Ranged item", "Item four"),
start = c("2016-01-10", "2016-01-11", "2016-01-20", "2016-02-14"),
end = c(NA , NA, "2016-02-04", NA)
)
timevis(data)
This gives the following output from timevis:
But I want each of the items on a separate line
I am aware of other packages than timevis, but I would prefer using timvis as the interactivity is very usefull for what I am trying to vizualise.
From the examples, slightly modified:
timevis(data = data.frame(
start = c(Sys.Date(), Sys.Date()+1, Sys.Date() + 2, Sys.Date() + 3),
content = c("one", "two", "three", "four"),
group = c(1, 2, 3, 4)),
groups = data.frame(id = 1:4, content = c("G1", "G2", "G3", "G4"))
)
This should be a good starting point.
You need the option: stack = TRUE for this, this puts them on different lines inside a group, example final call below
tv <<- timevis(main,group, showZoom = FALSE,options = list(
editable = TRUE,stack = TRUE, showCurrentTime = FALSE,multiselect = TRUE,align = "center"))
How can you add a tool tip to column header in a data table display
output$table <- renderDataTable({
df <- iris
colnames(df) <- sapply(names(df), function(x) abbreviate(x))
df
})
The actual data frame i'm using (which I can't show) has very long names and there are >20 columns. So abbreviating is necessary to show the entire data frame with minimal horizontal scrolling. I'm looking for a way to add a hover over tool tip that shows the full, un-abbreviated name of each column when you hover over an abbreviated column header.
dat <- iris[1:3,]
names(dat) <- c(
"A long name",
"Another long name",
"Yet another long name",
"This name is long as well",
"This one is not short"
)
headerCallback <- c(
"function(thead, data, start, end, display){",
" var ncols = data[0].length;",
sprintf(" var shortnames = [%s]",
paste0(paste0("'",abbreviate(names(dat)),"'"), collapse = ",")),
sprintf(" var tooltips = [%s];",
paste0(paste0("'",names(dat),"'"), collapse = ",")),
" for(var i=0; i<ncols; i++){",
" $('th:eq('+i+')',thead).attr('title', tooltips[i]).text(shortnames[i]);",
" }",
"}"
)
datatable(dat, rownames = FALSE,
options = list(
headerCallback = JS(headerCallback)
)
)