dfSummary() graphs are not printed in the HTML file - r

When using dfSummary() from the "summarytools" package in Rmarkdown -file, I get Graph -part of the summary printed as plain ASCII despite the st_options(plain.ascii = FALSE). The correct graphs are printed in the /tmp -folder, but are not displayed in the html-file.
{r Summary, results='asis', echo=FALSE}
st_options(bootstrap.css = FALSE,
plain.ascii = FALSE,
style = "rmarkdown",
dfSummary.silent = TRUE)
st_css()
dfSummary(df_data, tmp.img.dir = "/tmp", valid.col = FALSE, graph.magnif = 0.75)
Summary from the code above gets printed like this:
How can I get the proper graphs (which are in the tmp-folder nice and shiny) included in the HTML file?

How can I get the proper graphs (which are in the tmp-folder nice and shiny) included in the html file?
According to the Dominic's vignette - "Recommendations for Using summarytools With Rmarkdown":
For dfSummary(), grid is recommended.
So, you may try following, using print() function:
```{r Summary, results = "asis", cache = FALSE}
base::print(summarytools::dfSummary(df_data,
valid.col = FALSE, # drop Valid column if redundant
style = "grid", # set style to “grid”
plain.ascii = FALSE,
graph.magnif = 0.75, # zoom factor (max = 1) for bar plots and histograms
tmp.img.dir = "./tmp"),
dfSummary.silent = TRUE, # Suppresses messages about temporary files
bootstrap.css = FALSE)
```
Or, if you prefer to declare st_options() first:
```{r Summary2, results = 'asis', echo = FALSE}
st_options(bootstrap.css = FALSE,
dfSummary.silent = TRUE)
st_css()
dfSummary(df_data,
valid.col = FALSE,
style = "grid",
plain.ascii = FALSE,
graph.magnif = 0.75,
tmp.img.dir = "./tmp")
```
Let us know if this is helpful.

Just add style="grid":
dfSummary(df_data, style = "grid", tmp.img.dir = "/tmp",
valid.col = FALSE, graph.magnif = 0.75)
The documentation is not clear enough on this point, it'll be fixed in the next version.

Related

R: put the table from "htmlTable" in the middle of the page (html report)

I'm struggling to find a way to place my table in the middle of the page in my R markdown using "getDescriptionStatsBy" and "htmlTable" functions
library(Gmisc);library(htmlTable)
tables%>%
Gmisc::getDescriptionStatsBy(Age,sexe,by=Group,
statistics = list(continuous = getPvalAnova,
factor = getPvalChiSq,
proportion = getPvalFisher),
factor_fn = describeFactors,header_count = "n=%s",show_all_values=T,html = T)%>%
htmlTable::addHtmlTableStyle(align='c')%>%
htmlTable::htmlTable(caption = "Demographic Table")
html report preview:
I tried with the argument "align='c'" in addHtmlTableStyle function, but it doesn't work, the table is still in left side of the htlm report.. Any ideas ?! thanks
***** ANSWER *****
I found a solution :
I just needed to add before the chunk <div align="center"> and </div> after
<div align="center">
```{r, echo = FALSE, fig.align = 'center', fig.width = 6, fig.height = 4, message = FALSE, warning = FALSE}
tables%>%
Gmisc::getDescriptionStatsBy(Age,sexe,by=Group,
statistics = list(continuous = getPvalAnova,
factor = getPvalChiSq,
proportion = getPvalFisher),
factor_fn = describeFactors,header_count = "n=%s",show_all_values=T,html = T)%>%
htmlTable::addHtmlTableStyle(align='c')%>%
htmlTable::htmlTable(caption = "Demographic Table")
```
</div>

Unwanted bars in nodes of manifest variables by semPlot::semPaths

I am using the following code semPlot::semPaths to plot a lavaan model (the complete reproducible code is in this Gist).
semPaths(f,
title = FALSE,
curvePivot = TRUE,
what = "std",
rotation = 2,
layout = "tree2",
optimizeLatRes = TRUE,
intercepts = FALSE,
edge.label.cex = 0.95,
exoVar=FALSE,
sizeMan=5,
sizeLat=7,
nCharNodes=5,
residuals=FALSE,
fixedStyle=1,
freeStyle=1,
curvePivot = FALSE)
And the following is the semPaths output:
As you can see, there are weired horizonla lines in the last four manifest variables. How can I remove them? (and why it's happening?)
Thanks in advance!
Update:
This is also the case if saved to PDF (by adding arguments filetype = "pdf", filename = "x") to the function:

How to make R datatable buttons save to specific location

I have been using the R Datatable package to display information for a team a work with and spit out html docs for them to use. We are trying to find a way to add comments for all to see on the data tables, so i made the columns editable however i can't find a way to get this information to everyone. I created a save button that would work however i can only get this to save to the downloads folder of whoever is clicking the button. Is there a way to save this file to a public location?
Or a better way to add comments on the DT.
Here is the code im currently using:
---
title: "Comments Test"
output: html_document
---
#### `r format(Sys.time(), "%B %d, %Y")`
```{r echo=FALSE, warning=FALSE}
library(DT)
df <- data.frame(matrix(rnorm(50), nrow=10))
df$Comments <- ""
datatable(df, extensions = c('FixedHeader',
'ColReorder', 'Buttons'),
options = list(
dom = 'Blfrtip',
buttons = list(list( extend = 'csv',
filename = '//public/comments/comments.csv',
text = 'Save')),
autoWidth = TRUE,
fixedHeader = TRUE,
colReorder = TRUE),
width = "965px", fillContainer = FALSE, escape = FALSE,
rownames = FALSE, autoHideNavigation = FALSE, editable = TRUE)
```
Thank You.

Are rCharts and DT compatible in rmarkdown?

I am trying to create a document with rmarkdown that includes both plots from the rCharts package and a datatable using the DT library included in htmlwidgets.
For some reason I cannot display both of them together.
---
title: "Untitled"
output: html_document
---
```{r, echo=FALSE}
library(DT)
library(rCharts)
df<-data.frame(Name=c("a","Z","h","k","j"),Value=(sample(10^7,5)))
datatable(df, filter = 'top', options = list(
pageLength = 10,iDisplaylength=10, autoWidth = TRUE
))
```
```{r, message=FALSE, echo=FALSE, results='asis'}
df<-data.frame(label=c("One","Two","Three"),valuea=c(1,2,3),
othera=c(10,11,12),stringsAsFactors = FALSE)
p1 <- nPlot(valuea~ label, data = df, type = 'pieChart')
#Different options I tried
p1$print('inline', include_assets = TRUE, cdn = FALSE)
#p1$show('inline', include_assets = TRUE, cdn = FALSE)
#p1$print('inline', include_assets = TRUE)
#p1$show('inline', include_assets = TRUE)
#These provide an error
#p1$print('inline', include_assets = TRUE, cdn = TRUE)
#p1$show('inline', include_assets = TRUE, cdn = TRUE)
```
The commented lines are the things I have tried.
Note I: if p1$print('inline', include_assets = TRUE, cdn = FALSE) is commented the datatable is displayed properly.
Note II: I am aware of p1$save() function combined with an iframe, however, I would like to use the chart inline.
The jQuery library is included at the top of the page and when you include_assets in the print, the it is included again which causes issues.
To fix this, you can try setting include_assets to false and adding the required libraries except jQuery "by hand".
p1 <- nPlot(valuea~ label, data = df, type = 'pieChart')
cat("<link rel='stylesheet' href=.../R/3.1/library/rCharts/libraries/nvd3/css/nv.d3.css>
<link rel='stylesheet' href=.../R/3.1/library/rCharts/libraries/nvd3/css/rNVD3.css>
<script type='text/javascript' src=.../R/3.1/library/rCharts/libraries/nvd3/js/d3.v3.min.js></script>
<script type='text/javascript' src=.../R/3.1/library/rCharts/libraries/nvd3/js/nv.d3.min-new.js></script>
<script type='text/javascript' src=.../R/3.1/library/rCharts/libraries/nvd3/js/fisheye.js></script> ")
p1$print('inline', include_assets = F, cdn = FALSE)
You can find the required libraries and links by running p1$print('inline', include_assets = T, cdn = FALSE) in R, they will be the first lines of output. The src paths are absolute so I replaced some of it by ... in the code above.

Setting rChart width and height

In the (few so far) learning-oriented examples on the web of integrating knitr and rCharts we often see a line of code within an R chunk as follows:
options(RCHART_WIDTH = 800, RCHART_HEIGHT = 500)
When I include this sort of variable setting in an R chunk of an R markdown document it seems to make no difference at all to the actual size of the eventual chart in html. What extra thing am I missing that will let me control the width and height of rCharts? Reproducible *.rmd file is below.
```{r echo = F, message = F, cache = F}
opts_chunk$set(results = 'asis', comment = NA, message = F, tidy = F, echo=FALSE, cache=FALSE)
require(rCharts)
options(RCHART_WIDTH = 600, RCHART_HEIGHT = 400)
```
## Example plot with un-customised dimensions
```{r}
data(economics, package = 'ggplot2')
econ <- transform(economics, date = as.character(date))
m1 <- mPlot(x = 'date', y = c('psavert', 'uempmed'), type = 'Line',
data = econ)
m1$set(pointSize = 0, lineWidth = 1)
m1$print('chart2', include_assets = TRUE, cdn=TRUE)
```
The issue is that the print method does not include the css that sizes the charts. Easiest fix would be to add the following css to your Rmd file .rChart{width: 600px; height: 400px}. The print method is being rewritten and we are trying to figure out the most flexible yet useful way to do it.

Resources