R闪亮的Checkboxgroupinput with Plotly

时间:2016-09-28 15:23:53

标签: r plotly shiny shinydashboard

到目前为止,热爱这里的所有帮助,但不得不自学R / Shiny,办公室里没有人帮忙,我不幸再次陷入困境!

我正在尝试在Shiny中执行checkboxgroups。我读了很多,例如thisthisthisthisthis已经有所帮助,但我现在被困住了。

所以我的数据集“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”代码,所以我不知道在这种情况下如果有一条线有时候有多条线也会这样做。

任何帮助表示赞赏!!

1 个答案:

答案 0 :(得分:3)

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

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

要绘制多行,您可以使用另一个参数split。我不得不将hoverinfo = y更改为hoverinfo = "y",否则它无效(我有最新版本的情节)

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)

enter image description here