ggvis如何与Shiny集成?

3
这是一个比较简单的问题。我阅读了其他帖子,并发现为了将GGVIS可视化插入Shiny中,您需要:
  1. ui.R中-调用ggvisOutput(“EvolucionVisitas”)
  2. server.R中-使用函数bind_shiny(“EvolucionVisitas”)
我在我的选项卡“Evolución Visitas”中绘制图表时遇到了问题。我都做了,但是我在某个地方失败了。
我的选项卡“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))


    })

})

您的数据无法再现,数据中缺少“Fuentes”。 - cdeterman
3个回答

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时有类似的东西——我的代码在这里:https://github.com/jalapic/shinyapps/tree/master/soccerteams,可能会有所帮助。


我认为你忘记将 ggvis 代码应用到数据框中了。我尝试了你说的方法,但仍然没有任何输出。 - Omar Gonzales
噢,我发现当使用交互式工具提示并将shiny绑定到该反应式时,经常需要将我的ggvis代码放入反应式表达式中。也许你的问题是其他方面引起的。 - jalapic

1

仅针对ggvis的故障排除,您的问题主要是由于尝试自定义x轴而引起的。 ggvis试图通过将日期解释为时间来变得智能。 对于此绘图目的,我认为最好将它们视为因素。

以下是完整的可重现答案。

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

你会注意到一些其他的区别。最好使你的数据集响应式,这样你就可以在多个地方重复使用它而不会增加额外的开销。同时,正如@jalapic最初建议的那样,你需要使你的ggvis对象响应式,这样图形就可以是动态的,并使用漂亮的工具提示。

请问您能否将"ui.R"和"server.R"分开吗?我稍后会在家里尝试。谢谢。 - Omar Gonzales
@OmarGonzales,我已经将文件分开了,这个解决方案对你有效吗? - cdeterman
@cderterman,我已经尝试并得到了:"正在侦听 http://127.0.0.1:4457 在 match(x, table, nomatch = 0L) 中出现错误: 'match' 需要向量参数"。我无法完全测试它,稍后再试一下。非常感谢。 - Omar Gonzales
@OmarGonzales,啊!我竟然忘了包含这些库。我已经编辑了我的答案,就像Cory一样简洁明了。 - cdeterman
@OmarGonzales,很难说。我怀疑与反应式有关?但是x轴格式在两种情况下都应该是一个问题。 - cdeterman
显示剩余2条评论

0

哇...这有点乱。我把它简化到了ggvis部分并尝试让它运行。在这个gist中查看。

您可以使用以下命令运行:

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

我已经尝试过这个,但是与DateInputRange按钮不反应...但是当改变日期时,图形没有改变。你能帮忙查一下吗?谢谢! - Omar Gonzales
哦,我想你可以让它运行起来。我已经更新了要点。现在图表是带有日期选择器的交互式的。 - cory
它运行得很完美。只有x标签需要显示所选范围内的第一个、中间和最后一个日期,但实际上它并没有这样做。我现在正在工作,所以无法完全测试它。稍后我会仔细检查它。谢谢。 - Omar Gonzales

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接