2016-09-28 217 views
2

到目前爲止所有的幫助,但不得不自己教R/Shiny,沒有人在辦公室幫忙,我不幸再次卡住!R的Checkboxgroupinput Plotly閃亮

我想在Shiny中做checkboxgroups。我讀了不少,例如this,this,this,thisthis已經幫助,但我現在卡住了。

所以把我的數據 「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」代碼,所以我不確定在這種情況下如何做到這一點,有時有一行,有時是多行。

任何幫助讚賞!

+0

你想爲每個「目標」分開一行嗎? –

+0

理想情況下......這可能嗎? – RSesom

+0

我正在努力:) –

回答

3

要正確渲染圖形,必須使用filteredData()並略微更改語法。

由於data你應該使用過濾數據集,然後xy變量相應的名稱的前綴~第一個參數。

要繪製多行,您可以使用另一個參數split。我不得不改變hoverinfo = yhoverinfo = "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的子集 - 至少在這個在這種情況下。


EDITED:

我轉換數值變量datedate格式:

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

+0

非常感謝!,真的很感激! - 但是,這似乎只適用於新的情節,這不幸破壞了我大部分的舊圖 - 因爲你似乎很熟悉新(4.6)和舊(3.5)之間的區別,你能否指我一個資源,我可以找到我需要改變的舊圖表,讓他們在新的情節下正確顯示?非常感謝! – RSesom

+1

好吧,好像我發現網站有足夠的信息 - 需要通過我所有的圖表和實施變化。再次感謝!!! :) – RSesom