I am building a new htmlwidget package called fusionchartsR (https://github.com/alexym1). I tried to embed a little piece of code to my rmarkdown report however, it doesn't work and I don't know why. I tried differents strategies without any success.
First strategy
---
title: "Stack overflow"
author: "John Doe"
date: "01/04/2020"
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Render an htmlwidget graphic
```{r}
library(fusionchartsR)
df <- data.frame(label = c("Venezuela", "Saudi", "Canada", "Russia"), value = c(290, 260,180, 115))
fusionPlot(data = df, type = 'pie2d') %>%
fusionTheme(theme = "fusion")
```
Second strategy
# Webshot and phantomjs have been previously installed.
library(webshot)
webshot::install_phantomjs()
# Then, I loaded packages and built a little piece of code
library(fusionchartsR)
library(htmlwidgets)
df <- data.frame(label = c("Venezuela", "Saudi", "Canada", "Russia"), value = c(290, 260,180, 115))
widget <- fusionPlot(data = df, type = 'pie2d') %>%
fusionTheme(theme = "fusion")
# Save a rendered widget to an HTML file
saveWidget(widget = widget, file = "Mywidget.html")
# An error appeared: `Error: pandoc document conversion failed with error 99`
# Take a webshot
webshot(url = "Mywidget.html", file = "webshot.png")
The Mywidget.html can be found on your Documents folder.
How can I solve this problem ?
I will be very greatful !
Instead of using webshot, you should consider to try webshot2 on https://github.com/rstudio/webshot2 which doesn't suffer from this issue. I have replicated your scenario with webshot2, the issue is resolved as below screenshot. See my detailed answer to the similar case.
The code:
# Webshot and phantomjs have been previously installed.
library(webshot2)
# install.packages("remotes")
# remotes::install_github("alexym1/fusionChartsR")
# Then, I loaded packages and built a little piece of code
library(fusionchartsR)
library(htmlwidgets)
df <- data.frame(label = c("Venezuela", "Saudi", "Canada", "Russia"), value = c(290, 260,180, 115))
widget <- fusionPlot(data = df, type = 'pie2d') %>%
fusionTheme(theme = "fusion")
# Save a rendered widget to an HTML file
saveWidget(widget = widget, file = "Mywidget.html")
# An error appeared: `Error: pandoc document conversion failed with error 99`
# Take a webshot
webshot(url = "Mywidget.html", file = "webshot.png")
The output:
Related
I'm trying to print an R help file vignette in an R notebook chunk for output into an HTML file. I want the entire vignette to show up as output in the HTML notebook preview because it serves as a nice data dictionary for a quick regression example. Using library(mlbench), I tried:
print(?BostonHousing2)
and I tried just calling
?BostonHousing2
in the code chunks, and neither of them output to the HTML file, they just populate in the Help tab inside of RStudio.
Anyone have any ideas?
Here is a way (if I correctly understand what you want). But maybe not the best one.
---
title: "Untitled"
author: "Stéphane Laurent"
date: "29 février 2020"
output: html_document
---
```{r setup, include=FALSE}
library(gbRd) # for Rd_fun
```
```{r, results='asis'}
Rd <- Rd_fun(help("pretty"))
htmlfile <- tempfile(fileext = ".html")
tools::Rd2HTML(Rd, htmlfile, package = "",
stages = c("install", "render"))
htmllines <- readLines(htmlfile)
i <- grep("<body>", htmllines)
j <- grep("</body>", htmllines)
cat(htmllines[(i+1):(j-1)], sep = "\n")
```
Based on Noam Ross's solution, and modified with some of the code in Stéphane Laurent's answer, the following function can be used to return an HTML version of the help file
help_console <- function(topic, package,
format=c("text", "html", "latex", "Rd"),
before=NULL, after=NULL) {
# topic - the command for which help is required
# package - the package name with the required topic
# format - output format
# before - place code before the output e.g. "<blockquote>"
# after - place code after the output e.g. "</blockquote>"
# based on code by Noam Ross
# http://www.noamross.net/archives/2013-06-18-helpconsoleexample/
# Stéphane Laurent
# https://stackoverflow.com/questions/60468080/
# print-an-r-help-file-vignette-as-output-into-an-r-html-notebook
# and Michael Sumner (mdsumner)
# https://stackoverflow.com/questions/7495685/
# how-to-access-the-help-documentation-rd-source-files-in-r
format <- match.arg(format)
if (!is.character(topic)) topic <- deparse(substitute(topic))
db <- tools::Rd_db(package)
helpfile <- db[paste0(topic, ".Rd")][[1]]
hs <- capture.output(
switch(
format,
text = tools::Rd2txt(helpfile),
html = tools::Rd2HTML(
helpfile,
package = "",
stages = c("install", "render")
),
latex = tools::Rd2latex(helpfile),
Rd = tools:::prepare_Rd(helpfile)
)
)
if (format == "html") {
i <- grep("<body>", hs)
j <- grep("</body>", hs)
hs <- hs[(i+1):(j-1)]
}
hs <- c(before, hs, after)
hs <- cat(hs, sep = "\n")
invisible(hs)
}
Which then can be used as follow in a RMarkdown document, ready for knitting:
```{r, echo = FALSE, results = "asis"}
help_console("pretty", "base", format = "html")
```
As usual, the R chunk needs to be set to results = "asis" to output to HTML.
This solution does not require the gbRd package.
For some strange reason, I can Knit to html_vignette from RStudio's Knit dropdown, but cannot successfully Build Document from the RStudio menu with Project Options - Build Tools - Roxygen Configure - Vignettes enabled, with the error
Error: Sections \title, and \name must exist and be unique in Rd files
thrown when trying to use the help_console function.
However, I was able to successfully able to create the HTML vignette and incorporate into a package with A-breeze's solution, or building a source or binary package (building source/binary packages can be done from RStudio's "build" menu).
Been learning how to use Drake today and managed to migrate my code but not my R markdown reports yet. This report compiles fine, and produces the expected output, but also gives this error which no amount of searching has shed light on.
I am using the r_make() command as recommended, and my plan so far reads:
plan_0 <- drake_plan(
raw_c_data = read_rds(
file_in("data/css_carib_data.rds")),
raw_c_fg = read_rds(
file_in("data/css_carib_fg.rds")),
c_data = clean_caribbean_fg(raw_c_data, raw_c_fg),
clean_c_fg = write_rds(
c_data,
file_out("data/clean_c_fg.rds"),
compress = "gz"),
c_maps = gen_maps(bbx_1, bbx_2, bbx_3, bbx_4, bbx_5),
c_maps_out = write_rds(
c_maps,
file_out("data/c_maps.rds"),
compress = "gz"),
c_base_report = rmarkdown::render(
knitr_in("R/0_prepare_data.Rmd"),
output_file = file_out("0_prepare_data.html"),
quiet = T)
)
That Rmd file starts with the following
---
title: "0: Data Description"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
require(drake)
require(tidyverse)
require(ggmap)
loadd(raw_c_fg)
loadd(clean_c_fg)
loadd(c_maps)
```
This document is a description of the data processed in `0_prepare_data.R` from the Caribbean region. The following table gives the counts of each classification within the aggregated functional groups.
```{r tables, echo = F}
raw_c_fg %>%
pull(fg) %>%
table() %>%
knitr::kable(col.names = c("Class", "Count"),
caption = "Counts of Each Functional Group")
```
I'm happy to attach as much more as required, but hopefully this is enough to see why I am getting this error?
Figured out what was going wrong, and it was the usual conflict between Rmd and Rproj/drake with relative locations. I had located the Rmd file within the R directory, but _drake.R was in the base directory, and so the differences in locating the output were causing drake to look in the wrong place.
I'm trying to create an interactive document like the tutorial on Shiny's website. However, I want the plot to be a Leaflet map. Following the Leaflet tutorial, it seems like the following should work using leaflet::renderLeaflet() instead of shiny::renderPlot().
Instead, I get an error when running the document: Error: object 'input' not found. Is there a special way to expose input to leaflet when using Shiny widgets inside an interactive Rmarkdown document?
---
runtime: shiny
output: html_document
---
```{r echo = FALSE}
library(magrittr)
shiny::selectInput(
"weight", label = "Weight:",
choices = c(1, 2, 3, 4, 5), selected = 5
)
```
```{r include = FALSE}
csa <- tigris::combined_statistical_areas(class = "sf")
```
```{r echo = FALSE}
leaflet::renderLeaflet({ # <- main difference from tutorial
leaflet::leaflet() %>%
leaflet::addTiles() %>%
leaflet::addPolylines(data = csa, color = '#333', weight = input$weight)
})
```
I can't confirm your issue. Your sample interactive document runs just fine:
I've been using the following library versions on R 3.6.1
leaflet_2.0.2
rmarkdown_1.16
shiny_1.4.0
I'm trying to create a pdf report using R, Shiny, and Rmarkdown on a Centos7/linux server that would include both plotly generated plots and tables using kable.
I've been able to successfully generate pdf reports on a Mac OS however, when I move the code to the linux based server I run into issues with generating the pdf report through the web browser. I've listed the error message I receive while generating the report below
The corresponding error I receive is:
pandoc: Could not find image `/srv/shiny-server/.../figure-latex/raw%20variant%20plot-1.pdf', skipping...
pandoc: Could not find image `/srv/shiny-server/.../figure-latex/Variant%20Table-1.pdf', skipping...
pandoc: Error producing PDF from TeX source.
! LaTeX Error: File `titling.sty' not found.
I've tried a number of different solutions to resolve this problem including updating my Latex postscript editor to the most recent version Texlive2017 (that contains the 'titling.sty', and tested to see if it can generate pdfs when launched within the linux server envrionment), updating Rstudio/Rmarkdown from the github repository, updating R to the most recent version (3.3) and all associated R plot/other packages.
I suspect that the issue is that Rmarkdown isn't pointing to my Texlive within the linux environment so that it doesn't recognize the titling.sty package.
I've also tried using the downloadHandler as provided by Shiny however ran into similar issues. Any guidance would be greatly appreciated!!
I've included my code below:
server.R
generateReport <- observeEvent(input$genReportInput, {
params <- list(runId = as.list(input$runInput),
data_Specimen = sumSpecimenTab(),
rawdata_VariantPlotRun = runIdQMData(),
rawdata_VariantPlotAll = allRunQMData())
outputPath <- paste("qmPath/", Sys.Date(), "_QMrun_nsc", input$runInput, ".pdf", sep = "")
rmarkdown::render("test_downloadQMReport.Rmd", output_dir = qmPath, output_file = paste(Sys.Date(), "_QMrun_nsc", input$runInput, ".pdf", sep = ""),
pdf_document(latex_engine = "pdflatex", latex_dependency("") ), params = params,envir = new.env(parent = globalenv()))
})
Rmarkdown
---
output: pdf_document
always_allow_html: yes
params:
data_Specimen: NA
rawdata_VariantPlotRun: NA
rawdata_VariantPlotAll: NA
---
```{r libraries, echo = FALSE}
library(plotly)
library(lazyeval)
library(tidyr)
library(reshape2)
library(plyr)
library(stringr)
library(dplyr)
library(ggplot2)
library(grid)
library(gridExtra)
library(knitr)
library(pander)
library(gtable)
```
```{r kable table, echo=FALSE}
kable(sumSpecimenTab <- tbl_df(params$data_Specimen))
```
```{r raw variant plot, plotly=TRUE, fig.height=9, fig.width=8, echo=FALSE}
runInputQMData <- tbl_df(params$rawdata_VariantPlotRun)
allRunQMdf <- tbl_df(params$rawdata_VariantPlotAll)
maxVF <- max(allRunQMdf$VF) + 0.1
ggplot(runInputQMData, aes(x=interaction(cDNAChange, GI), y=VF, color = GI, label = Rep)) + ylim(0,maxVF) + geom_point(shape=16, size=2) + labs(title= "") + theme(plot.margin = unit(c(1,1,3.5,1), "cm")) + theme(axis.text.x = element_text(angle=70, hjust=1, size = 10), axis.title.x=element_blank(),axis.title.y=element_blank(), legend.position="none", text=element_text(size=12))
```
Outside of Rmarkdown the stand alone googleVis chart works fine, but when I plug it in the Rmarkdown file I am receiving just the Rmarkdown Code:
Viewer Output:
> TEST H 4/13/2016 require(googleVis) Loading required package:
> googleVis Welcome to googleVis version 0.5.10 Please read the Google
> API Terms of Use before you start using the package:
> https://developers.google.com/terms/
>
> Note, the plot method of googleVis will by default use the standard
> browser to display its output. See the googleVis package vignettes
> for more details, or visit http://github.com/mages/googleVis. To
> suppress this message use:
> suppressPackageStartupMessages(library(googleVis))
>
> dttm = data.frame(DT_ENTRY=Sys.Date()-1:20,variable="x",value=1:20)
> g1=gvisAnnotationChart(dttm,datevar="DT_ENTRY",numvar="value",idvar="variable")
> plot(g1) starting httpd help server ... done
Rmarkdown Code Below:
---
title: "test"
author: "H"
date: "4/13/2016"
output: html_document
highlight: tango
number_sections: yes
---
```{r}
require(googleVis)
dttm = data.frame(DT_ENTRY=Sys.Date()-1:20,variable="x",value=1:20)
g1=gvisAnnotationChart(dttm,datevar="DT_ENTRY",numvar="value",idvar="variable")
plot(g1)
```
The r chunk has to be declared as:
```{r plotg0, results='asis', tidy=FALSE, echo=FALSE}
The "asis" is important because it returns raw HTML
I am not sure if you still need an answer. However, I faced the same problem while trying to embed a sankey plot from gvisSankey onto a section on Rmarkdown. Thankfully, I found the solution on github.com/mages/googleVis (weirdly not recommended on Google Search).
Below is a reproducible example.
First, to create a sankey plot, I do this:
# install.packages("googleVis")
library(googleVis)
# Create a data.frame that is gvisSankey-conform
# i.e., has these columns: FROM - TO - WEIGHT
df <- data.frame(FROM = rep(c("A","B","C"), each = 3),
TO = rep(c("D","E","F"), times = 3),
WEIGHT = sample(1:10, 9, replace = TRUE))
sankeyplot <- gvisSankey(data = df,
from = "FROM",
to = "TO",
weight = "WEIGHT")
plot(sankeyplot)
plot(sankeyplot) will open a new tab containing the sankeyplot. Now, how do I embed the sankeyplot onto my Rmarkdown?
There are two important points:
To set self_contained:false in the YAML header
To set results='asis' in the R code chunk where the sankeyplot is embedded
---
title: "SankeyPlot_Example"
output:
html_document:
self_contained: false
date: '2022-10-21'
---
## Embed googleVis object (sankey plot) onto Rmarkdown
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(googleVis)
# Create a reproducible data.frame
df <- data.frame(FROM = rep(c("A","B","C"), each = 3),
TO = rep(c("D","E","F"), times = 3),
WEIGHT = sample(1:10, 9, replace = TRUE))
```
```{r results='asis'}
sankeyplot <- gvisSankey(data = df,
from = "FROM",
to = "TO",
weight = "WEIGHT")
print(sankeyplot, 'chart')
```