Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205343

Image to an excel spreadsheet in R

$
0
0

I have this script so I can convert an image to an excel. However, when I save to Excel, it is saved empty, without the printed image. Do you know why that happens?

Underneath, the libraries are loaded and the code is initialized.

library(raster)
library(openxlsx)
library(magick)
require(tidyr)
require(dplyr)
require(lazyeval)
[enter image description here][1]
img <- readJPEG("hSAxgtiSnG.jpg", native = TRUE)

rlogo <- image_read(img) %>% 
  image_scale("50%") %>%
  image_data()

size  <- dim(rlogo[1,,])
rlogo <- as.raster(as.integer(rlogo)/255)
plot(rlogo)

rlogo <- as.character(rlogo)
dim(rlogo) <- size 
rlogo <- as.data.frame(rlogo)

colours <- rlogo %>% 
  gather(col, colour) %>%
  group_by(col) %>%
  mutate(row = seq(n())) %>%
  ungroup() %>%
  mutate(col = readr::parse_number(col))

blank <- rlogo
blank[] <- ""

wb <- createWorkbook("coolbutuseless")
addWorksheet(wb, "Rlogo", gridLines = TRUE)
writeData(wb, sheet = 1, blank, colNames = FALSE)
setColWidths(wb, shee=1, cols=seq(size[1]), widths = 2)

for (col_df in split(colours, colours$colour)) {
  cell_style <- createStyle(col_df$colour[[1]])
  addStyle(wb, sheet = 1, cell_style, rows = col_df$col, cols = col_df$row, gridExpand = FALSE)
}

openxlsx::saveWorkbook(wb, "crap_2.xlsx", overwrite = TRUE)

enter image description hereenter image description here


Viewing all articles
Browse latest Browse all 205343

Trending Articles