在闪亮的应用程序中使用 sliderInput 为区域着色

Tar*_*Jae 4 graphics r slider background-color shiny

我有一个改编自网络的小示例应用程序:

library( shiny )
library( shinyWidgets )

  ui <- fluidPage(
    
    tags$br(),
    
    noUiSliderInput(
      inputId = "noui2", label = "Slider vertical:",
      min = 0, max = 1000, step = 50,
      value = c(100, 400), margin = 100,
      orientation = "vertical",
      width = "100px", height = "300px"
    ),
    verbatimTextOutput(outputId = "res2")
    
  )
  
  server <- function(input, output, session) {
    
    output$res2 <- renderPrint(input$noui2)
    
  }
  
  shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

然后我加载图像作为背景,如下所示:

在此输入图像描述

我想知道是否有一种方法可以对图形边框中 100 到 400 之间的特定区域高度(由滑块给出)进行着色,如下所示:

在此输入图像描述

ism*_*gal 5

下面请找到一种使用plotly的填充面积图的方法:

library(shiny)
library(plotly)
library(shinyWidgets)

DF <- data.frame(
    x = c(cos(seq(0.01, 10, 0.01)) * 1000:1 + 1000, cos(seq(0.01, 10, 0.01)) * 1000:1 + 1500),
    y = rep(1:1000, 2),
    id = c(rep("trace_1", 1000), rep("trace_2", 1000))
  )

ui <- fluidPage(
  br(),
  column(
    2,
    noUiSliderInput(
      inputId = "noui2",
      label = "Slider vertical:",
      min = 0,
      max = 1000,
      step = 50,
      value = c(100, 400),
      margin = 100,
      orientation = "vertical",
      direction = c("rtl"),
      width = "100px",
      height = "350px"
    )
  ),
  column(4, plotlyOutput("plot")),
  verbatimTextOutput(outputId = "res2")
)

server <- function(input, output, session) {
  output$res2 <- renderPrint(input$noui2)
  
  plotDF <- reactive({
    plotDF <- DF[DF$y %in% input$noui2[1]:input$noui2[2], ]
    plotDF$id <- paste0("filtered_", plotDF$id)
    plotDF
  })
  
  output$plot <- renderPlotly({
    fig <- plot_ly(
        rbind(DF, plotDF()),
        x = ~ x,
        y = ~ y,
        split = ~ id,
        type = "scatter",
        mode = "lines",
        color = I("black"),
        fillcolor = 'red',
        showlegend = FALSE
      ) |> style(fill = 'tonexty', traces = 2)
  })
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

结果