R:Shiny/Ggvis - daterangeInput:条形图更改但工具提示没有
R: Shiny/Ggvis - daterangeInput: Bar graph changes but Tooltip does not
我的图表发生变化,我知道这是因为 Y 轴改变了它的限制。但是无论我是否更改日期范围,每个栏的工具提示都是相同的。
如您所见,它表示:"Total number of Sessions: 11,610" 用于 "Directo" 来源。这是整个十二月的时间。但是,如果我将日期从“2014-12-01”更改为“2014-12-01”,只查看 12 月第一天的数据,条形图会发生变化,但工具提示一直显示:"Total number of Sessions: 11,610" 对于 "Directo".
我认为我的 "mysessions" 工具提示部分的函数有问题,当 daterangeInput 更改时无法识别数据的子集。但是想不通。
我的server.R: ###你可以在最后看到"dput()"我的数据
library(dplyr)
library(ggvis)
library(shiny)
Visitas_Por_Fuente <- read.csv("D:\RCoursera\Star- App3\Visitas_Por_Fuente_Dic.csv") ## Call it from a CSV file, but you can see the dput() data at the end of the post.
Visitas_Por_Fuente$date <- as.Date(Visitas_Por_Fuente$date)
shinyServer(
function(input, output, session){
myData <- 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)
return(EvolucionVisitas)
})
############# Evolución de las visitas ##############################################
#####################################################################################**
myvis <- reactive({
EvolucionVisitas <- myData()
mysessions <- function(x) {
if(is.null(x)) return(NULL)
# compute the total number of sessions of the "Fuente" type that is hovered over
total_sessions <- sum(EvolucionVisitas[EvolucionVisitas$Fuentes == x$Fuentes, "sessions"])
# format the value with prettyNum if you like:
paste0("Total number of Sessions:", " ",
prettyNum(total_sessions, big.mark=",",scientific=F))
}
outvis <-
EvolucionVisitas %>%
ggvis(~Fuentes, ~sessions) %>%
layer_bars(width = 0.8, fill = ~Fuentes) %>%
add_tooltip(mysessions ,"hover")
return(outvis)
})
myvis %>% bind_shiny("EvolucionVisitas")
}
)
我的ui.R:
library(shiny)
library(ggvis)
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('Evolución de las visitas',
ggvisOutput("EvolucionVisitas"))
)
)
)
)
)
我的数据:#needed to convert using as.data.frame。来自 dplyr 的 group_by 函数正在发生某些事情。
as.data.frame(structure(list(date = structure(c(1417410000, 1417410000, 1417410000,
1417410000, 1417410000, 1417410000, 1417496400, 1417496400, 1417496400,
1417496400, 1417496400, 1417496400, 1417496400, 1417582800, 1417582800,
1417582800, 1417582800, 1417582800, 1417582800, 1417582800, 1417669200,
1417669200, 1417669200, 1417669200, 1417669200, 1417669200, 1417669200,
1417755600, 1417755600, 1417755600, 1417755600, 1417755600, 1417755600,
1417842000, 1417842000, 1417842000, 1417842000, 1417842000, 1417842000,
1417842000, 1417928400, 1417928400, 1417928400, 1417928400, 1417928400,
1417928400, 1417928400, 1418014800, 1418014800, 1418014800, 1418014800,
1418014800, 1418014800, 1418014800, 1418101200, 1418101200, 1418101200,
1418101200, 1418101200, 1418101200, 1418101200, 1418187600, 1418187600,
1418187600, 1418187600, 1418187600, 1418187600, 1418187600, 1418274000,
1418274000, 1418274000, 1418274000, 1418274000, 1418274000, 1418274000,
1418360400, 1418360400, 1418360400, 1418360400, 1418360400, 1418360400,
1418360400, 1418446800, 1418446800, 1418446800, 1418446800, 1418446800,
1418446800, 1418446800, 1418533200, 1418533200, 1418533200, 1418533200,
1418533200, 1418533200, 1418533200, 1418619600, 1418619600, 1418619600,
1418619600, 1418619600, 1418619600, 1418619600, 1418706000, 1418706000,
1418706000, 1418706000, 1418706000, 1418706000, 1418706000, 1418792400,
1418792400, 1418792400, 1418792400, 1418792400, 1418792400, 1418792400,
1418878800, 1418878800, 1418878800, 1418878800, 1418878800, 1418878800,
1418878800, 1418965200, 1418965200, 1418965200, 1418965200, 1418965200,
1418965200, 1418965200, 1419051600, 1419051600, 1419051600, 1419051600,
1419051600, 1419051600, 1419051600, 1419138000, 1419138000, 1419138000,
1419138000, 1419138000, 1419138000, 1419224400, 1419224400, 1419224400,
1419224400, 1419224400, 1419224400, 1419224400, 1419310800, 1419310800,
1419310800, 1419310800, 1419310800, 1419310800, 1419397200, 1419397200,
1419397200, 1419397200, 1419397200, 1419397200, 1419397200, 1419483600,
1419483600, 1419483600, 1419483600, 1419483600, 1419483600, 1419483600,
1419570000, 1419570000, 1419570000, 1419570000, 1419570000, 1419570000,
1419656400, 1419656400, 1419656400, 1419656400, 1419656400, 1419656400,
1419742800, 1419742800, 1419742800, 1419742800, 1419742800, 1419742800,
1419742800, 1419829200, 1419829200, 1419829200, 1419829200, 1419829200,
1419829200, 1419915600, 1419915600, 1419915600, 1419915600, 1419915600,
1419915600, 1419915600, 1420002000, 1420002000, 1420002000, 1420002000,
1420002000), class = c("POSIXct", "POSIXt"), tzone = "America/Lima"),
Fuentes = c("Adwords", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Adwords", "Campañas", "Directo",
"Email", "Referencias", "SEO", "Social Media", "Adwords",
"Campañas", "Directo", "Email", "Referencias", "SEO", "Social Media",
"Adwords", "Campañas", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Campañas", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Adwords", "Campañas", "Directo",
"Email", "Referencias", "SEO", "Social Media", "Adwords",
"Campañas", "Directo", "Email", "Referencias", "SEO", "Social Media",
"Adwords", "Campañas", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Adwords", "Campañas", "Directo",
"Email", "Referencias", "SEO", "Social Media", "Adwords",
"Campañas", "Directo", "Email", "Referencias", "SEO", "Social Media",
"Adwords", "Campañas", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Adwords", "Campañas", "Directo",
"Email", "Referencias", "SEO", "Social Media", "Adwords",
"Campañas", "Directo", "Email", "Referencias", "SEO", "Social Media",
"Adwords", "Campañas", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Adwords", "Campañas", "Directo",
"Email", "Referencias", "SEO", "Social Media", "Adwords",
"Campañas", "Directo", "Email", "Referencias", "SEO", "Social Media",
"Adwords", "Campañas", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Adwords", "Campañas", "Directo",
"Email", "Referencias", "SEO", "Social Media", "Adwords",
"Campañas", "Directo", "Email", "Referencias", "SEO", "Social Media",
"Adwords", "Campañas", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Campañas", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Adwords", "Campañas", "Directo",
"Email", "Referencias", "SEO", "Social Media", "Campañas",
"Directo", "Email", "Referencias", "SEO", "Social Media",
"Adwords", "Campañas", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Adwords", "Campañas", "Directo",
"Email", "Referencias", "SEO", "Social Media", "Campañas",
"Directo", "Email", "Referencias", "SEO", "Social Media",
"Campañas", "Directo", "Email", "Referencias", "SEO", "Social Media",
"Adwords", "Campañas", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Campañas", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Adwords", "Campañas", "Directo",
"Email", "Referencias", "SEO", "Social Media", "Directo",
"Email", "Referencias", "SEO", "Social Media"), sessions = c(10L,
326L, 18L, 527L, 999L, 52L, 3L, 7L, 324L, 19L, 498L, 933L,
44L, 5L, 12L, 363L, 15L, 546L, 1206L, 202L, 7L, 12L, 1122L,
27L, 1249L, 5168L, 607L, 17L, 432L, 22L, 527L, 1553L, 637L,
5L, 5L, 356L, 16L, 507L, 1901L, 487L, 4L, 8L, 312L, 30L,
500L, 1622L, 370L, 3L, 11L, 341L, 18L, 504L, 1312L, 352L,
2L, 13L, 1188L, 33L, 682L, 2008L, 1508L, 3L, 15L, 1086L,
36L, 646L, 2124L, 380L, 3L, 11L, 355L, 17L, 383L, 1054L,
236L, 2L, 9L, 321L, 15L, 370L, 1118L, 245L, 7L, 9L, 259L,
13L, 332L, 1261L, 230L, 3L, 7L, 539L, 9L, 626L, 2336L, 256L,
1L, 12L, 292L, 8L, 386L, 1070L, 220L, 1L, 12L, 278L, 9L,
393L, 1129L, 22L, 1L, 16L, 521L, 18L, 665L, 2400L, 20L, 1L,
13L, 204L, 7L, 258L, 789L, 11L, 2L, 12L, 253L, 6L, 277L,
803L, 9L, 1L, 4L, 262L, 8L, 324L, 960L, 9L, 3L, 442L, 15L,
516L, 1890L, 16L, 2L, 14L, 249L, 8L, 240L, 688L, 11L, 15L,
182L, 7L, 200L, 548L, 5L, 2L, 3L, 171L, 7L, 183L, 480L, 5L,
1L, 3L, 176L, 5L, 231L, 506L, 6L, 15L, 283L, 18L, 322L, 786L,
11L, 13L, 210L, 9L, 232L, 649L, 2L, 1L, 2L, 258L, 4L, 306L,
891L, 9L, 4L, 183L, 10L, 293L, 634L, 4L, 1L, 3L, 188L, 9L,
244L, 566L, 11L, 134L, 1L, 176L, 453L, 4L)), .Names = c("date",
"Fuentes", "sessions"), row.names = c(NA, -208L), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), vars = list(date), drop = TRUE))
出于某种原因,如果自定义 tool_tip
函数将用作反应式,则该函数必须在反应式上下文之外定义。即使将函数直接放在 ggvis
代码中也不会随着数据的变化而更新。只需将 mysessions
函数移动到反应性 ggvis
绘图上方,您的代码就可以正常工作。
shiny::runGist("https://gist.github.com/cdeterman/cc0e4a38863edcfea3ea")
我对 shiny 中的机制没有足够深入的了解来解释为什么会这样。如果您对具体原因感到好奇,您可能需要转到 github 页面上的问题,看看是否有人可以更好地解释这一点。
我的图表发生变化,我知道这是因为 Y 轴改变了它的限制。但是无论我是否更改日期范围,每个栏的工具提示都是相同的。
如您所见,它表示:"Total number of Sessions: 11,610" 用于 "Directo" 来源。这是整个十二月的时间。但是,如果我将日期从“2014-12-01”更改为“2014-12-01”,只查看 12 月第一天的数据,条形图会发生变化,但工具提示一直显示:"Total number of Sessions: 11,610" 对于 "Directo".
我认为我的 "mysessions" 工具提示部分的函数有问题,当 daterangeInput 更改时无法识别数据的子集。但是想不通。
我的server.R: ###你可以在最后看到"dput()"我的数据
library(dplyr)
library(ggvis)
library(shiny)
Visitas_Por_Fuente <- read.csv("D:\RCoursera\Star- App3\Visitas_Por_Fuente_Dic.csv") ## Call it from a CSV file, but you can see the dput() data at the end of the post.
Visitas_Por_Fuente$date <- as.Date(Visitas_Por_Fuente$date)
shinyServer(
function(input, output, session){
myData <- 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)
return(EvolucionVisitas)
})
############# Evolución de las visitas ##############################################
#####################################################################################**
myvis <- reactive({
EvolucionVisitas <- myData()
mysessions <- function(x) {
if(is.null(x)) return(NULL)
# compute the total number of sessions of the "Fuente" type that is hovered over
total_sessions <- sum(EvolucionVisitas[EvolucionVisitas$Fuentes == x$Fuentes, "sessions"])
# format the value with prettyNum if you like:
paste0("Total number of Sessions:", " ",
prettyNum(total_sessions, big.mark=",",scientific=F))
}
outvis <-
EvolucionVisitas %>%
ggvis(~Fuentes, ~sessions) %>%
layer_bars(width = 0.8, fill = ~Fuentes) %>%
add_tooltip(mysessions ,"hover")
return(outvis)
})
myvis %>% bind_shiny("EvolucionVisitas")
}
)
我的ui.R:
library(shiny)
library(ggvis)
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('Evolución de las visitas',
ggvisOutput("EvolucionVisitas"))
)
)
)
)
)
我的数据:#needed to convert using as.data.frame。来自 dplyr 的 group_by 函数正在发生某些事情。
as.data.frame(structure(list(date = structure(c(1417410000, 1417410000, 1417410000,
1417410000, 1417410000, 1417410000, 1417496400, 1417496400, 1417496400,
1417496400, 1417496400, 1417496400, 1417496400, 1417582800, 1417582800,
1417582800, 1417582800, 1417582800, 1417582800, 1417582800, 1417669200,
1417669200, 1417669200, 1417669200, 1417669200, 1417669200, 1417669200,
1417755600, 1417755600, 1417755600, 1417755600, 1417755600, 1417755600,
1417842000, 1417842000, 1417842000, 1417842000, 1417842000, 1417842000,
1417842000, 1417928400, 1417928400, 1417928400, 1417928400, 1417928400,
1417928400, 1417928400, 1418014800, 1418014800, 1418014800, 1418014800,
1418014800, 1418014800, 1418014800, 1418101200, 1418101200, 1418101200,
1418101200, 1418101200, 1418101200, 1418101200, 1418187600, 1418187600,
1418187600, 1418187600, 1418187600, 1418187600, 1418187600, 1418274000,
1418274000, 1418274000, 1418274000, 1418274000, 1418274000, 1418274000,
1418360400, 1418360400, 1418360400, 1418360400, 1418360400, 1418360400,
1418360400, 1418446800, 1418446800, 1418446800, 1418446800, 1418446800,
1418446800, 1418446800, 1418533200, 1418533200, 1418533200, 1418533200,
1418533200, 1418533200, 1418533200, 1418619600, 1418619600, 1418619600,
1418619600, 1418619600, 1418619600, 1418619600, 1418706000, 1418706000,
1418706000, 1418706000, 1418706000, 1418706000, 1418706000, 1418792400,
1418792400, 1418792400, 1418792400, 1418792400, 1418792400, 1418792400,
1418878800, 1418878800, 1418878800, 1418878800, 1418878800, 1418878800,
1418878800, 1418965200, 1418965200, 1418965200, 1418965200, 1418965200,
1418965200, 1418965200, 1419051600, 1419051600, 1419051600, 1419051600,
1419051600, 1419051600, 1419051600, 1419138000, 1419138000, 1419138000,
1419138000, 1419138000, 1419138000, 1419224400, 1419224400, 1419224400,
1419224400, 1419224400, 1419224400, 1419224400, 1419310800, 1419310800,
1419310800, 1419310800, 1419310800, 1419310800, 1419397200, 1419397200,
1419397200, 1419397200, 1419397200, 1419397200, 1419397200, 1419483600,
1419483600, 1419483600, 1419483600, 1419483600, 1419483600, 1419483600,
1419570000, 1419570000, 1419570000, 1419570000, 1419570000, 1419570000,
1419656400, 1419656400, 1419656400, 1419656400, 1419656400, 1419656400,
1419742800, 1419742800, 1419742800, 1419742800, 1419742800, 1419742800,
1419742800, 1419829200, 1419829200, 1419829200, 1419829200, 1419829200,
1419829200, 1419915600, 1419915600, 1419915600, 1419915600, 1419915600,
1419915600, 1419915600, 1420002000, 1420002000, 1420002000, 1420002000,
1420002000), class = c("POSIXct", "POSIXt"), tzone = "America/Lima"),
Fuentes = c("Adwords", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Adwords", "Campañas", "Directo",
"Email", "Referencias", "SEO", "Social Media", "Adwords",
"Campañas", "Directo", "Email", "Referencias", "SEO", "Social Media",
"Adwords", "Campañas", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Campañas", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Adwords", "Campañas", "Directo",
"Email", "Referencias", "SEO", "Social Media", "Adwords",
"Campañas", "Directo", "Email", "Referencias", "SEO", "Social Media",
"Adwords", "Campañas", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Adwords", "Campañas", "Directo",
"Email", "Referencias", "SEO", "Social Media", "Adwords",
"Campañas", "Directo", "Email", "Referencias", "SEO", "Social Media",
"Adwords", "Campañas", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Adwords", "Campañas", "Directo",
"Email", "Referencias", "SEO", "Social Media", "Adwords",
"Campañas", "Directo", "Email", "Referencias", "SEO", "Social Media",
"Adwords", "Campañas", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Adwords", "Campañas", "Directo",
"Email", "Referencias", "SEO", "Social Media", "Adwords",
"Campañas", "Directo", "Email", "Referencias", "SEO", "Social Media",
"Adwords", "Campañas", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Adwords", "Campañas", "Directo",
"Email", "Referencias", "SEO", "Social Media", "Adwords",
"Campañas", "Directo", "Email", "Referencias", "SEO", "Social Media",
"Adwords", "Campañas", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Campañas", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Adwords", "Campañas", "Directo",
"Email", "Referencias", "SEO", "Social Media", "Campañas",
"Directo", "Email", "Referencias", "SEO", "Social Media",
"Adwords", "Campañas", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Adwords", "Campañas", "Directo",
"Email", "Referencias", "SEO", "Social Media", "Campañas",
"Directo", "Email", "Referencias", "SEO", "Social Media",
"Campañas", "Directo", "Email", "Referencias", "SEO", "Social Media",
"Adwords", "Campañas", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Campañas", "Directo", "Email", "Referencias",
"SEO", "Social Media", "Adwords", "Campañas", "Directo",
"Email", "Referencias", "SEO", "Social Media", "Directo",
"Email", "Referencias", "SEO", "Social Media"), sessions = c(10L,
326L, 18L, 527L, 999L, 52L, 3L, 7L, 324L, 19L, 498L, 933L,
44L, 5L, 12L, 363L, 15L, 546L, 1206L, 202L, 7L, 12L, 1122L,
27L, 1249L, 5168L, 607L, 17L, 432L, 22L, 527L, 1553L, 637L,
5L, 5L, 356L, 16L, 507L, 1901L, 487L, 4L, 8L, 312L, 30L,
500L, 1622L, 370L, 3L, 11L, 341L, 18L, 504L, 1312L, 352L,
2L, 13L, 1188L, 33L, 682L, 2008L, 1508L, 3L, 15L, 1086L,
36L, 646L, 2124L, 380L, 3L, 11L, 355L, 17L, 383L, 1054L,
236L, 2L, 9L, 321L, 15L, 370L, 1118L, 245L, 7L, 9L, 259L,
13L, 332L, 1261L, 230L, 3L, 7L, 539L, 9L, 626L, 2336L, 256L,
1L, 12L, 292L, 8L, 386L, 1070L, 220L, 1L, 12L, 278L, 9L,
393L, 1129L, 22L, 1L, 16L, 521L, 18L, 665L, 2400L, 20L, 1L,
13L, 204L, 7L, 258L, 789L, 11L, 2L, 12L, 253L, 6L, 277L,
803L, 9L, 1L, 4L, 262L, 8L, 324L, 960L, 9L, 3L, 442L, 15L,
516L, 1890L, 16L, 2L, 14L, 249L, 8L, 240L, 688L, 11L, 15L,
182L, 7L, 200L, 548L, 5L, 2L, 3L, 171L, 7L, 183L, 480L, 5L,
1L, 3L, 176L, 5L, 231L, 506L, 6L, 15L, 283L, 18L, 322L, 786L,
11L, 13L, 210L, 9L, 232L, 649L, 2L, 1L, 2L, 258L, 4L, 306L,
891L, 9L, 4L, 183L, 10L, 293L, 634L, 4L, 1L, 3L, 188L, 9L,
244L, 566L, 11L, 134L, 1L, 176L, 453L, 4L)), .Names = c("date",
"Fuentes", "sessions"), row.names = c(NA, -208L), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), vars = list(date), drop = TRUE))
出于某种原因,如果自定义 tool_tip
函数将用作反应式,则该函数必须在反应式上下文之外定义。即使将函数直接放在 ggvis
代码中也不会随着数据的变化而更新。只需将 mysessions
函数移动到反应性 ggvis
绘图上方,您的代码就可以正常工作。
shiny::runGist("https://gist.github.com/cdeterman/cc0e4a38863edcfea3ea")
我对 shiny 中的机制没有足够深入的了解来解释为什么会这样。如果您对具体原因感到好奇,您可能需要转到 github 页面上的问题,看看是否有人可以更好地解释这一点。