R 使用动态输入字段绘制交互式图表

R 使用动态输入字段绘制交互式图表,r,shiny,r-plotly,R,Shiny,R Plotly,我正在尝试创建一个具有下拉列表的图表,用户可以在其中选择要在图表上显示的内容,就像Power BI图表中的“过滤器”,但我希望保留在R中。因此,我将使用Shiny和Plotly,欢迎提供任何其他建议 大多数教程都指向硬编码输入选择,但我有数千个选择,因此我希望从数据源中的字段动态生成此列表。这是我到目前为止的一个示例,我希望下拉列表可以让用户在Home 1和Home 2之间进行选择: library(shiny) library(plotly) df <- data.frame(Name

我正在尝试创建一个具有下拉列表的图表,用户可以在其中选择要在图表上显示的内容,就像Power BI图表中的“过滤器”,但我希望保留在R中。因此,我将使用Shiny和Plotly,欢迎提供任何其他建议

大多数教程都指向硬编码输入选择,但我有数千个选择,因此我希望从数据源中的字段动态生成此列表。这是我到目前为止的一个示例,我希望下拉列表可以让用户在Home 1和Home 2之间进行选择:

library(shiny)
library(plotly)

df <- data.frame(Name = c("Home 1", "Home 1", "Home 2", "Home 2"), Rating = c("Good", "Excellent", "Good", "Good"),
                     Date = c("2016-07-14", "2016-08-14", "2016-07-14", "2016-08-14"))


# Get a basic shiny app working with dropdowns
ui <- fluidPage(
  plotlyOutput("plot"),
  selectInput(df$Name, "Select Name", df$Name),
  verbatimTextOutput("event")
)

server <- function(input, output) { 
  output$plot <- renderPlotly({
    plot_ly(df, x = df$Date, y = df$Rating, type = 'scatter', mode = 'line')
  })

  output$event <- renderPrint({
    d <- event_data("plotly_hover")
    if (is.null(d)) "Hover on a point!" else d
  })
}

shinyApp(ui, server)
我觉得我已经很接近了,但我已经筋疲力尽了,基本上需要一个比我更擅长的人来为我指明正确的方向

或者除了shiny&plotly之外,还有更好的方法使用软件包吗


谢谢

以下是我的做法:

library(shiny)
library(plotly)

df <- data.frame(Name = c("Home 1", "Home 1", "Home 2", "Home 2"), Rating = c("Good", "Excellent", "Good", "Good"),
                 Date = c("2016-07-14", "2016-08-14", "2016-07-14", "2016-08-14"))

# Get a basic shiny app working with dropdowns
ui <- fluidPage(
  plotlyOutput("plot"),
  selectInput("SelectName", "Select Name", df$Name, selected = unique(df$Name), multiple = TRUE),
  verbatimTextOutput("event")
)

server <- function(input, output) { 

  filteredDf <- reactive({
    req(input$SelectName)
    df[df$Name %in% input$SelectName, ]
  })

  output$plot <- renderPlotly({
    plot_ly(filteredDf(), x = ~Date, y = ~Rating, type = 'scatter', mode = 'line', color = ~Name)
  })

  output$event <- renderPrint({
    d <- event_data("plotly_hover")
    if (is.null(d)) "Hover on a point!" else d
  })
}

shinyApp(ui, server)

您需要从df创建一个反应式过滤数据集,我称之为filteredDf

Nice!但是选定的输入对图表没有影响?第一次呈现图表时,两个主视图都会显示,然后当我选择一个特定的主视图时,图表不会发生任何变化。它确实会对图表产生影响。尝试使用退格键取消选择或删除selectInput中的项目。
library(shiny)
library(plotly)

df <- data.frame(Name = c("Home 1", "Home 1", "Home 2", "Home 2"), Rating = c("Good", "Excellent", "Good", "Good"),
                 Date = c("2016-07-14", "2016-08-14", "2016-07-14", "2016-08-14"))

# Get a basic shiny app working with dropdowns
ui <- fluidPage(
  plotlyOutput("plot"),
  selectInput("SelectName", "Select Name", df$Name, selected = unique(df$Name), multiple = TRUE),
  verbatimTextOutput("event")
)

server <- function(input, output) { 

  filteredDf <- reactive({
    req(input$SelectName)
    df[df$Name %in% input$SelectName, ]
  })

  output$plot <- renderPlotly({
    plot_ly(filteredDf(), x = ~Date, y = ~Rating, type = 'scatter', mode = 'line', color = ~Name)
  })

  output$event <- renderPrint({
    d <- event_data("plotly_hover")
    if (is.null(d)) "Hover on a point!" else d
  })
}

shinyApp(ui, server)