I’ve (finally) started using blogdown: http://riinu.netlify.com
I will convert all WordPress posts and move them into blogdown shortly…
I’ve (finally) started using blogdown: http://riinu.netlify.com
I will convert all WordPress posts and move them into blogdown shortly…
What is Shiny?
Shiny is an R package (install.packages("shiny")
) for making your outputs interactive. Furthermore, Shiny creates web apps meaning your work can be shared online with people who don’t use R. In other words: with Shiny, R people can make websites without ever learning Javascript etc.
I am completely obsessed with Shiny and these days I end up presenting most of my work in a Shiny app.
Your first Shiny app
Getting started with Shiny is actually a lot easier than a lot of people make it out to be. So I created a very short (9 slides) presentation outlining my 5-step programme for your first Shiny app.
This is the app: https://riinu.shinyapps.io/shiny_testing/
This is the presentation: http://rpubs.com/riinu/shiny
And here are the steps (also included in the presentation):
STEP 0: install.packages("shiny")
. Use RStudio.
STEP 1: Create a script called app.R
using this skeleton:
library(shiny) | |
library(tidyverse) | |
library(gapminder) | |
# User Interface | |
ui <- basicPage( | |
plotOutput(outputId = "myplot") | |
) | |
# Server | |
server <- function(input, output) { | |
output$myplot <- renderPlot({ | |
#your plot code here: | |
}) | |
} | |
shinyApp(ui, server) |
STEP 2: Copy your plot code into the renderPlot function.
STEP 3: Add a sliderInput
to your User Interface (ui
). A slider is just one of the many Shiny widgets you could be using: https://shiny.rstudio.com/gallery/widget-gallery.html
STEP 4: Tell your Server you wish the dplyr::filter()
to use the value from the slider. All inputs from the User Interface (ui
) are stored in input$variable_name
: replace the 2007
with input$year
.
STEP 5 (optional): Add animate = TRUE.
Press Control+Shift+Enter
or the “Run App” button. You now have a Shiny app running on your computer. To deploy it to the internet, e.g. like I’ve done in the link above, see this.
With a simple combination of mutate_if
and fct_explicit_na,
you can replace all NAs in all factors with “Missing”:
If some of the words/symbols appear in white font, see the example here: Gist: factor_NA_levels.R
library(dplyr) # gives mutate_if
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(forcats) # gives fct_explicit_na
#example dataframe, a and c are factors,
#b is numeric, d is boolean (TRUE/FALSE)
mydata = data.frame(
a = c( 'Yes', 'No', NA),
b = c( 0.5, NA, 0.6),
c = c( 'No', NA, 'Yes'),
d = c( TRUE, NA, FALSE)
)
# Making the missing fields in the columns which are factors explicit
#(by default, fct_explicit_na changes NAs "(Missing)"):
newdata1 = mydata %>%
mutate_if(is.factor, fct_explicit_na)
# changing the missing label to "Dunno"
#(note how the syntax is a little bit different
# than when using fct_explicit_na on a single column)
newdata2 = mydata %>%
mutate_if(is.factor, fct_explicit_na, na_level = 'Dunno')
# on a single column it would look like:
mydata$a %>% fct_explicit_na(na_level = 'Dunno')
#> [1] Yes No Dunno
#> Levels: No Yes Dunno
mydata
#> a b c d
#> 1 Yes 0.5 No TRUE
#> 2 No NA <NA> NA
#> 3 <NA> 0.6 Yes FALSE
newdata1
#> a b c d
#> 1 Yes 0.5 No TRUE
#> 2 No NA (Missing) NA
#> 3 (Missing) 0.6 Yes FALSE
newdata2
#> a b c d
#> 1 Yes 0.5 No TRUE
#> 2 No NA Dunno NA
#> 3 Dunno 0.6 Yes FALSE
dplyr
reference: http://dplyr.tidyverse.org/reference/index.html
forcats
reference: http://dplyr.tidyverse.org/reference/index.html
To create a .bib file that only includes the citations you used in the manuscript:
bibexport -o extracted_file.bib manuscript.aux
There are a few issues with this though. The command bibexport comes with the installation of TexLive, but my Windows computer (bless) does not cooperate (“bibexport is not recognised as an internal or external command…”) . So I can only use it on my Mac (luv ya).
ggplot includes built in and seamless functionality that summarises your data before plotting it. As shown in the example below, ggplot_build() can be used to access the summarised dataset.
library(ggplot2) | |
p = ggplot(diamonds, aes(x = color, fill=cut(price, | |
breaks = quantile(price), | |
include.lowest=TRUE, | |
labels = c('Expensive', | |
'More expensive', | |
'Very expensive', | |
'Ridiculously expensive')))) + | |
geom_bar(position='fill') + | |
facet_wrap(~cut) + | |
scale_fill_brewer('Price', palette='OrRd') + | |
theme(legend.position='top') | |
p | |
p_build = ggplot_build(p) | |
plot_data = as.data.frame(p_build$data) | |
head(plot_data) |
fill y count prop x PANEL group ... #D7301F 0.2147239 35 1 1 1 4 ... #FC8D59 0.6871166 77 1 1 1 3 ... #FDCC8A 0.9570552 44 1 1 1 2 ... #FEF0D9 1.0000000 7 1 1 1 1 ... #D7301F 0.1696429 38 1 2 1 8 ... #FC8D59 0.6116071 99 1 2 1 7 ... ...