Pete900
Pete900

Reputation: 2166

Widget position when using rmarkdown for shiny

I am building an "interactive document" by writing code in rmarkdown and using a shiny runtime. Everything works fine but I would like to control the widget positions because I end up with lots of white space. There are plenty of useful pages describing how to do this when using a ui.R and server.R:

http://shiny.rstudio.com/articles/layout-guide.html

shiny 4 small textInput boxes side-by-side

However, in rmarkdown you do not define a "shinyUI" (perhaps I am wrong). How do I control the position of widgets / plots in rmarkdown with shiny runtime? Here is an example dataset:

---
title: "Yield5"
author: "P Downs"
date: "Tuesday, May 26, 2015"
output: html_document
runtime: shiny
---

# Create user input for reactive subsetting
```{r echo=FALSE}
sliderInput("MeasA_L", label = "Measure A lower bound:",
            min=2, max=9, value=3, step=0.1)

sliderInput("MeasA_U", label = "Measure A upper bound:",
            min=2, max=9, value=8, step=0.1)

sliderInput("MeasB_L", label = "Measure B lower bound:",
            min=2, max=9, value=3, step=0.1)

sliderInput("MeasB_U", label = "Measure B upper bound:",
            min=2, max=9, value=8, step=0.1)

# create reactive variables for use in subsetting below

MAL <- reactive({input$MeasA_L})
MAU <- reactive({input$MeasA_U})

MBL <- reactive({input$MeasB_L})
MBU <- reactive({input$MeasB_U})

```

# Create example data frame. Measurement is grouped by batch and ID number
```{r echo=FALSE, message=FALSE}

library(plyr)
library(ggplot2)
library(dplyr)


set.seed(10)

MeasurementA <- rnorm(1000, 5, 2)
MeasurementB <- rnorm(1000, 5, 2)

ID <- rep(c(1:100), each=10)
Batch <- rep(c(1:10), each=100)

df <- data.frame(Batch, ID, MeasurementA, MeasurementB)

df$ID <- factor(df$ID)
df$Batch <- factor(df$Batch)

# function used to count number of "passed" data based on user input from sliders i.e. how many data points are between ML and MU

countFunc <-  function(x, y, MAL, MAU, MBL, MBU) sum( (x > MAL()) & (x < MAU()) & (y > MBL()) & (y < MBU()) )

# user dplyr to count produce summary of count for: total data, passed data, then calculate % yield 

totals <- reactive({
  df %>% group_by(Batch, ID) %>%
  summarize(total = length(MeasurementA), x = countFunc(MeasurementA, MeasurementB, MAL(), MAU(), MBL(), MBU())) %>%
  mutate(Yield = (x/total)*100) %>%
  as.data.frame()
  })

# Plot yield by against ID number grouped by batch

renderPlot({ggplot(totals(), aes(ID, Yield, colour=Batch)) + geom_point() +
              scale_y_continuous(limits=c(0,100))})

and the resulting working html:

https://pragmaticprinting.shinyapps.io/Yield5/Yield5.Rmd

I would like two sliders to be side by side OR the plot to be on the right with sliders on the left. Is this possible?

Upvotes: 0

Views: 841

Answers (1)

shadow
shadow

Reputation: 22293

You can get two sliders side by side using fluidRow and column. Just replace the sliderInput section of your code with the following:

fluidRow(
  column(6, 
         sliderInput("MeasA_L", label = "Measure A lower bound:",
                     min=2, max=9, value=3, step=0.1)
         ), 
  column(6, 
         sliderInput("MeasA_U", label = "Measure A upper bound:",
                     min=2, max=9, value=8, step=0.1)
         )
  )
fluidRow(
  column(6, 
         sliderInput("MeasB_L", label = "Measure B lower bound:",
                     min=2, max=9, value=3, step=0.1)
         ), 
  column(6, 
         sliderInput("MeasB_U", label = "Measure B upper bound:",
                     min=2, max=9, value=8, step=0.1)
         )
  )

Upvotes: 1

Related Questions