Is it possible to adjust position of an inserted image with openxlsx? - r

I've created an excel template for reporting. In the excel file, there will be some images. I'm able to insert the images by using openxlsx package.
test.fpath <-'Templates/CB.xlsx'
wb <- openxlsx::loadWorkbook(test.fpath)
insertImage(wb, sheet = 1, file = "tm_player_image.png",startRow = 8, startCol = 3, width = 1.1, height = 1.73, units = "in")
saveWorkbook(wb, file = "createWorkbookExample.xlsx", overwrite = TRUE)
openxlsx package allows you to set specific value for starting row and column. When I run the script, excel file is saved like the following image.
First position of the image
However, I don't want the image start row = 8 and col = 3. I should able to drag the image where ever I want and define top-left position values. Is there any way to achieve this?
Adjusted position of the image
I need to define position of the image like this.
Left (Sol): 13,64''
Top (Üst): 0,74''
Thanks for your help.

It is possible, but there is no function to do this and AFAIK Excel positions are always relative to the screen and OS one is using. Therefore you have to use a bit try and error to get the correct positions.
Have a look at wb$drawings[[1]][[1]]. The content is an XML string. You are looking for the xdr:from part (as seen below). You have to adjust xdr:colOff and xdr:rowOff like in the example below. I had to insert a fairly high value to see any impact.
<xdr:from xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing">\n
<xdr:col xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing">2</xdr:col>\n
<xdr:colOff xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing">50000</xdr:colOff>\n
<xdr:row xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing">4</xdr:row>\n
<xdr:rowOff xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing">40000</xdr:rowOff>\n
</xdr:from>

Related

r what filename for saving gganimate file

The following two lines of code, taken from this script, do what I think they should; that is, use a previously generated ggplot file - gganim - create an animation in Rstudio's viewer (the first line) and save the last animation to the graphics directory on my mac (the second line). But my mac thinks this is a txt file. I've tried adding .mpeg, mp4, etc. to the file name without any success. Seems like one of this problems I'll do a Homer Simpson on after someone points out the solution.
gganimate::animate(gganim, nframes = length(yearsToDisplay), fps = 1, renderer = ffmpeg_renderer())
gganimate::anim_save("animateOutput", animation = last_animation(), path = "graphics")
After some more experimentation, I discovered that the following modifications to the two lines work; that is, I explicitly specify the file format in the ffmpeg_render function and in the anim_save file name
gganimate::animate(gganim, format = "mp4", nframes = length(yearsToDisplay), fps = 1, renderer = ffmpeg_renderer(format = "mp4"))
gganimate::anim_save("animateOutput.mp4", animation = last_animation(), path = "graphics")

R flextable as_image (how to insert external image into flextable, how define an image path)

I have an issue with inserting image into flextable.
Creating flextable and saving it in pptx works fine. But image don't show up.
Please have a look at the code and point to the error.
I think I have issue with the path to the image. It needs to the be an external image saved as png in specific fo, not in R.home()
Idea is to insert red_up.png image which is an red arrow if the Value is above 1.
final <- flextable(as.data.frame(final))
img.file <file.path("C:\\Users\\People\\Desktop\\Data_R\\Reporting\\red_up.png")
final <- display(final, i = ~ Value >1, col_key="Apr",
pattern = "{{signal_sign}}",
formatters = list(signal_sign ~ as_image(Apr, src = img.file, width=0.2, height=0.15)))
Thank you

GET request for an image returns array of 3 matrix

I obtain the url of an user profile image using twitter API. GET request on the url obtained returns me an array of three matrix. Why there are three matrix? When I display each matrix using image function (s3 generic) I got the image with different colour variation. Why I am not getting the actual image? I am new to all this. Please help.
> imatrix <- content(GET("http://pbs.twimg.com/profile_images/454954067488288768/fU6NY-EI_normal.jpeg"))
> class(imatrix)
[1] "array"
> image(imatrix[,,1])
Below Image is the actual image and above one is image displayed using image
Depends on where you want to go exactly you might have to look into other packages.
An easy example would be the jpeg-package:
library(jpeg)
link <- "http://www.starwars.com/the-force-awakens/images/share_1200x627.jpg"
download.file(link, destfile = "picture1.jpg", mode = "wb")
img <- readJPEG("picture1.jpg")
plot(1:2, type = "n")
rasterImage(img, 1, 1, 2, 2)
(I'm sorry that I changed the picture, but found mine more beautiful... :) )
If you want to go deeper into image processing in R, you might want to look into the imageR, or the ripa-package.
Lastly, you get the wrong colors in your picture, as the image-function plots the data using the heat.colors as specified with the col argument of the function.
I hope that points you in the right direction...

R, XLConnect: Setting color with RGB code

I have come across the following problem. A lot of data is written to an Excel file. Within the written excel table, I want to set the cell color to a non-predefined value (which is a function of the number written to the cell). E.g.: The higher the number in a given cell, the greener the cell.
I know that solutions exist for the package xlsx (See HERE and HERE). But I already use XLConnect throughout the entire project and do not want to convert all of the code that I have so far.
Currently, I use the following code to set the cell color:
# create the excel workbook
wb <- loadWorkbook("FILENAME.xls", create=TRUE)`
# Create a CellStyle with yellow solid foreground
CellColor <- createCellStyle(wb)
setFillPattern(CellColor, fill = XLC$"FILL.SOLID_FOREGROUND")
setFillForegroundColor(CellColor, color = XLC$"COLOR.YELLOW")
# apply the CellStyle to a given cell, here: (10,10)
setCellStyle(wb, sheet=SHEETNAME, row=10, col=10, cellstyle=CellColor)
# save the workbook
saveWorkbook(wb)
Obviously, the problematic part is
color = XLC$"COLOR.YELLOW"
because it does not let me set the rgb code of the color I like. Attempts like
color = rgb(0.2,0.4,0.8)
fail.
The XLConnect documentation on page 91 does only tell that
The color is normally specified via a corresponding color constant from the XLC object.
There is no explanation on how to use RGB code.

phpexcel fit to selection

I generate an excel file using the great PHPExcel library. At this moment I need to show all the columns into a single page. You know the feeling when you have to scroll left and right up and down to see all the data. Basically I have 33 columns and I need to fit them on any display
I don't know if is my setup or where is the problem, but I can't make it work. This is the code I'm using
$objPHPExcel->getActiveSheet()->getPageSetup()->setFitToPage(true);
$objPHPExcel->getActiveSheet()->getPageSetup()->setFitToHeight(0);
$objPHPExcel->getActiveSheet()->getPageSetup()->setFitToWidth(1);
On MS Excel I select all thw colums then I choose View-> Zoom, then select Fit Selection. How can I do that programatically when excel file is generated with PHPExcel.
i hope i don't understand you wrong, here it goes:
phpExcel has an method called setZoomScalezoom(), however this does not auto fill the whole screen:
example:
$excel = new PHPExcel();
$sheet = $excel->getActiveSheet();
$sheet->getSheetView()->setZoomScale(300);
$writer = new PHPExcel_Writer_Excel5($excel);
$writer->save('test.xls');
got my information from:
source: http://typo3.org/extension-manuals/phpexcel_library/1.7.4/view/5/4/
Setting worksheet zoom level To set a worksheet’s zoom level, the
following code can be used:
$objPHPExcel->getActiveSheet()->getSheetView()->setZoomScale(75);
Note that zoom level should be in range 10 – 400.

Resources