【问题标题】:Linking the layout of a sliderInput and a graph链接滑块输入和图形的布局
【发布时间】:2018-08-02 01:53:39
【问题描述】:

我有一个 selectInput 用于在 sidebarPanel( 或 fluidRow(column(4,) ) 中指定 sliderInput。sliderInput 从数据集中获取其最小值和最大值,并用于指定图形。由于数据不是等距的,可以选择没有数据的间隔。我想要一个很好的方法来显示数据的位置。下面我做了一些示例代码(没有所需的图表)并附上了一张图片我想要什么(或认为我想要。我愿意接受建议)。我尝试在滑块下摆弄一个流畅的行和 renderPlot,但我没有设法让它们对齐。(这样布局仍然看起来不错当浏览器重新缩放时)。 最好我希望新图是 ggplot2。

更新

已根据 divibisan 的建议更新了代码。我仍然无法正确对齐。

    library(shiny)
library(ggplot2)
# data
df = data.frame("sub1" = c(1,10), "sub2" = c(5,90))
sub1_val = data.frame("x" = c(seq(1,4),seq(9,10)), "y" = rnorm(6))
sub2_val = data.frame("x" = c(seq(5,40),seq(85,90)), "y" = rnorm(42))
#
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fluidRow(
        selectInput("sel",label = "Choose subject",
                    choices = names(df),
                    selected = "sub2")
        ,uiOutput("subjectUI")
      ),
      plotOutput("selPlot", width = "75%", height = "100px")
    ),
  mainPanel(
    h1("Let there be stuff"),
    plotOutput("plot")
  )
)
)
server <- function(input, output,session) {

  output$subjectUI <- renderUI({ 
       sliderInput("slide","This is a slide",
                         min = df[input$sel][1,],
                         max = df[input$sel][2,],
                         value = c(min,max),
                         width = "75%"
             )
    })
  output$selPlot <- renderPlot({
    switch(input$sel,
           "sub1" = if(!is.null(input$slide[2])){plotData = data.frame(xVar = seq(df[input$sel][1,],df[input$sel][2,]));plotData["yVar"] = 1*(plotData$xVar %in% sub1_val$x) },
           "sub2" = if(!is.null(input$slide[2])){plotData = data.frame(xVar = seq(df[input$sel][1,],df[input$sel][2,]));plotData["yVar"] = 1*(plotData$xVar %in% sub2_val$x) })
    if(!is.null(input$slide[2])){
      ggplot(data = plotData, aes(x = xVar, y = yVar)) +
        geom_point() + geom_line() +
        geom_vline(xintercept = input$slide[1]) +
        geom_vline(xintercept = input$slide[2]) +
        labs(x = "", y="")  +
        scale_x_continuous(expand=c(0,0)) + 
        theme(axis.title.y = element_blank(),
              axis.text.y = element_blank(),
              axis.ticks.y = element_blank())
    }
    })
  #
  output$plot <- renderPlot(
    switch(input$sel,
           "sub1" = if(!is.null(input$slide[2])){vec = sub1_val$x>= input$slide[1] & sub1_val$x <= input$slide[2];plot(sub1_val$x[vec],sub1_val$y[vec],type ="b")},
           "sub2" = if(!is.null(input$slide[1])){vec = sub2_val$x>= input$slide[1] & sub2_val$x <= input$slide[2];plot(sub2_val$x[vec],sub2_val$y[vec],type ="b")}
           )
  )
}
shinyApp(ui = ui, server = server)

How I imagine a solution could look

My second attempt

【问题讨论】:

    标签: shiny


    【解决方案1】:

    我将使用所有数据渲染第二个图,然后让input$slide[1]input$slide[2] 更改标记所选区域的两条垂直线的位置。我在 ggplot 中编写了它,因为我不太了解您是如何生成绘图的,但大概使用该系统在绘图上绘制一条垂直线很简单。像这样的:

    output$plot2 <- renderPlot({
        ggplot(data = DATA, aes(x = XVAR, y = YVAR)) +
            geom_point() + geom_line() +
            geom_vline(xintercept = input$slide[1]) +
            geom_vline(xintercept = input$slide[2])
    })
    

    【讨论】:

    • 我尝试实施您的建议,但问题是情节与幻灯片无关,因此当您更改浏览器窗口时,它们似乎并没有以完全相同的方式发生变化。此外,即使我剥离了所有图例的情节,情节的左侧也不与滑块对齐。
    • 你说的是页面元素的对齐和定位吗?使用闪亮实际上很漂亮,因为每个闪亮标签通常会使用自己的 CSS 生成几个 HTML 标签。对于这个问题,您可以尝试为两个元素指定一个绝对宽度并使用它,直到它们按照您想要的方式排列。不幸的是,要让它们排列得恰到好处,需要对 CSS 进行一些处理。
    • 是的,对齐是问题所在。由于我的 CSS 技能有些有限,我希望有一个闪亮的解决方案。如果您有任何 CSS 建议,请随时分享。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多