- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是一个相当简单的问题。我阅读了其他主题,发现要将 GGVIS 可视化插入 Shiny,您需要:
ui.R
中 - 调用 ggvisOutput("EvolucionVisitas")
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"))
)
)
)
))
服务器.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:", " ",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))
})
})
最佳答案
仅对 ggvis 进行故障排除,您的问题主要是由于您尝试自定义 x 轴造成的。 ggvis
试图通过将日期解释为时间来变得聪明。出于此图的目的,我认为最好将它们视为因素。
这是一个完整的可重现的答案。
shiny::runGist("https://gist.github.com/cdeterman/0ac102cd68a7987a8a90")
您会注意到其他一些差异。最好使您的数据集具有反应性,这样您就可以在多个地方重用它而无需额外的开销。此外,正如 @jalapic 最初建议的那样,您希望使 ggvis
对象具有反应性,以便绘图可以是动态的并使用漂亮的工具提示。
关于r - ggvis 与 Shiny 的集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28423302/
我已经搜索过了,所以如果我遗漏了什么,请原谅我。 让我们假设一个包含名称、日期、卡路里的数据框,其中卡路里是该人当天消耗的卡路里总数。 Name Date Calories Amy
我读过一篇similar post on SO ,但无法根据我的具体情况调整答案。我正在处理时间序列数据,并希望将两个不同的数据集合并到同一个图中。虽然我可以将数据合并到一个数据框中,但我真的很想了解
我曾经用 ggplot2 制作交互图,代码如下。现在我想用 ggvis 重现相同的情节,如下所示,它与 ggplto2 输出不同。如何使用 ggvis 获得相同的图? library(ggplot2)
我正在尝试使用 ggvis 中 ggplot2 中的数字复制热图。ggplot2版本是 library(ggplot2) hec % ggvis(~Hair, ~Eye, fill=~Freq)%>%
在ggvis中如何使图例垂直? mtcars %>% ggvis(x = ~wt, y = ~mpg, fill = ~cyl) %>% layer_points() %>% add_legend("
有没有办法在 ggvis 中将标签附加到堆叠条形图? 示例代码: plotData = data.frame(v1 = c("ABC", "ABC", "ABC", "XYX","XYX","X
我正在尝试更改 ggvis 中的刻度标签阴谋。我的数据点是 x = c(1,2,3)和 y = c(1,2,3) .但是,下面的代码会产生完全没有意义的刻度标签! library(dplyr) lib
我的 ggvis 图取决于几个输入字段,它们的工作方式类似于输入数据的过滤器。对于某些组合,结果数据框为空,ggvis 抛出错误并破坏整个应用程序。 我试着把 if(nrow(inputdatafra
我有一个 RStudio Shiny 服务器正在运行,我从 https://github.com/rstudio/ggvis 安装了 ggvis但我无法在服务器中重现任何演示示例。 当我在服务器(3.
ggvis 将根据我传递的因子列自动为我的绘图着色。所以如果我的因子列被命名为“区域”,我可以写这个,它会完美地执行。 names = c("Bacilli", "Actinobacteria", "
我正在从 Excel 迁移到 ggvis 进行数据分析。对于具有两个变量的典型分组条形图,但是我很难情节 条形图并排而不是堆叠 . 以下数据有四个步骤 A、B、C、D,其中“比率”数据来自两个特征 c
使用这个小数据集: df % ggvis(x=~variable, y=~value, fill=~colour) %>% group_by(colour) %>% layer_bars(
我是 R 的新手,因此不得不问你一个基本问题。 我有两个大型面板数据集(60 个变量,每个变量代表 30 个国家,范围从 1950 年到 2013 年)。 60个变量同名,数据可能不同也可能不同。 我
我正在尝试让交互式缩放在 ggvis 中工作,尤其是使用画笔进行缩放。从https://github.com/rstudio/ggvis/issues/143判断我认为这应该有效。 我有以下 Shin
我有多个带有多个段的路径。如何用相同的颜色为每条路径的第 N 段着色? 例如: require(dplyr) require(ggvis) df % group_by(group) %>% ggv
我正在尝试使用 ggvis 为 2014 赛季创建 NFL 赛程强度图表。数据来自 FootballOutsiders.com,稍后我将制作一个 Shiny 应用程序,当它在赛季期间更新时,它会自动从
我想为 ggvis 绘图添加标题。我在任何地方都找不到示例。处理其他 R 图很简单,例如 library(ggplot2) library(ggvis) x % ggvis(~x, ~y) %>% l
嗨,我对 ggvis 中的刻度有点困惑。我想做两件事:一是有一个对数刻度(相当于plot()中的log =“x”)。我也在寻找 xlim=c() 的等效项。在这两种情况下,下面的代码都没有给出预期的结
我正在尝试用 ggvis 制作一个箱线图,但我似乎无法查看一个简单的示例 library(dplyr) library(ggplot2) library(shiny) #I think this is
我正在尝试使用来自不同数据帧的数据在 ggvis 图中添加带有任意文本的图例。我试过使用 add_legend() 但我不知道要使用什么参数。使用 plot() 使用 legend() 函数非常简单,
我是一名优秀的程序员,十分优秀!