【问题标题】:Widget position when using rmarkdown for shiny使用 rmarkdown 进行闪亮时的小部件位置
【发布时间】:2015-08-09 16:29:10
【问题描述】:

我正在通过在 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

我希望两个滑块并排或绘图在右侧,滑块在左侧。这可能吗?

【问题讨论】:

    标签: r shiny


    【解决方案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)
             )
      )
    

    【讨论】:

    • 可爱,谢谢。顺便问一下,你会建议我学习如何使用 ui.R 和 server.R 构建应用程序的方式吗?看起来你有更多的控制权?
    猜你喜欢
    • 2018-07-25
    • 2023-03-25
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    • 2019-07-28
    • 2021-01-14
    相关资源
    最近更新 更多