RObservations #46: Starmaps and Shiny- I Replicated an E-Commerce Store Using My Custom Package, starBliss

[This article was first published on r – bensstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

Recently, I released a R package called starBliss that aimed to replicate the output of a e-commerce site called MapsForMoments – a site which lets users order custom prints of the night sky on the date of their choosing (usually a special occasion such as a birthday, first date, wedding etc.) and allows them to choose a style, and add some custom text. It was a great experience getting to build the package which replicated the MapsForMoments product and I was shocked to see how well it was received when I posted about it- with the Github receiving over 30 stars at the time of writing this blog!

I decided to take this to the next level by trying to build a similar UI in shiny which allows the user to create a custom star map and not need to use the R console. In this blog I share my experience constructing and showcase the “free alternative” to MapsForMoments – starBlissGUI!

Building the App

Building an app for starBliss presented some interesting challenges. For one, the plot output created by the plot_starmap() function is quite unwieldy. Previewing the image requires a user to have a large enough screen size and does not shrink well. The present workaround in the starBliss package is to save the plot as an image that accommodates the appropriate sizing like so:

# install.packages("ggplot2")
# devtools::install_github("benyamindsmith/starBliss")

library(ggplot2)
library(starBliss)

p<- plot_starmap(location= "Toronto, ON, Canada",
             date="2022-01-17",
             style="black",
             line1_text="Toronto",
             line2_text ="January 17th, 2023",
             line3_text="43.6532° N, 79.3832° W")

ggsave('toronto_black.png', plot = p, width = unit(10, 'in'), 
       height = unit(15, 'in'))

For starBlissGUI a temporary image is created in the server and displayed on the preview panel. The only drawback with this approach is that does create some lag on cheaper hardware and on the free tier of Shinyapps.io. You can check out the source code on the Github here and the hosted app on Shinyapps.io here (apologies for the lag, as it is being hosted on the free tier).

Building a Docker Image

Since getting a paid plan from ShinyApps.io was not something that I was particularly interested in perusing if I were to host this app professionally, I decided to explore the Docker route. With the help of Google, Stackoverflow and yes, ChatGPT (lots of prompts!) I was able to put together the following script:

FROM rocker/shiny:latest

RUN apt-get update && apt-get install -y \
    libssl-dev \
    libcurl4-gnutls-dev \
    libxml2-dev \
    libudunits2-dev \
    libgdal-dev

RUN R -e 'install.packages(c("shiny","shinyWidgets","ggplot2","lubridate", "remotes"))'
RUN R -e 'remotes::install_github("benyamindsmith/starBliss")'

COPY app.R /srv/shiny-server/
COPY www /srv/shiny-server/www

EXPOSE 3838

CMD ["R", "-e", "shiny::runApp('/srv/shiny-server/',, host='0.0.0.0', port=3838)"]

One of the things that is important to note if you are installing packages which are hosted on github, you need to make sure that the libudunits2-dev and libgdal-dev libraries are installed in the container otherwise it will not work.

Conclusion

While I played around with shiny before, I never tried to build anything serious with it until now. It was a very useful experience getting to thoroughly build the app. While there is still some functionality that I would like to improve on (like autofill for the location field, optimizing the server side and making a form to collect some user data before downloading the map for free), I am happy to share what I have right now!

Please be sure to check out the starBlissGUI GitHub and give it a star and open an issue if you see anything that you want to have added!

Thank you for reading!

Want to see more of my content?

Be sure to subscribe and never miss an update!

To leave a comment for the author, please follow the link and comment on their blog: r – bensstats.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)