使用rmarkdown闪亮时的小部件位置

时间:2015-05-27 14:08:54

标签: r shiny

我正在通过在rmarkdown中编写代码并使用闪亮的运行时来构建“交互式文档”。一切正常,但我想控制小部件位置,因为我最终有很多空白区域。有很多有用的页面描述了在使用ui.R和server.R时如何执行此操作:

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

shiny 4 small textInput boxes side-by-side

然而,在rmarkdown中你没有定义“shinyUI”(也许我错了)。如何使用闪亮的运行时控制rmarkdown中小部件/绘图的位置?这是一个示例数据集:

---
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))})

以及生成的工作html:

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

我希望两个滑块并排或左侧的滑块位于右侧。这可能吗?

1 个答案:

答案 0 :(得分:1)

您可以使用fluidRowcolumn并排获得两个滑块。只需使用以下代码替换代码的sliderInput部分:

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)
         )
  )