带有 Plotly 的 R Shiny 中的复选框组输入

Checkboxgroupinput in R Shiny with Plotly

喜欢这里所有的帮助,但不得不自学 R / Shiny,办公室里没有人帮忙,不幸的是我又被困住了!!

我正在尝试在 Shiny 中创建复选框组。我读了一些 quite,例如 , , this, and this 已经有所帮助,但我现在卡住了。

所以我的数据集 "ConversionsCompletions" 现在看起来像这样:

 date     |   goal1completions | goal
--------------------------------------------
01012016  |         4          | goal1
01012016  |        10          | goal2 
01012016  |         8          | goal3
01012016  |        13          | goal4 
02012016  |         6          | goal1
02012016  |         7          | goal2   
.....

ui:

                          checkboxGroupInput("checkGroup", label = h3("Goal"), 
                                             choices = c("Goal 1" = "goal1",
                                                            "Goal 2" = "goal2", 
                                                            "Goal 3" = "goal3",
                                                            "Goal 4" = "goal4",
                                                            "Goal 5" = "goal5",
                                                            "Goal 6" = "goal6",
                                                            "Goal 7" = "goal7"),
                                             selected = "Goal 1")

 plotlyOutput("Conversionrate1")

服务器:

filteredData <- reactive({ 

  filter(ConversionsCompletions[ConversionsCompletions$goal %in% input$checkGroup,])
})


 output$Conversionrate1 <- renderPlotly({
 plot_ly(filteredData(), x = ConversionsCompletions$date, y = ConversionsCompletions$goal1Completions, mode = "lines + markers", hoverinfo = y) 


})

有一个图表,但当我切换框时它没有改变,或者一次显示多条线。我知道通常你需要为 plotly 图表添加 "add_trace" 代码,所以我不确定在这种情况下该怎么做,有时是一行,有时是多行。

感谢任何帮助!!

要正确呈现图形,您必须使用 filteredData() 并稍微更改语法。

作为第一个参数 data,您应该使用过滤后的数据集,然后为 xy 变量使用带有前缀 ~ 的适当名称。

要绘制多条线,您可以使用另一个参数 split。我不得不将 hoverinfo = y 更改为 hoverinfo = "y" 否则它不起作用(我有最新版本的 plotly)

plot_ly(
      data = filteredData(),  
      x = ~date,
      y = ~goal1completions,
      split = ~goal,
      mode = "lines + markers",
      hoverinfo = "y" # "y" instead of y ... at least in the newest version
    ) 

我还使用了 setNames 函数来缩短 checkboxGroupInput 的代码。

setNames(object = paste0("goal", 1:7), 
         nm = paste0("Goal ", 1:7))

您不需要 dplyr 函数 filter 进行子集化 - 至少在这种情况下是这样。


已编辑:

我把数值变量date转换成了date格式:

ConversionsCompletions <- read.table("~/Downloads/data", header = T)
d <- as.character(ConversionsCompletions$date)
d <-  paste0(substr(d, 0, 2), "-", substr(d, 3, 4), "-", substr(d, start = 4,          7))
ConversionsCompletions$date <- as.Date(d, format = "%d-%m-%Y")

完整示例:

library(shiny)
library(plotly)

rm(ui) ; rm(server)

# use example data
ConversionsCompletions <- read.table("~/Downloads/data", header = T)
d <- as.character(ConversionsCompletions$date)
d <-  paste0(substr(d, 0, 2), "-", substr(d, 3, 4), "-", substr(d, start = 4, 7))
ConversionsCompletions$date <- as.Date(d, format = "%d-%m-%Y")

ui <- fluidPage(
  checkboxGroupInput("checkGroup", label = h3("Goal"), 
                     setNames(object = paste0("goal", 1:7), 
                              nm = paste0("Goal ", 1:7)),
                     selected = "Goal 1"),
  plotlyOutput("Conversionrate1")
)

server <- function(input, output) { 

  filteredData <- reactive({ 
    # no need for "filter"
    ConversionsCompletions[ConversionsCompletions$goal %in% input$checkGroup, ]
  })


  output$Conversionrate1 <- renderPlotly({
    # use filteredData() instead of the full dataset
    plot_ly(
      filteredData(),  
      x = ~date,
      y = ~goal1completions,
      split = ~goal,
      mode = "lines + markers",
      hoverinfo = "y" # "y" instead of y ... at least in the newest version
    ) 
  })
}

shinyApp(ui, server)