I have a quick question. I have written an app that I run on shinyapps.io. Part of the page generates a graph, through ggplot2, that I offer users the ability to download.
When exporting the file, the image is stretched right to the borders of the overall png size. My titles, x/y axis titles and the legend are all right against the edge of the frame.
Is there any quick way to either center the image by scaling it inwards from the frame of the page, or add padding and/or margins to the entire graph image?
Here is my export code:
output$downloadPlot <- downloadHandler(
filename = function() { paste(input$get.id," _Chart", '.png', sep='') },
content = function(file) {
device <- function(..., width, height) grDevices::png(..., width = width, height = height, res = 300, units = "in")
ggsave(file, plot = p(), device = device,scale=1.95)
}
)
Thank you in advance!
Related
I was working on a Shiny application, and instead of a title, I wanted to use an image. While the code did not raise any errors and shows the designated area for the image, the image doesn't show up.
Here is the code:
# header UI
ui <- navbarPage(title = div(img(src="/Users/atillacolak/Desktop/tafn_logo.png",
height = 60,
style = "margin-top: -14px; padding-right:10px;padding-bottom:10px")),
selected = "home",
theme = bs_theme(
bg = "white",
fg = "#ed7117",
base_font = font_google("Prompt"),
code_font = font_google("JetBrains Mono")),
fluid = TRUE,
home)
The result of this code is given in the image below.
As you can see the image does not format. What is the problem here?
You need to make the image available as a static resource to shiny's webserver.
Either you put the image in a www folder (subdirctory of your app folder) and set src = "/tafn_logo.png" or you use addResourcePath:
addResourcePath(prefix = "Desktop", directoryPath = "/Users/atillacolak/Desktop")
src using prefix:
img(src="Desktop/tafn_logo.png",
height = 60,
style = "margin-top: -14px; padding-right:10px;padding-bottom:10px")
See my related answer here.
I know how to put a number of plots side by side within a html output using Rmarkdown
{r maps, fig.cap = "Fig. Z - Maps", fig.show="hold", out.width="25%", fig.align = "center", echo = FALSE}
knitr::include_graphics("plots/X1.map.png")
knitr::include_graphics("plots/X2.map.png")
knitr::include_graphics("plots/X3.map.png")
knitr::include_graphics("plots/X4.map.png")
Each of these maps have been created and saved using tmap function, saving each map with the same height and width:
tmap_save(X1.map, "plots/X1.map.png", height = 4, width = 1.5)
However, when knitting the html document together the maps, although having the same widths have different heights.
Any help would be much appreciated.
Have you tried setting the size of your plots in your global chunk options? This is probably not going to work if you don't have the code to those plots instead of the saved png's.
knitr::opts_chunk$set(fig.width=x, fig.height=y)
Alternatively if you only have access to the png's you can try this:
knitr::include_graphics("plots/X1.map.png"){ width=z% }
knitr::include_graphics("plots/X2.map.png"){ width=z% }
knitr::include_graphics("plots/X3.map.png"){ width=z% }
knitr::include_graphics("plots/X4.map.png"){ width=z% }
Replace the x, y, and z with the values you'd like.
In a word document, I want single spaced footnotes while the body of the document is 1.5 spaced.
No matter what I try, the footnote spacing stays at 1.15 spacing instead of single spacing.
I've tried changing the style of the footnote and changing the padding around each line of the footnote (treating each as a different paragraph). Styles don't seem to work on footnotes and changing the padding to zero still left the default 1.15 space between each "paragraph."
My current code:
footnote1 = Footnote()
footnote1 = addParagraph( footnote1,
value = pot("footnote text",
textProperties(font.size = 10, font.family = "Arial"))
)
doc <- addParagraph( doc,
value = "\tparagraph text" +
pot("addfootnotehere.", footnote = footnote1 ) +
"more text.",
stylename = "NormalLeft",
bookmark = "paragraph1"
)
Your problem may be fixed if you used your own created template instead of the default doc <- docx().
Set up a template with the spacing required, save it, then call the newly created template with:
doc <- docx(template = 'D:/docs/template/my_corporate_template.docx')
then addFootnote should read your template sizing without auto correcting.
I use the library (tcltk) in R to make the GUI. Code to create a button is :
tt <- tktoplevel()
button.widget <- tkbutton(tt, text = "", command = function())
I want the existing button on the GUI has an icon. How is the code to add the icon in the button using the library (tcltk) in R?
Use the tkimage.create function to create a Tcl-level representation of an image file. Note that there are some limitations on image formats and I believe GIF is the easiest to work with. Then, you specify the image as the image argument to tkbutton. Here's an example using, an Example.gif from Wikipedia:
library("tcltk")
img <- tclVar()
tclimg <- tkimage.create("photo", img, file = "Example.gif")
tt <- tktoplevel()
button.widget <- tkbutton(tt, text = "Click me!", image = tclimg, compound = "top",
command = function() tkmessageBox(message = "Hello!"))
tkgrid(button.widget)
You can control the relative placement of the image and text on the button using the compound argument:
"none" (display only the image if present, otherwise the text; the default)
"text" (text only)
"image" (image only)
"center" (text in center of image)
"top" (image above text)
"left" (image left of text)
"bottom" (image below text)
"right" (image right of text)
Working on allowing the upload of images which can range in a variety of size, then allowing to crop a predefined area of the image for a thumbnail.
The thumbnail size is predefined to 150x150. Using the Jcrop.js tool to select a section of the image.
Problem:
When displaying the uploaded image in a smaller size than the original image by implementing set height/width on the image rendered, then there is a scale factor that comes into play when selecting an area to crop.
You either have to scale down the cropping area proportionately or you have to scale the image in relation to the actual image's size in comparison to its displayed size.
Question:
How do I figure out the scale of the browser displayed image vs. original image? I am currently using the following code to save the image, but how would I take into consideration the scaling?
public static Image CropImage(Image originalImage, int x, int y, int width, int height)
{
var bmp = new Bitmap(width, height);
bmp.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
using (var graphic = Graphics.FromImage(bmp))
{
graphic.SmoothingMode = SmoothingMode.AntiAlias;
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.PixelOffsetMode = PixelOffsetMode.HighSpeed;
graphic.DrawImage(originalImage, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);
return bmp;
}
}
Bonus Question:
Another problem I discovered, is that there seems to be no efficient way to transfer the original file's ImageFormat when creating a new Bitmap which creates a ImageFormatMemoryBMP and when you attempt to call Bitmap.Save(memorystream, original rawformat) it will blow up. And bitmap RawFormat has no setter.
So how can you set the format on a new bitmap?
I think perhaps that this problem is solved purely on the front end, no need to use any server side for this.
Jcrop has a built in scale factor handler.
http://deepliquid.com/content/Jcrop_Sizing_Issues.html
Now you can use this in two ways, as I understand it. Either to 'resize' the image for you on the front end using 'box sizing', or you can tell it the 'truesize' of the image and it will work out the scale factor and handle the coordinates for you on it's own.
Box sizing
$('#cropbox').Jcrop({ boxWidth: 450, boxHeight: 400 });
True Size
$.Jcrop('#cropbox',{ trueSize: [500,370] });
Using the true size method you will need to invoke jcrop using the api method:
http://deepliquid.com/content/Jcrop_API.html#API_Invocation_Method
var jcrop_api,
options = { trueSize: [500,370] };
$('#target').Jcrop(options,function(){
jcrop_api = this;
});
Good luck!