【问题标题】:连接 rangeslider R Shiny 以进行绘图
【发布时间】:2022-01-20 07:34:06
【问题描述】:

因此,一个有情节的情节有一个嵌入式范围滑块,但我不喜欢它的外观。 R Shiny 中的 rangeslider 看起来更好更专业,但是我如何将两者联系起来?

假设您有一个包含一些值和日期范围的数据框,例如:

library(lubridate)

    df <- data.frame(
  "Date" = c(seq(ymd('2015-09-15'), ymd('2015-09-24'), by = "1 days")),
  "values" = c(3,6,5,3,5,6,7,7,4,2)
    )

情节的代码

library(plotly)

    plot_df <- plot_ly(df) 

    plot_df  <- plot_df  %>%  add_lines(type = 'scatter', mode = "lines",
                                      x = ~Date, y = ~values)

代码闪亮

library(shiny)
library(shinydashboard)

  ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(
    # Boxes need to be put in a row (or column)
    fluidRow(
      box(plotlyOutput("plotdf", height = 250)),
      
      box(
        title = "Controls",
        sliderInput("Date", "", min = df$Date[1], tail(df$Date, 1), value = tail(df$Date, 1)
        )
      )
    )
  )
)

server <- function(input, output) {
  output$plotdf<-renderPlotly({
    plot_df
  })
}

shinyApp(ui, server)

【问题讨论】:

    标签: shiny plotly rangeslider bs4dash


    【解决方案1】:

    我们可以使用dplyr::filter 并将其通过管道传递给plot_ly()

        output$plotdf<-renderPlotly({
            filter(df, Date <= input$Date) %>% 
            plot_ly() %>%  
             add_lines(type = 'scatter', mode = "lines",
                                                x = ~Date, y = ~values)
        })
    

    编辑:下面是与应用程序分开的情节代码,带有sliderInput 以选择日期范围。

    library(shiny)
    library(dplyr)
    library(lubridate)
    library(plotly)
    
    source(file = 'my_functions_script.R', local = TRUE)
    
    
    df <- data.frame(
        "Date" = c(seq(ymd('2015-09-15'), ymd('2015-09-24'), by = "1 days")),
        "values" = c(3,6,5,3,5,6,7,7,4,2)
    )
    
    
    library(shiny)
    library(shinydashboard)
    
    ui <- dashboardPage(
        dashboardHeader(title = "Basic dashboard"),
        dashboardSidebar(),
        dashboardBody(
            # Boxes need to be put in a row (or column)
            fluidRow(
                box(plotlyOutput("plotdf", height = 250)),
                
                box(
                    title = "Controls",
                    shiny::sliderInput("Date", "", min = df$Date[1], tail(df$Date, 1), value = c(df$Date[1],tail(df$Date, 1))
                    )
                )
            )
        )
    )
    
    server <- function(input, output) {
        output$plotdf<-renderPlotly({
            filter(df,Date >= input$Date[[1]], Date <= input$Date[[2]]) %>% 
                plt()
            
        })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 太棒了!但是我需要将情节的完整情节代码添加到服务器吗?因为这是一个简单的情节,但我的原始情节要复杂得多,有很多代码......而且我想在 Shiny 中添加各种这些情节......因此服务器代码会很长...... .所以我宁愿将情节情节分开在脚本中并调用它而不是在renderPlotly中
    • @H.berg 是的,我们可以将 plotly 代码包装到一个函数中,该函数接受一个名为 data 的参数,并在需要时在服务器代码中使用它。检查我的编辑。
    • 啊,虽然不是每个情节都是一样的,所以我需要比我想的更多的功能......不可能如此“来源(......脚本的位置等)......至少我试了但没用
    • 您可以将所有函数放在位于工作目录的脚本中,然后在库代码下方调用source(file = 'my_functions_script.R', local = TRUE)。那应该可以。
    • @H.berg 要在滑块中有 2 个值,请将向量 c(value1, value2) 传递给 value 参数。我编辑了代码以包含它。
    猜你喜欢
    • 2017-02-17
    • 2023-04-06
    • 1970-01-01
    • 2020-07-23
    • 2014-08-03
    • 1970-01-01
    • 2021-02-16
    • 2021-12-14
    • 2014-04-19
    相关资源
    最近更新 更多