2015-02-10 61 views
3

这是一个相当简单的问题。我看了其他线程,结果发现,为了插入GGVIS可视化到闪亮,你需要:ggvis与Shiny集成

  1. ui.R - 呼叫ggvisOutput("EvolucionVisitas")
  2. server.R - 使用功能bind_shiny("EvolucionVisitas")

我m有问题为我的标签“EvoluciónVisitas”绘制图表。 我做了两个,但我失败了。

我的选项卡中没有打印:EvoluciónVisitas。其他一切都没问题。

这里是我的数据:

structure(list(date = structure(1:31, .Label = c("2014-12-01", 
"2014-12-02", "2014-12-03", "2014-12-04", "2014-12-05", "2014-12-06", 
"2014-12-07", "2014-12-08", "2014-12-09", "2014-12-10", "2014-12-11", 
"2014-12-12", "2014-12-13", "2014-12-14", "2014-12-15", "2014-12-16", 
"2014-12-17", "2014-12-18", "2014-12-19", "2014-12-20", "2014-12-21", 
"2014-12-22", "2014-12-23", "2014-12-24", "2014-12-25", "2014-12-26", 
"2014-12-27", "2014-12-28", "2014-12-29", "2014-12-30", "2014-12-31" 
), class = "factor"), sessions = c(1932L, 1828L, 2349L, 8192L, 
3188L, 3277L, 2846L, 2541L, 5434L, 4290L, 2059L, 2080L, 2111L, 
3776L, 1989L, 1844L, 3641L, 1283L, 1362L, 1568L, 2882L, 1212L, 
957L, 851L, 928L, 1435L, 1115L, 1471L, 1128L, 1022L, 768L), id = 1:31), .Names = c("date", 
"sessions", "id"), row.names = c(NA, -31L), drop = TRUE, class = c("tbl_df", 
"tbl", "data.frame")) 

这里我的代码,谢谢。

ui.R

library(shiny) 
library(ggvis) 

# Define the overall UI 
shinyUI(

    # Use a fluid Bootstrap layout 
    fluidPage( 

    # Give the page a title 
    br(), 
    br(), 
    titlePanel("Visitas por fuente"), 

    # Generate a row with a sidebar 
    sidebarLayout(  

     # Define the sidebar with one input 



     sidebarPanel(
     dateRangeInput("dates", label = h3("Date range"), 
         start = "2014-12-01", end = "2014-12-31") 

    ), 


     mainPanel(
     tabsetPanel(
      tabPanel('Visitas por fuente', 
        plotOutput("VisitasFuente")), 
      tabPanel('Evolución de las visitas', 
        ggvisOutput("EvolucionVisitas")), 
      tabPanel('Comentarios', 
        dataTableOutput("Comentarios")) 
     ) 

    ) 
) 
)) 

server.R

library(shiny) 
library(ggvis) 



Visitas_Por_Fuente <- read.csv("D:\\RCoursera\\Star-App-2\\Visitas_Por_Fuente_Dic.csv") 
labelsF = c("Directo", "Email", "Referencias", "SEO", "Social Media", "Campañas", "Adwords") 
Visitas_Por_Fuente$date <- as.Date(Visitas_Por_Fuente$date) 
ComentariosDic <- read.csv("D:\\RCoursera\\Star-App-2\\ComentariosDic2014.csv",header = TRUE, sep = ";") 
ComentariosDic$date <- as.Date(ComentariosDic$date) 


shinyServer(


    function(input, output) { 



    output$VisitasFuente <- renderPlot({ 

     # Filter the data based on user selection month  
     date_seq <- seq(input$dates[1], input$dates[2], by = "day") 


     VisitasData <- filter(Visitas_Por_Fuente, date %in% date_seq & Fuentes %in% labelsF) 

     VisitasData <- VisitasData %>% group_by(Fuentes) %>% 
            summarise(sessions = sum(sessions)) 




     # Bar graph using ggplot2 library 
     ggplot(VisitasData, aes(factor(Fuentes), sessions, fill = Fuentes)) + 
     geom_bar(stat="identity", position = "dodge") + 
     geom_text(aes(label = comma(sessions)), position=position_dodge(width=0.9), vjust=-0.25) + 
     scale_fill_manual(breaks = c("0", "1", "3", "6", "9", "12", "15"), 
          labels = labelsF, 
          values = c("#E69F00", "#56B4E9", "#009E73", 
            "#F0E442", "#0072B2", "#A082F8", "#F072A2")) 

    }) 

    **############# Evolución de las visitas ############################################## 
    #####################################################################################** 


    output$EvolucionVisitas <- renderPlot({ 

     # Filter the data based on user selection month  
     date_seq <- seq(input$dates[1], input$dates[2], by = "day") 


     EvolucionVisitas <- filter(Visitas_Por_Fuente, date %in% date_seq) 


     mysessions <- function(x) { 
     if(is.null(x)) return(NULL) 
     #notice below the id column is how ggvis can understand which session to show 
     row <- EvolucionVisitas[EvolucionVisitas$id == x$id, ] 
     #prettyNum shows the number with thousand-comma separator 
     paste0("Sessions:", "&nbsp;",prettyNum(row$sessions, big.mark=",",scientific=F)) 
     } 




     EvolucionVisitas %>% 
     ggvis(x= ~date, y= ~sessions, key := ~id) %>% 
     layer_points() %>% 
     add_tooltip(mysessions ,"hover") %>% 
     layer_paths() %>% 
     add_axis("x", value=c(as.character(EvolucionVisitas$date[1]),as.character(EvolucionVisitas$date[round(length(EvolucionVisitas$date)/2,0)]), 
           as.character(tail(EvolucionVisitas$date, n=1)))) %>% 
     bind_shiny("EvolucionVisitas") 







    ##################################################################################### 
    ##################################################################################### 


    output$Comentarios = renderDataTable({ 

     date_seq <- seq(input$dates[1], input$dates[2], by = "day") 


     ComentariosDic <- filter(ComentariosDic, date %in% date_seq) 

     ComentariosDic <- filter(ComentariosDic, !grepl("^$", Comentarios)) 


    }) 

}) 
+0

你的数据是不可复制的,'Fuentes'从你的数据丢失。 – cdeterman 2015-02-12 13:55:03

回答

1

只有对ggvis进行故障排除,您的问题主要是您尝试自定义x轴的结果。 ggvis通过将日期解释为时间来尝试变得聪明。出于这个阴谋的目的,我认为最好把它们当作因素。

这是一个完全可重复的答案。

shiny::runGist("https://gist.github.com/cdeterman/0ac102cd68a7987a8a90") 

您会注意到其他一些差异。可能最好使数据集处于活动状态,以便您可以在多个地方重复使用它,而无需额外的开销。另外,由于@ jalapic最初建议,你想让你的ggvis对象反应,所以剧情可以是动态的,并使用漂亮的工具提示。

+0

请您将“ui.R”从“server.R”分开。稍后我会在家里尝试。谢谢。 – 2015-02-12 18:18:48

+0

@OmarGonzales,我把文件分开了,这个解决方案对你有用吗? – cdeterman 2015-02-12 21:29:58

+0

@cderterman,我试过并得到:“听http://127.0.0.1:4457 匹配错误(x,table,nomatch = 0L): '匹配'需要向量参数”。我无法完全测试它,所有测试都在稍后尝试。非常感谢。 – 2015-02-12 21:35:43

1

如果你有server.R开始

output$EvolucionVisitas <- renderPlot({ 

你可以尝试在反应一样包裹它的代码这个,而不是在renderPlot

vis <- reactive({ 

# Filter the data based on user selection month  
     date_seq <- seq(input$dates[1], input$dates[2], by = "day") 


     EvolucionVisitas <- filter(Visitas_Por_Fuente, date %in% date_seq) 


     mysessions <- function(x) { 
     if(is.null(x)) return(NULL) 
     #notice below the id column is how ggvis can understand which session to show 
     row <- EvolucionVisitas[EvolucionVisitas$id == x$id, ] 
     #prettyNum shows the number with thousand-comma separator 
     paste0("Sessions:", "&nbsp;",prettyNum(row$sessions, big.mark=",",scientific=F)) 
     } 

myvis <- 
    ggvis(x= ~date, y= ~sessions, key := ~id) %>% 
     layer_points() %>% 
     add_tooltip(mysessions ,"hover") %>% 
     layer_paths() %>% 
     add_axis("x", 

    value=c(as.character(EvolucionVisitas$date[1]),as.character(EvolucionVisitas$date[round(length(EvolucionVisitas$date)/2,0)]), 
            as.character(tail(EvolucionVisitas$date, n=1)))) 


    myvis 
    }) 

然后反应放之外:

vis %>% bind_shiny("EvolucionVisitas") 

我觉得我记得类似的,当我在做一个闪亮/ ggvis - 我的代码是在我的github上的位置:https://github.com/jalapic/shinyapps/tree/master/soccerteams这可能会有帮助。

+0

我想你的遗漏将ggvis代码应用于数据框。我试过你说了什么,但是仍然没有打印。 – 2015-02-10 03:21:34

+0

嗯,我发现我经常需要将我的ggvis代码放入反应式表达式中,然后使用交互式工具提示,然后绑定闪烁的反应。 - 也许你的问题是别的 – jalapic 2015-02-10 03:30:36

+0

我会试着找出它是什么。无论如何,男士。 – 2015-02-10 03:32:23

0

呃......这有点乱。我把它剥离到ggvis部分,并试图让它运行。看看这个gist

你可以运行它:

shiny::runGist("https://gist.github.com/corynissen/f75ecae388f81be13436") 
+0

我试过这个,但是没有与dateinputRange按钮反应......但是当改变日期时图形不会改变。你可以看看吗?谢谢! - – 2015-02-12 21:22:26

+0

哦,我想你可以得到那个跑步。我已经更新了要点。该图表现在与日期选择器交互。 – cory 2015-02-12 21:30:59

+0

它工作完美。只需要x标签,只需要显示所选范围内的第一个,中间和最后一个日期,而它实际上并未这样做。我现在在工作,所以我不能完全测试它。我稍后会仔细检查它。谢啦。 – 2015-02-12 21:39:10