R: Insert table in template table using ReporteRs - r

I am in the process of making a dashboard. Basically, I am pulling out selected information from a database that I then want to display in a word template. I set up the template with a table (2 rows, 3 columns). In each cell I put a bookmark. Then, using the R package ReporteRs I put specific information from R into a cell within the template based on the bookmark.
ISSUE: I cant seem to insert tables into a cell of the template.
Tried: I thought it might be due to size of the table (as i experienced that size was the issue when inserting figures), but this is not the case.
Below is an example with the error. In order to run this you have to create a Word document with a table with a bookmark in one of the cells called 'test'.
doc = docx( title = "Dashboard",template="H:\\test.docx")
myt <-FlexTable(cars[c(1:10),])
doc <-addFlexTable(doc,myt,bookmark="test")
writeDoc(doc, "H:\\testresult.docx")
If you then create a bookmark outside of the table and assign the table to the new bookmark, it seems to work.
Does anyone know how to insert a table (data frame as a table) into any cell of my template?

If argument bookmark is used, content (plots, paragraphs or images) will replace the whole paragraph containing the bookmark.
Behavior is different for tables: tables are added after the paragraph that contains the bookmark. A workaround is to add a paragraph after the bookmarked paragraph in the cell of the template. Then use deleteBookmark to delete the paragraph containing the bookmark.
doc <- docx( title = "Dashboard",template="H:\\test.docx")
myt <- FlexTable(cars[c(1:10),])
# make sure there is one new paragraph after the paragraph that contains 'test'
# add the FlexTable just after the paragraph containing bookmark 'test'
doc <- addFlexTable(doc,myt,bookmark="test")
# then delete the paragraph containing bookmark 'test'
deleteBookmark(doc, bookmark= "test")
writeDoc(doc, "H:\\testresult.docx")
You may use Sections with columns instead (and eventually column breaks):
doc = docx( )
doc = addSection(doc, landscape = TRUE, ncol = 2 )
doc = addPlot( doc = doc, fun = function() {
barplot( 1:8, col = 1:8 )
}, width = 3, height = 3, pointsize = 5)
doc = addColumnBreak(doc )
doc = addFlexTable(doc, FlexTable(head(iris) ) )

Related

Save gt table as html w/o extra line return

I have a simple table with a link in it.
I am using gt because it does some formatting nicely, which is not shown, because it is not relevant to my question.
I wish to save the gt table as html with no tag so that it can be inserted into another html page.
Here is the gt_tbl in RStudio's Viewer. Great!
And here is the raw_html_tbl as viewed in a web browser saved using either the writeLines method or the cat method as shown in the script.
The HTML table in the browser seems to have added a line return, hence the need for the scroll bar.
How can I save the raw html file so that the scroll bar does not appear and the height of "This is a link" remains one line?
library(gt)
library(tibble)
df <- tibble(
note = c("This is a <a href = https://www.cnn.com>link</a>.")
)
gt_tbl <- gt(df) |>
tab_options(
table.width = 450
) |>
fmt_markdown(columns = note)
gt_tbl
raw_html_tbl <- gt_tbl |>
as_raw_html()
writeLines(text = raw_html_tbl, con = "test/test_tbl.html")
cat(raw_html_tbl, file = "test/test_tbl.html")

Select input from a list in Shiny

I am new to Shiny R. Based on a Table Editor, I would like to customise certain functions but I am having some issues. There is an Add button that contains several fields. I would not like to write every time some fields to fill the form, e.g., the Supplier or product names. I tried using SelectInput from the csv file created by the app, but when clicking the add row button, there is a drop list of the Suppliers but I can't write any other name that is not in that list. I read that another option would be textInput.typeahead but I can't figure out how to make it work.
Here is the piece of code I am using:
mydata1 = read.csv("Supplier_NCR1.csv", row.names = NULL, na.strings = "", stringsAsFactors = FALSE)
mylist1 <- as.list(unique(mydata1[[4]]))
observeEvent(input$Add_row_head, {
### This is the pop up board for input a new row
showModal(modalDialog(title = "Add a new row",
dateInput(paste0("Date_add", input$Add_row_head), "Date:", value = Sys.Date()),
numericInput(paste0("ReportNo_add", input$Add_row_head), "Report Number:",0),
selectInput(paste0("Supplier_add", input$Add_row_head), "Supplier Name:",choices=c("",mylist1)),
'''
Also, I do not know if it possible to read that field from the datatable itself instead of the csv file. Thank you in advance and hope to find a solution.

OfficeR: How to add text in word document template on a placeholder

I want to generate a word document from a pre saved word template, I have added placeholders on word template, but I'm not able to save the texts on placeholder location. I tried using ph_with function but it gives error as its for .pptx, please tell me which function to use for saving the desired text to a specific placeholder location. Also I'm adding the placeholders in word template by typing (Placeholder1 ...and so on) is this correct method?
template <- system.file(package = "officer", "doc_examples", "protocol_template.docx")
doc <- read_docx(path = template)
ph_with(doc,
value = input$text,
location = ph_location_label(ph_label = "Placeholder1"))
After loading a word document you can use the body_replace_all_text function to pipe in the desired text into the document.
body_replace_all_text(
x,
old_value,
new_value,
only_at_cursor = FALSE,
warn = TRUE,
...
)

Creating space under/over paragraphs, tables etc

I cant figure out from the officer manual or function reference, if it is possible to create space between objects in a word document? By space i mean a function that would equal pressing "enter" while writing.
Thank you in advance!
body_add_par(value="")
should do the job.
eg.
myDoc <- myDoc %>%
body_add_par(value = "example1")
body_add_par(value = "")
body_add_par(value = "example1")
You can use this to create spaces after tables and other objects.
from the documentation using /r/n will do the work . Example
ftext(", \r\nhow are you?", prop = bold_face ) )

How can I add a table to the header of a Word document using the officer package in R?

I'm trying to switch from ReporteRs to officer. When using ReporteRs, I was able to add a FlexTable to the header by using a bookmark that I set up in a Word document template (I placed a bookmark in the header of the template called "HEAD"):
library(ReporteRs)
library(dplyr)
doc <- docx(template = "Template.docx")
ft1 <- FlexTable(mtcars)
addFlexTable(doc,
ft1,
bookmark = "HEAD")
writeDoc(doc, file = "test.docx")
Presumably, it might be possible do the same thing using officer with the flextable package, and it might look something like this:
library(officer)
library(flextable)
library(dplyr)
doc <- docx(template = "Template.docx")
ft1 <- flextable(mtcars)
doc <- cursor_bookmark(doc,"HEAD") %>% body_add_flextable(ft)
print(doc,target="test.docx")
If I have a bookmark named "HEAD" in the body of the document, this works, but if I have a bookmark named "HEAD" in the header of the Word document, it says Error: cannot find bookmark "HEAD".
I know there are specific functions to add text to the header, but these don't have the ability to add tables. The closest I can find is:
doc <- docx(template = "Template.docx")
ft1 <- flextable(mtcars)
doc <- headers_replace_text_at_bkm(doc,"HEAD",ft)
print(doc,target="test.docx")
But this returns the error Error in headers_replace_text_at_bkm(doc, "HEAD", ft) : is_scalar_character(value) is not TRUE.
Is there any way to add a table to the header using officer and flextable?

Resources