When I try to run this by Jupyter:
library(leaflet)
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
m # Print the map
I get this error:
HTML widgets cannot be represented in plain text (need html).
As suggested here I have tried:
library(plotly)
embed_notebook(m)
but I get:
Error in UseMethod("embed_notebook"): no applicable method for 'embed_notebook' applied to an object of class "c('leaflet', 'htmlwidget')
How could I plot this kind of graph?
embed_notebook is specifically defined for plotly objects. I would look through the documentation to see if leaflet has its own equivalent function.
Alternatively, since it's an html widget, you can save it as an html file, then embed that file inside of an iframe in your notebook. This can be accomplished with something like
library(IRdisplay)
htmlwidgets::saveWidget(m, "m.html")
display_html('<iframe src="m.html" width=100% height=450></iframe>')
If you don't want to keep a bunch of html files in your folder, you can also enter the raw html of your widget into your iframe then delete it using
rawHTML = base64enc::dataURI(mime = "text/html;charset=utf-8", file = "m.html")
display_html(paste("<iframe src=", rawHTML, "width=100% height=450></iframe>", sep = "\""))
unlink("m.html")
But I've found that this generates an error with the most recent version of Chrome.
If it helps, I cobbled together the following function from the source code of embed_notebook
embed = function(x, height) {
library(IRdisplay)
tmp = tempfile(fileext = ".html")
htmlwidgets::saveWidget(x, tmp)
rawHTML = base64enc::dataURI(mime = "text/html;charset=utf-8", file = tmp)
display_html(paste("<iframe src=", rawHTML, "width=100% height=", height, "id=","igraph", "scrolling=","no","seamless=","seamless", "frameBorder=","0","></iframe>", sep = "\""))
unlink(tmp)
}
But again, this may not work for Chrome.
Related
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")
So I'm trying to write an html R markdown document with interactive shiny bits that allow the user to edit a graph and then download the results to a pdf. However, there is something catastrophically wrong with the way that I'm trying to do this because as soon as the html starts, it overwrites the original markdown file with the contents of the pdf - turning it into complete gibberish right in the editor.
I doubt that I've found a completely new way to fail at R but I haven't been able to find where anybody else has had this issue. Additionally, I've looked over the shiny reference material and I'm just going in circles at this point, so any help would be greatly appreciated.
I'm using Rstudio 1.0.44, rmarkdown 1.2 and shiny 0.14.2. A small (not)working example:
---
title: "Minimum Failing Example"
author: "wittyalias"
date: "December 5, 2016"
output: html_document
runtime: shiny
---
```{r echo = FALSE}
library(ggplot2)
today <- Sys.Date()
inputPanel(downloadButton("dnld", label = "Download pdf"))
renderPlot({
# Example code from http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/
p1 <<- ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet, group=Chick)) +
geom_line() +
ggtitle("Growth curve for individual chicks")
p1
})
reactive({
fname <- paste0("Chick Weight - ", today, ".pdf")
output$dnld <- downloadHandler(filename = fname,
content = makethepdf(file))
makethepdf <- function(fname) {
pdf(fname,
width = 14,
height = 8.5)
p1
dev.off()
}
})
```
EDIT: To be clear: I want the user to be able to download multiple pages of graphs, some of which will have different formatting. The user won't be downloading just a pdf version of the markdown document.
This happens because reasons I weren't able to identify makethepdf runs with the file = [name of the file]. Insert a print(fname) to see. The download handler isn't supposed to be inside an observer though. You need to have it outside on its own. I also failed to make pdf() dev.off() combination work for some reason so here's a working version below.
output$dnld = downloadHandler(filename = paste0("Chick Weight - ", today, ".pdf"),
content = function(file){
ggsave(file, plot = p1, width = 14, height = 8.5)
})
Use tempfile() and tempdir() to create a temporary file:
output$downloadReport = downloadHandler(
filename = function() {
normalizePath(tempfile("report_", fileext = ".docx"), winslash = "/")
},
content = function(file) {
out = rmarkdown::render("./report.Rmd",
output_file = file,
output_dir = tempdir(),
output_format = "pdf_document",
intermediates_dir = tempdir(),
envir = new.env(),
params = list( fontSize = 10)
)
})
I usually use a separate .Rmd template for my downloaded reports as the layout and text are usually similar but not identical to what works in an app.
I also find using parameters is a convenient way to pass input settings from my app to my report. See this RStudio post for details
Alright, so there are a number of problems with my code, but using some of the suggestions in the other answers I've been able to work it out.
The primary problem with this little document is that content in the downloadHandler is a function, but in my code I set content equal to the result of a function call. It looks like when the shiny app is first run it compiles content, thinking that it is a function, but actually ends up calling the function. It sends file as an arguement, which doesn't seem to exist except as a base function. Calling makethepdf with just file throws an error when I use it in the console, but for whatever reason in this app it just goes with the call, apparently with file = [name of the .Rmd] (just as OganM said).
To fix, change this:
output$dnld <- downloadHandler(filename = fname,
content = makethepdf(file))
to
output$dnld <- downloadHandler(filename = fname,
content = makethepdf)
To be clear: this code does not overwrite the .Rmd file if content calls makethepdf with any argument other than file. For instance, content = makethepdf(fnm)) causes the download button to display an object not found error and content = makethepdf(fname)) causes the download button to throw an attempt to apply non-function error when pressed.
I want to add a pulsing marker to the map I built with the R leaflet package
Here is the plugin I want to use. In order to do it, I wrote the following code from this from this github account
library(leaflet)
library(htmltools)
library(htmlwidgets)
# This tells htmlwidgets about our plugin name, version, and
# where to find the script. (There's also a stylesheet argument
# if the plugin comes with CSS files.)
esriPlugin <- htmlDependency("leaflet-icon-pulse",version = "1.0",
src = c(href = "https://raw.githubusercontent.com/mapshakers/leaflet-icon-pulse/master/src/"),
script = "L.Icon.Pulse.js",stylesheet ="L.Icon.Pulse.css")
# A function that takes a plugin htmlDependency object and adds
# it to the map. This ensures that however or whenever the map
# gets rendered, the plugin will be loaded into the browser.
registerPlugin <- function(map, plugin) {
map$dependencies <- c(map$dependencies, list(plugin))
map
}
leaflet() %>% setView(-52.520, 13.185, zoom = 5) %>%
# Register ESRI plugin on this map instance
registerPlugin(esriPlugin) %>%
# Add your custom JS logic here. The `this` keyword
# refers to the Leaflet (JS) map object.
onRender("function(el,x) {
var pulsingIcon = L.icon.pulse({iconSize:[20,20],color:'red'});
var marker = L.marker([52.9167,13.9333],{icon: pulsingIcon}).addTo(this);
}")
However, it does not work. I got a grey rectangle instead of a beautiful map with a beautiful pulsing marker. Anyone see something wrong in my code?
This code works with three remarks:
the js and css file are stored locally
the icon is displayed correctly in RStudio viewer but it does not pulsate
With the "Show in new window" option in the Viewer everything works fine (tested in Firefox 48.0 and Chrome 53.0.2785.116 (64-bit))
This is the code (adjust the src parameter to match your file location):
library(leaflet)
library(htmltools)
library(htmlwidgets)
# This tells htmlwidgets about our plugin name, version, and
# where to find the script. (There's also a stylesheet argument
# if the plugin comes with CSS files.)
esriPlugin <- htmlDependency("leaflet-icon-pulse",version = "1.0",
src = "/home/valter/Desktop/test",
script = "L.Icon.Pulse.js",stylesheet ="L.Icon.Pulse.css")
# A function that takes a plugin htmlDependency object and adds
# it to the map. This ensures that however or whenever the map
# gets rendered, the plugin will be loaded into the browser.
registerPlugin <- function(map, plugin) {
map$dependencies <- c(map$dependencies, list(plugin))
map
}
leaflet() %>% addTiles() %>% setView(-52.520, 13.185, zoom = 5) %>%
# Register ESRI plugin on this map instance
registerPlugin(esriPlugin) %>%
# Add your custom JS logic here. The `this` keyword
# refers to the Leaflet (JS) map object.
onRender("function(el,x) { var pulsingIcon = L.icon.pulse({iconSize:[20,20],color:'red'});
var marker = L.marker([13.185,-52.520],{icon: pulsingIcon}).addTo(this); }")
We want to display an htmlwidget inside an OpenCPU application.The html is generated by Leaflet without problems, however we have some troubles to display it within the OpenCPU app. We used the following function to generate the Leaflet Map:
leafmap1 <- function(ecoregion='10105',wdpa_id='1500'){
require(leaflet)
require(shiny)
require(htmlwidgets)
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
m
}
The JavaScript is as follows:
function SochiCtrl($scope){
$scope.ecoregions = ['10105']
$scope.wdpa_ids = ["1500"]
$scope.ecoregion = $scope.ecoregions[0]
$scope.wdpa_id = $scope.wdpa_ids[0]
$scope.makeChart = function(){
var req = ocpu.rpc("leafmap1",
{"ecoregion": $scope.ecoregion, "wdpa_id": $scope.wdpa_id}, function(output){
$('#map').html(output)
}).fail(function(text){
alert("Error: " + req.responseText);
});
}
$scope.$watchCollection('[ecoregion, wdpa_id]', function(newValues){
console.log(newValues)
$scope.makeChart({ecoregion: newValues[0], wdpa_id: newValues[1]})
})
}
Now the app shows the Leaflet frame but I have some problems getting the json from OpenCPU I got the following error No method asJSON S3 class: htmlwidget. I also tried with:
m <- toJSON(m, force= TRUE)
but it doesn't seem to work.
The full code is available at: https://github.com/Arevaju/ocpuleaflet.
Thanks a lot for your help and congratulations for your great work!!
Sorry as this is not a tested answer, but this is easier to explain a proposed approach here than in a comment.
What I propose is to have your function leafmap1 return plain text (HTML) instead of the leaflet object.
You can see that the leaflet object inherits the class htmlwidget.
For this class, there exists a method for the generic function toHTML that would allows retrieving such HTML code.
Assumed a leaflet object:
m = leaflet() %>% addTiles()
Let's have a look at it's class:
class(m)
[1] "leaflet" "htmlwidget"
Get the underlying generated html:
> (out <- unclass(htmlwidgets:::toHTML(m)))
[[1]]
<div id="htmlwidget-7863" style="width:100%;height:400px;" class="leaflet html-widget"></div>
[[2]]
<script type="application/json" data-for="htmlwidget-7863">{"x":{"calls":[{"method":"addTiles","args": ["http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",null,null,{"minZoom":0,"maxZoom":18,"maxNativeZoom":null,"tileSize":256,"subdomains":"abc","errorTileUrl":"","tms":false,"continuousWorld":false,"noWrap":false,"zoomOffset":0,"zoomReverse":false,"opacity":1,"zIndex":null,"unloadInvisibleTiles":null,"updateWhenIdle":null,"detectRetina":false,"reuseTiles":false,"attribution":"© <a href=\"http://openstreetmap.org\">OpenStreetMap\u003c/a> contributors, <a href=\"http://creativecommons.org/licenses/by-sa/2.0/\">CC-BY-SA\u003c/a>"}]}]},"evals":[],"jsHooks":[]}</script>
[[3]]
NULL
attr(,"html_dependencies")
attr(,"html_dependencies")[[1]]
[...]
Third slot contains dependancies (javascript+css) so I guess those are already loaded in your report.
You may return the concatenation of the first two components (function result):
return(paste(out[[1]], out[[2]], sep="\n"))
I would like to know if there is any function which makes it easy to visualize an html object in the RStudio's viewer pane. For instance, I would like to know if it would be possible to view an html table in the viewer pane.
library("Quandl")
library("knitr")
df <- Quandl("FBI_UCR/USCRIME_TYPE_VIOLENTCRIMERATE")
kable(head(df[,1:9]), format = 'html', table.attr = "class=nofluid")
I have a solution that works for kable tables.
kable(iris) %>% kableExtra::kable_styling()
This is automatically displayed in the viewer pane. No need for tempfile.
I have this functionality in my htmlTable package and the function is rather simple:
print.htmlTable<- function(x, useViewer = TRUE, ...){
# Don't use viewer if in knitr
if (useViewer &&
!"package:knitr" %in% search()){
htmlFile <- tempfile(fileext=".html")
htmlPage <- paste("<html>",
"<head>",
"<meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\">",
"</head>",
"<body>",
"<div style=\"margin: 0 auto; display: table; margin-top: 1em;\">",
x,
"</div>",
"</body>",
"</html>", sep="\n")
cat(htmlPage, file=htmlFile)
viewer <- getOption("viewer")
if (!is.null(viewer) &&
is.function(viewer)){
# (code to write some content to the file)
viewer(htmlFile)
}else{
utils::browseURL(htmlFile)
}
}else{
cat(x)
}
}
RStudio recommends that you use the getOption("viewer") instead of #Ramnath's suggestion, the raw RStudio::viewer(). My solution also adds the utils::browserURL() in case you are not using RStudio. I got the idea from this blog post.
Here is a quick way to do this in RStudio
view_kable <- function(x, ...){
tab <- paste(capture.output(kable(x, ...)), collapse = '\n')
tf <- tempfile(fileext = ".html")
writeLines(tab, tf)
rstudio::viewer(tf)
}
view_kable(head(df[,1:9]), format = 'html', table.attr = "class=nofluid")
If the kable function can return an object of class kable, then one could rename view_kable as print.kable in which case merely calling the kable function would open the table in the viewer. If you think this is useful, please go ahead and file a feature request on the knitr github page.
As was explained on this RStudio Support page, the key is to use tempfile() :
Note that the Viewer pane can only be used for local web content. This
content can either be static HTML files written to the session
temporary directory (i.e. files with paths generated by the tempfile
function) or a locally run web application.
See my answer to this question for a bare-bones example.
For kable objects, we can use print.kableExtra
library(knitr)
x <- kable(head(iris), format = "html")
library(kableExtra)
class(x) <- c("kableExtra", class(x))
print(x)