๐ฉ๐ผโ๐ป How do you set up your own #ShinyApp?
It's easy in #rstats! Start a new #Rproject and select "Shiny Application". It will create a project with an "app.R" file for you โจ
Once it's open, you can replace the code that is already in the "app.R" file with this code snippet below๐ It does all the magic and shows how you can build a simple #ShinyApp ๐ฎ
You have checkboxes on the left side that let you choose countries (it's the ISO3 abbreviation, so "RWA" stands for Rwanda) and, depending on what you selected, your #ShinyApp will show a (non-realistic) population size for each country in a new plot.
When looking at the code, it might be overwhelming at first - but if we think of it as the pseudo-code snippets that I tweeted earlier, things start falling into place ๐ค
But let's start from the beginning โญ
We'll load the essential packages first - we obviously need {shiny} ๐ฆ but I also load {dplyr} for data wrangling, {ggplot2} for visualization, and {overviewR} for the data (you can take any other data here).
In the next step, we load the toy data (toydata) from overviewR and now you're ready to go!
What you now see in code is how you can fill the UI ๐ค and server ๐ง with content.
If you compare the pseudo code with the actual code, you'll see how similar they are. We define the title in "titlePanel()", add a "sidebarPanel()" (this is where the user selects what to see), and a "mainPanel()" (this is where the output lives).
For the sidebarPanel, I picked checkboxes for the selection in the sidebar of the UI ๐ค (but there are multiple other possibilities for more control widgets): shiny.rstudio.com/tutorial/writtโฆ).
To build the connection between the server ๐ง and the UI ๐ค later, we'll do one important thing: we give our selector a unique ID: "countries" (you can see it in the code, but more on this once we're talking about the server ๐ง )
For the mainPanel, we want a plot, so we call "plotOutput("first_plot")". "first_plot" in the parentheses refers to the plot that we later build in the server ๐ง . Think of it as another ID or cross-reference that allows the server ๐ง and the UI ๐ค to speak to each other.
For the server ๐ง , I added a #ggplot2 inside the renderPlot() function to generate the visualization ๐ฉ๐ผโ๐จ
These render* calls are typical parts of the server ๐ง . Just wrap them around your normal {ggplot2} calls (for instance).
And what you also see is that we store our visualization in "output$first_plot" ๐ this is where our "first_plot" is generated that we call later in the UI ๐ค. And there's something else - otherwise, your selection in the UI wouldn't work.
If you look at the filter, you'll see that we filter for countries in "input$countries" ("dplyr::filter(ccode %in% input$countries)"). This is exactly the "countries" ID that we added to the UI ๐ค previously.
And now the magic happens ๐ง๐ปโโ๏ธ
We call "shinyApp (ui = ui, server = server)" and bring the server ๐ง and the UI ๐ค together.
And here's how it "runs" in action! Just highlight the code and let it run - a new window with your first ShinyApp will open ๐
(I wanted to capture the full window so that you get the intuition of what it looks like in practice. That's why it's probably a bit small - but the code remains the same as above โจ )
It's a really simple app and can be further customized - if you want to copy-paste the code to play around with it yourself, it's all in this repository: bit.ly/repo-shinyapp
Some things that I learned when building ShinyApps:
โจ Sketch your ShinyApp on paper (or tablet) with a pencil. This helps to understand what you want to get (and also what's probably missing). It's a bit like doing a design workshop with yourself.
โจ Start simple. Trust me, your ShinyApp will get complicated soon enough ๐
โจ ๐ฆ {echarts4r} is a package for making beautiful visualizations (echarts4r.john-coene.com)
โจ If you're not so much up for writing code, have a look at ๐ฆ {shinyuieditor} - it allows you to create a UI without coding rstudio.github.io/shinyuieditor/โฆ
โจ Deploying a ShinyApp on shinyapps.io is simple (if you don't have any data etc. restrictions) - just follow these steps: bit.ly/deploy-shiny
... and that all the talk about brains and bodies reminds me so much of Liv Moore ๐งโโ๏ธ๐
โข โข โข
Missing some Tweet in this thread? You can try to
force a refresh
And in my last Twitter thread, I wanted to talk with you about some powerful approaches in #NLP and how we can use both #rstats and #python to unleash them ๐ช
One possible downside when using the bag of words approach described before is that you often cannot fully take the structure of the language into account (n-grams are one way, but they are often limited).
You also often need many data to successfully train your model - which can be time-consuming and labor intensive. An alternative is to use a pre-trained model. And here comes @Google's famous deep learning model: BERT.
The curation week is almost over and I would like to thank everyone for joining the discussions this week! Itโs been a blast ๐ฅณ
If you enjoyed this week, feel free to reach out on Twitter (@cosima_meyer) or GitHub (github.com/cosimameyer/) โจ
@cosima_meyer I feel very honored that I had the chance to talk with you about the things I enjoy doing and I cannot wait to learn more from the upcoming curators - the lineup looks amazing! ๐
@cosima_meyer If you missed a Twitter thread this week, head over to @pilizalde's amazing thread where she collected all of them (I love the GitHub emoji ๐บ)
๐ก What is reactivity and what does it have to do with a carrier pigeon? ๐ฆ
To better understand how a #ShinyApp works, it's good to understand what's behind reactivity.
To describe it, I love the image of a carrier pigeon ๐ฆ (I picked up this idea when reading a post by @StatGarrett - so all credits go to him and all errors are mine โจ)
@StatGarrett What reactivity does is "a magic trick [that] creates the illusion that one thing is happening, when in fact something else is going on" (shiny.rstudio.com/articles/underโฆ).
Today, we'll discover how you can use the power of #rstats to create an interactive #shinyapp โจ
๐ก What is a ShinyApp?
Shiny is a framework that allows you to create web applications - ShinyApps โบ๏ธ You can use them for multiple purposes - to visualize data ๐จ (for instance the Scottish Household Survey by @ViktErik, bit.ly/3TqZevY, ...