I'm using flextable and officer to write a table with hyperlinked cells to a powerpoint slide, but I can't figure out how to remove the underline in the URL. Here's my code:
library(officer)
library(flextable)
library(magrittr)
# Make the table
no_ul <- fp_text(underlined = FALSE)
ft <- data.frame(x = "Hello", link = "www.google.com") %>%
flextable(col_keys = "x") %>%
compose(j = 1,
value = as_paragraph(hyperlink_text(x = x, url = link, props = no_ul))
)
# Add to a slide
my_pres <- read_pptx() %>%
add_slide() %>%
ph_with(value = ft, location = ph_location_type(type = "body"))
print(my_pres, "~/Desktop/flex_example.pptx")
When I open up "flex_example.pptx" this is the table:
How do I remove the underline of the hyperlink???? I was hoping underlined = FALSE would do it, but no luck!
THANK YOU IN ADVANCE!!!!
Related
I can print my data with gt table and create hyperlinks for different cells, as shown below by knitting an HTML file:
library(dplyr)
library(gt)
raw_dat <- mtcars[1:15, ] %>% rownames_to_column(var = "id") %>% select(id, mpg)
df <- tibble(
name = raw_dat$id,
link = 'https://www.cars.com//')
df %>%
mutate(link = sprintf('<p>%s', link, raw_dat$mpg),
link = map(link, gt::html)) %>%
gt
I want to print the same output to a powerpoint slide and preserve the links. I understand that I cannot use 'ph_with' with an object of class "c('gt_tbl', 'list')". So I was wondering what else I can do to print a table with hyperlinks in powerpoint.
NEW <- read_pptx("Presentation1.pptx") %>%
add_slide(., layout = "Blank", master = "Office Theme") %>%
ph_with(raw_dat,
location = ph_location(left = 0, top = 0, width = 13.33, height = 7.5))
This is a solution with flextable that is supported by officer.
library(tibble)
library(dplyr)
library(flextable)
library(officer)
raw_dat <- mtcars[1:15, ] %>% rownames_to_column(var = "id") %>% select(id, mpg)
df <- tibble(name = raw_dat$id, link = 'https://www.cars.com//')
ft <- flextable(df) %>%
mk_par(j = "link", value = as_paragraph(hyperlink_text(x = name, url = link))) %>%
autofit()
read_pptx() %>%
add_slide() %>%
ph_with(ft, location = ph_location_type()) %>%
print(target = "toto.pptx")
PS: I am not sure that I answer the question as it is generated with officer, not officedown, but your example is not a R Markdown example. I assumed you wanted to use officer.
I have the following table generated in RMarkdown using the kableExtra package. I'm trying to bold selected words in a cell (the word First in my example below) and force a linebreak between two words in a cell (First and Message), however this doesn't seem to work. Any ideas on how to do this?
library(kableExtra)
library(tidyverse)
first <- c('\\textbf{First} Message','\\textbf{First}\n Message','First Message')
second <- c('Second Message','Second Message','Second Message')
third <- c('Third Message','Third Message','Third Message')
data.frame(first,second,third) %>%
kable(format='latex',caption="Caption",
col.names = c('First',"Second","Third"), booktabs = T, escape = FALSE) %>%
kable_styling(latex_options = c("HOLD_position"), font_size = 7) %>%
row_spec(0,bold=T,color = 'white', background = '#7c3042')
You need to add mutate_all(linebreak) %>% to your code.
Check the documentation here (page 26): https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf
Modifying your code:
library(kableExtra)
library(tidyverse)
first <- c('\\textbf{First} Message','\\textbf{First}\n Message','First Message')
second <- c('Second Message','Second Message','Second Message')
third <- c('Third Message','Third Message','Third Message')
data.frame(first,second,third) %>%
mutate_all(linebreak) %>%
kable(format='latex',caption="Caption",
col.names = c('First',"Second","Third"), booktabs = T, escape = FALSE) %>%
kable_styling(latex_options = c("HOLD_position"), font_size = 7) %>%
row_spec(0,bold=T,color = 'white', background = '#7c3042')
Result:
I want to create on the same word landscape document both a table and text formatted in column.
The problem is that whenever I add body_end_section_columns_landscape() it creates a new page.
Example of working code in portrait format:
library(officer)
library(flextable)
ft <- qflextable(head(iris))
read_docx() %>%
body_add_flextable(value = ft ) %>%
body_end_section_continuous() %>%
body_add_par(value = paste(rep(letters,50), collapse = ' ')) %>%
body_end_section_columns() %>%
print(target = "test.docx")
If I try to create similar in landscape
ft <- qflextable(head(iris))
read_docx() %>%
body_add_flextable(value = ft ) %>%
body_end_section_landscape() %>%
body_add_par(value = paste(rep(letters,50), collapse = ' ')) %>%
body_end_section_columns_landscape() %>%
print(target = "test.docx")
It adds a second page for the text.
Is there a possibility to have both on same page as landscape same as in the portrait one?
Thank you
Yes, functions body_end_section_* are adding a break page between sections. You need to add specific section settings (type = "continuous") and use body_end_block_section() to achieve what you want to do:
library(officer)
library(magrittr)
library(flextable)
landscape_one_column <- block_section(
prop_section(
page_size = page_size(orient = "landscape"), type = "continuous"
)
)
landscape_two_columns <- block_section(
prop_section(
page_size = page_size(orient = "landscape"), type = "continuous",
section_columns = section_columns(widths = c(4, 4))
)
)
ft <- qflextable(head(iris))
read_docx() %>%
# there starts section with landscape_one_column
body_add_flextable(value = ft) %>%
body_end_block_section(value = landscape_one_column) %>% # there stops section with landscape_one_column
# there starts section with landscape_two_columns
body_add_par(value = paste(rep(letters, 50), collapse = " ")) %>%
body_end_block_section(value = landscape_two_columns) %>% # there stops section with landscape_two_columns
print(target = "test.docx")
I have a zip file which has multiple images of 2 types.1 -FrequencyRose images 2- EnergyRose images. I have created a flextable and then replacing the even rows with images and odd rows with the image titles using for loop . The loop is correctly displaying the titles but its only printing the last read image of each type mulitple times instead of actually printing all the images as per loop count.
img.file <- unzip("D:\\Mast_Image Files.zip")
fr_files <- img.file[grepl(paste("FrequencyRose", collapse = "|"), img.file)]
er_files <- img.file[grepl(paste("EnergyRose", collapse = "|"), img.file)]
fr_files has 3 image file paths and same with er_files
click
num_masts = length(img.file)
c1 = rep("Freq_rose",num_masts)
c2 = rep("Energy_Rose",num_masts)
df = data.frame(c1,c2)
dfft = flextable(df)
sso=seq(1,num_masts,2)
sse=seq(2,num_masts,2)
for (g in 1:(num_masts/2)){
ff.img = fr_files[g]
ef.img = er_files[g]
dfft2 = dfft %>%
display(
i = sse[g], col_key = "c1", pattern = "{{img}}",
formatters = list( img ~ as_image(c1,
src = ff.img, width = 3, height = 3))) %>%
display(
i = sse[g], col_key = "c2", pattern = "{{img}}",
formatters = list( img ~ as_image(c2,
src = ef.img, width = 3, height = 3))) %>%
display(
i = sso[g], col_key = "c1", pattern = paste("Freq_Rose","mast",g)) %>%
display(
i = sso[g], col_key = "c2", pattern = paste("Energy Rose","mast",g))
}
this loop is able to produce titles correctly but the only the fr_files[3], er_files[3] is looping over all the even rows of corresponding columns. output is as :final results . could not find the issue.
I can't reproduce your example (I don't have the images).
library(ggplot2)
library(tidyverse)
library(flextable)
# a data example ----
zz <- iris %>%
group_by(Species) %>%
summarise( n = n(), avg = mean(Petal.Length),
img_path = paste0( unique(as.character(Species)), ".png"),
gg = list(ggplot(tibble(Sepal.Length, Petal.Width), aes(Sepal.Length, Petal.Width)) + geom_point() + theme_minimal())
)
zz
# create the png ----
walk2(zz$gg, zz$img_path, function(gg, path){
png(filename = path, width = 300, height = 300)
print(gg)
dev.off()
})
From there you have everything you need and the flextable commands could be:
# create the flextable -----
flextable(zz, col_keys = c("Species", "n", "avg", "plot")) %>%
# here, i is selecting only odd rows
compose(i = ~ seq_along(Species) %% 2 > 0, j = "plot", value = as_paragraph(as_image(img_path, width = 300/72, height = 300/72))) %>%
theme_box() %>%
autofit()
I am trying to make a powerpoint file with officer that includes tables with hyperlinks in their cells, but I can't find a way how to do it.
E.g. a row in a 2 column table on a slide could include 'Ensembl' in the 1st column, the 2nd column would say 'ENSG00000165025' and clicking on it would open up the browser at 'uswest.ensembl.org/Homo_sapiens/Gene/Summary?g=ENSG00000165025'. Some values in the 2nd column could be just plain text.
Is this possible to achieve?
With the new version on github, you will be able to include hyperlinks as demo below:
library(flextable)
dat <- data.frame(
col = "CRAN website", href = "https://cran.r-project.org",
stringsAsFactors = FALSE)
ft <- flextable(dat)
ft <- display(
ft, col_key = "col", pattern = "# {{mylink}}",
formatters = list(mylink ~ hyperlinked_text(href, col) )
)
ft
`
dat <- data.frame(
col = "entrez",
href = "https://www.ncbi.nlm.nih.gov/gene?cmd=Retrieve&dopt=full_report&list_uids=6850",
stringsAsFactors = FALSE)
ft <- flextable(dat)
ft <- display(
ft, col_key = "col", pattern = "# {{mylink}}",
formatters = list(mylink ~ hyperlink_text(href, col) )
)
ft # works fine
doc <- read_pptx() %>%
add_slide(layout = 'Title and Content', 'Office Theme') %>%
ph_with_flextable(ft) # error
Error in doc_parse_raw(x, encoding = encoding, base_url = base_url,
as_html = as_html, : EntityRef: expecting ';' [23]
repeat with:
dat <- data.frame(
col = "entrez", href = URLencode("https://www.ncbi.nlm.nih.gov/gene?cmd=Retrieve&dopt=full_report&list_uids=6850", reserved = TRUE),
stringsAsFactors = FALSE)
ft <- flextable(dat)
ft <- display(
ft, col_key = "col", pattern = "# {{mylink}}",
formatters = list(mylink ~ hyperlink_text(href, col) )
)
ft # clicking the link in rstudio fails
doc <- read_pptx() %>%
add_slide(layout = 'Title and Content', 'Office Theme') %>%
ph_with_flextable(ft) # fine, no error message, but error message when opening pp file