Odd ReporteRs issue with pipe %>% - r

I am having an odd issue with ReporteRs all of a sudden, even though I haven't updated or installed any new packages since the last time my code worked fine.
It seems to be related to the pipe operator. Here is an example:
This code returns this very general error, and I am sure it worked on the same table object a couple of weeks ago:
Indeed re-writing the code without pipes works:
I get the same issue with other commands too, such as addParagraph, addSlide, etc, but only when chained through the pipe operator.
Here is the session info. Has anybody come across this issue, and can suggest a solution?
Note my analysis work takes place in a safe haven environment that is not connected to the internet, hence not being able to have much control over what software versions are installed (and providing screenshot only).
EDIT: Code used:
paired_outcomes2 <- data.frame(sample = as.factor("Service"), percentage = as.character("50%"), count = as.integer(30))
# works
MyFTable <- paired_outcomes2
# The table
MyFTable <- FlexTable(MyFTable, add.rownames = FALSE, header.columns = F,
header.cell.props = cellProperties( background.color = "white" ),
header.text.props = textProperties( color = "black",
font.size = 11, font.weight = "bold" ),
body.text.props = textProperties( font.size = 10 , font.family="Calibri"))
# doesn't work
MyFTable <- paired_outcomes2 %>%
# The table
FlexTable( add.rownames = FALSE, header.columns = F,
header.cell.props = cellProperties( background.color = "white" ),
header.text.props = textProperties( color = "black",
font.size = 11, font.weight = "bold" ),
body.text.props = textProperties( font.size = 10 , font.family="Calibri"))

Ah I found the reason! The package 'sets' for some reason disrupts the ReporteRs commands that use pipes, so detaching it (I don't normally use it but had on this occasion) fixes the issue.

Related

How to use a newline in node label - forceNetwork R Shiny

I am trying to include the ability to have newlines in a node label, otherwise they extend out pretty far and just look messy in my R Shiny application. How can I do this? I have tried including the html break character and newline character but neither has worked. My network is created using the following code
forceNetwork(
Links = edges,
Nodes = nodes,
fontFamily = "Arial",
Nodesize = "size",
Source = "source_id",
Target = "target_id",
NodeID = "type_2",
Group = "type",
zoom = TRUE,
opacity = 1,
charge = -100,
linkColour = color_string_link,
opacityNoHover = ifelse(input$togglelabels == "labels",1,0),
colourScale = JS(color_string)
)

Why do some waffle chart glyphs work but not others?

I have installed fontawesome ttfs onto my local machine and loaded them into R. When I test this using fonts()[grep("Awesome", fonts())] it returns "FontAwesome" as expected.
I can run a waffle chart with certain glyphs... so far male, female, and briefcase have worked, but not others, such as "building."
Any idea what's going on?
library(ggplot2)
library(fontawesome)
library(extrafont)
library(waffle)
# This works fine
waffle(
c(`Poor=10` =10, `Average=18` = 18, `Excellent=7` =7), rows = 5, colors = c("#FD6F6F", "#93FB98", "#D5D9DD"),
use_glyph = "female", glyph_size = 12 ,title = 'Girls Performance', legend_pos="bottom"
)
# This does not work
waffle(
c(`Poor=10` =10, `Average=18` = 18, `Excellent=7` =7), rows = 5, colors = c("#FD6F6F", "#93FB98", "#D5D9DD"),
use_glyph = "building", glyph_size = 12 ,title = 'Girls Performance', legend_pos="bottom"
)
Not sure exactly why it is happening for some glyphs but not others, but I ran across the same issue recently. Found an excellent how-to here: https://www.listendata.com/2019/06/create-infographics-with-r.html
It sounds like you already have fonts available so could skip the first part of the how-to, and that you may just be missing packages. You'll want to add two others: echarts4r (on CRAN) and echarts4r.assets (on github only as of now)
require(devtools)
devtools::install_github("JohnCoene/echarts4r.assets")
library(tidyverse)
library(waffle)
library(extrafont)
library(echarts4r.assets)
library(echarts4r)
# Works for me with all of these packages loaded
waffle(
c(`Poor=10` =10, `Average=18` = 18, `Excellent=7` =7), rows = 5, colors = c("#FD6F6F", "#93FB98", "#D5D9DD"),
use_glyph = "building", glyph_size = 12 ,title = 'Girls Performance', legend_pos="bottom"
)

Is it possible to customize the size of rpivottable treemaps

This code works and the pivot table is drawn but the treemap is huge and I need it to render to about 50% of the current size, 500px would be fine for now. It seems to ignore the size settings. One pivot table overlaps the other when drawn.
output$pivot3 <- renderRpivotTable({
rpivotTable(data = dotsData,
rows="county",
cols="shortBreath",
width="500px",
height="500px",
aggregatorName = "Count",
aggregators = list(
Percentage = htmlwidgets::JS('$.pivotUtilities.aggregators["Count as Fraction of Columns"]'),
Count = htmlwidgets::JS('$.pivotUtilities.aggregators["Count"]')),
sorters="function(attr) {
var sortAs = $.pivotUtilities.sortAs;
if (attr == \"county\") {
return sortAs;
}
}")
})
I left a comment not that long ago of a method in which you could control the size by changing the JS. However, I have an R-based solution now.
I forked the Cran version of this package and modified it. My version of this package does allow you to control the size of the Treemap.
To install my modified packaged, use the following:
devtools::install_github("fraupflaume/rpivotTable")
You'll call the library the same way you did with the Cran version. To modify the treemap, you need to know that this is a D3 graph. All of the other graphs in this package are C3.
The following example reflects how you would indicate a specific size for either D3 or C3. (I've included several controllable options for C3 graphs, as well.)
library(rpivotTable)
rpivotTable(mtcars, rows = c("mpg", "am"), cols = "cyl",
width = "90%", height = "40%",
rendererOptions = list(
c3 = list(legend = list(show = FALSE),
data = list(labels = TRUE),
options = list(responsive = TRUE,
maintainAspectRatio = FALSE),
size = list(width = "600",
height = "500")),
d3 = list(size = list(width = "500", height = "500"))))
I expanded the viewer pane so that you could see it no longer fills the viewer.
This is the same view, but with the D3 controls removed.

Errors when making tables with the flextable package

So before with reporters i made a matrix consisting purely of text and then made it into a flextable and at last added it to my Word document. That way i created a header for my document. Example:
Header <- matrix("",1,3)
Header[1] <- paste("bla bla")
Header[2] <- paste("blu blu")
Header[3] <- paste("ble ble")
myheader <- FlexTable(data = Header, header.columns = F,
add.rownames = F,
header.cell.props = cellProperties( background.color = "white" ),
header.text.props = textProperties( color = "black",
font.size = 11),
body.text.props = textProperties( font.size = 11,
font.weight = "bold"))
Now when i try to convert it into a flextable with the new function:
myheader <- flextable(Header)
It says
" Error in flextable(Header) : is.data.frame(data) is not TRUE"
or by:
myheader <- flextable(Header, col.keys = names(Header))
It says
" Error in flextable(Header, col.keys = names(Header)) :
unused argument (col.keys = names(Header)"
What am i doing wrong?
Thank you in advance!
Ps. Feel free to modify my visual presentation (im still trying to figure it out on here).
Also i just noticed, that i get the same problem with data tables, so theres definitely something i've missed.
Your Header object is a matrix:
class(Header)
[1] "matrix"
You can convert it to a data.frame using as.data.frame()
library(flextable)
flextable(as.data.frame(Header))
Alternatively, you could use tableHTML which also accepts matrices:
library(tableHTML)
Header %>%
tableHTML(rownames = FALSE,
theme = "scientific")
The result here is:

Indent a Flextable made with R-ReporteRs

I want to indent a (flex)table created with the ReporterRs package. Here's an example:
library(ReporteRs)
df <- data.frame(Column1 = 1:5, Column2 = c("a","b","c","d","e"))
Mydoc = docx(title="A testdoc for testing tables")
options('ReporteRs-fontsize'=10, 'ReporteRs-default-font'='Arial')
FTab = FlexTable( data = df, add.rownames = FALSE, header.columns = TRUE,
body.text.props = textProperties( font.size = 10,font.family = "Arial" ),
header.text.props = textProperties( font.size = 10,font.family = "Arial", font.weight = "bold"))
FTab = setFlexTableBorders(FTab,inner.vertical = borderNone(),inner.horizontal = borderNone(),
outer.vertical = borderNone(),
outer.horizontal = borderProperties(color = "black", style = "solid", width = 1))
Mydoc = addFlexTable(Mydoc, FTab)
nu <- format(Sys.time(), "%Y%m%d%H%M")
writeDoc(Mydoc, paste0("testreport_",nu,".docx"))
This creates a docx with a left aligned table. I want the table to move 1.5 cm to the right. So no center or right alignment, but an indentation of 1.5 cm. Is this possible? For text, I can use a pre-defined style that indents 1.5 cm, but for tables that doesn't seem possible. Or is it?
As a workaround, I could add an extra column at the left, without any borders or text. But I prefer a neat solution.
From the package documentation, unless you want to write a patch, it seems you'd better go with the invisible column:
Function addFlexTable
add FlexTable to document objects.
Word and html document
Add a FlexTable object in a document object with the function addFlexTable.
Expected arguments are:
the document object
the FlexTable object
eventually a parProperties object to define alignement. Note that with docx objects, only alignment will be used, if you’d like to add space around a table, specify padding on preceding and or following paragraph.

Resources