问题
I am using the R programming language. I am trying to combine a HTML file and a JPG Image file together.
My code looks something like this:
library(plotly)
library(shiny)
library(magick)
#create widget_1
widget_1 = plot_ly(iris, x = ~Sepal.Length, type = "histogram", nbinsx = 20)
#upload some jpg image from your computer into R
my_image = image_read("my_image.jpg")
doc <- htmltools::tagList(
div(widget_1, style = "float:left;width:50%;"),
div(my_image,style = "float:left;width:50%;")
)
htmltools::save_html(html = doc, file = "C://Users//Me//Desktop//combined_file.html")
However, this code produces the following error:
Error in as.vector(x, "character"): cannot coerce type 'externalptr' to vector of type 'character'
Is it possible to save html and jpg files together in R? Or is this simply not possible? Has anyone ever tried do this before?
Thanks
回答1:
Maybe you can put this in R Markdown and knit it as HTML to get the output in one HTML file.
---
title: "temp"
output: html_document
---
```{r, echo=FALSE, warning=FALSE, message=FALSE}
library(plotly)
library(shiny)
library(magick)
widget_1 = plot_ly(iris, x = ~Sepal.Length, type = "histogram", nbinsx = 20)
#upload some jpg image from your computer into R
my_image = image_read("try.png")
```
```{r, echo=FALSE, warning=FALSE, message=FALSE, fig.height=3}
widget_1
```
```{r, echo=FALSE, warning=FALSE, message=FALSE, out.width = "600px"}
my_image
```
This generates HTML file as :
来源:https://stackoverflow.com/questions/65802371/r-error-in-as-vectorx-character-cannot-coerce-type-externalptr-to-vecto