Directly Loading HTML files in R?
Asked Answered
P

1

0

I made the following 4 maps in R:

library(leaflet)
library(leaflet.extras)
id = 1:1000
long = 2.2945 + rnorm( 1000, 0.1085246 , 0.1)
lat = 48.8584 + rnorm( 1000, 0.009036273 , 0.1)
my_data_1 = data.frame(id, lat, long)
id = 1:1000
long = 2.2945 + rnorm( 1000, 0.1085246 , 0.1)
lat = 48.8584 + rnorm( 1000, 0.009036273 , 0.1)
my_data_2 = data.frame(id, lat, long)


map1 = my_data_1 %>%
    leaflet() %>%
    addTiles() %>%
    addHeatmap(lng=~long,lat=~lat,max=100,radius=20,blur=10)

map2 = my_data_2 %>%
    leaflet() %>%
    addTiles() %>%
    addHeatmap(lng=~long,lat=~lat,max=100,radius=20,blur=10)

map3 = my_data_1 %>% 
    leaflet() %>% 
    addTiles() %>% 
    addMarkers(clusterOption=markerClusterOptions())

map4 = my_data_2 %>% 
    leaflet() %>% 
    addTiles() %>% 
    addMarkers(clusterOption=markerClusterOptions())

I then saved each of these maps as an html file:

library(htmltools)
library(htmlwidgets)
library(plotly)

htmlwidgets::saveWidget(as_widget(map1), "m1.html")
htmlwidgets::saveWidget(as_widget(map2), "m2.html")
htmlwidgets::saveWidget(as_widget(map3), "m3.html")
htmlwidgets::saveWidget(as_widget(map4), "m4.html")

Based on the instructions in the following post, Read local HTML file into R, I learned how to import these html files back into R:

library(xml2)
rawHTML1 <- read_html(x = "m1.html")
# etc

I now want to directly load these html files into a dashboard in R. Here is the dashboard code:

---
title: "maps"
output: flexdashboard::flex_dashboard
---
    
```{r setup, include=FALSE}
#library(flexdashboard)
library(leaflet)
library(xml2)
library(leaflet.extras)
id = 1:1000
long = 2.2945 + rnorm( 1000, 0.1085246 , 0.1)
lat = 48.8584 + rnorm( 1000, 0.009036273 , 0.1)
my_data_1 = data.frame(id, lat, long)
id = 1:1000
long = 2.2945 + rnorm( 1000, 0.1085246 , 0.1)
lat = 48.8584 + rnorm( 1000, 0.009036273 , 0.1)
my_data_2 = data.frame(id, lat, long)

```    
  
   
Column {.tabset}
-------------------------------------
   
### map 1

```{r}

rawHTML1 <- read_html(x = "m1.html")
rawHTML1 
```   
 
### map 2
    
```{r}
rawHTML2 <- read_html(x = "m2.html")
rawHTML2 
```

### map 3
    
```{r}
rawHTML3 <- read_html(x = "m3.html")
rawHTML3 

```

### map 4
    
```{r}
rawHTML4 <- read_html(x = "m4.html")
rawHTML4 

But the resulting dashboard is not loading the maps correctly:

enter image description here

  • Does anyone know how to fix this problem?

I know that I can create the maps themselves in the rmarkdown/dashboard, but I would like to see if there is a way to directly load a pre-existing html version of the maps into the dashboard. Is this possible?

Thank you!

Phosphorous answered 24/8, 2022 at 5:11 Comment(0)
E
1

You can include it using <object>

Column {.tabset}
-------------------------------------
   
### map 1

<object type="text/html" data="m1.html"></object>

### map 2

<object type="text/html" data="m2.html"></object>

### map 3

<object type="text/html" data="m3.html"></object>

enter image description here


You can change size of the object like this:

<object type="text/html" data="m1.html" width="700" height="300"></object>
Efficient answered 24/8, 2022 at 8:6 Comment(2)
@ manro: thank you so much for your answer! Do you know why the maps are appearing significantly smaller than before? imgur.com/a/rUNvXIn Thank you so much!Phosphorous
@antonoyaro8 Look, I have added to the answer ;)Efficient

© 2022 - 2024 — McMap. All rights reserved.