[英]Checkboxgroupinput in R Shiny with Plotly
Love all the help here so far, but having to teach myself R / Shiny, with no one in the office to help, I am unfortunately stuck again!! 到目前为止,我很喜欢这里的所有帮助,但由于必须自学R / Shiny,而且办公室里没人帮助,我很遗憾再次陷入困境!
I am trying to do checkboxgroups in Shiny. 我正在尝试在Shiny中创建复选框组。 I read quite a bit, for example this , this , this , this and this already helped, but I am stuck now.
我读了很多,例如this , this , this , this和this已经有所帮助,但是我现在陷入困境。
So my data set "ConversionsCompletions" looks now something like this : 所以我的数据集“ 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")
server: 服务器:
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)
})
There is a graph, but it doesn't change when I switch boxes, or shows more than one line at a time. 有一个图,但是当我切换框或一次显示多条线时它不会改变。 I know usually you need to add the "add_trace" code for plotly charts, so I am not sure how to do it in this case when sometimes there is one line and sometimes multiple.
我知道通常您需要为绘图图表添加“ add_trace”代码,因此,当有时只有一条线,有时是多条线时,我不确定在这种情况下该如何做。
Any help appreciated!! 任何帮助表示赞赏!
To render a graph properly, you have to use filteredData()
and slightly change syntax. 为了正确地绘制图形,您必须使用
filteredData()
并稍微更改语法。
As the first parameter data
you should use the filtered dataset and then for x
and y
variables the appropriate names with a prefix ~
. 作为第一个参数
data
您应该使用过滤后的数据集,然后对x
和y
变量使用前缀~
的适当名称。
To plot multiple lines you can use another parameter split
. 要绘制多条线,可以使用另一个参数
split
。 I had to change hoverinfo = y
to hoverinfo = "y"
otherwise it didn't work (I have the newest version of plotly) 我必须将
hoverinfo = y
更改为hoverinfo = "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
)
I also used setNames
function to make the code for checkboxGroupInput
shorter. 我还使用
setNames
函数使checkboxGroupInput
的代码更短。
setNames(object = paste0("goal", 1:7),
nm = paste0("Goal ", 1:7))
You don't need the dplyr
function filter
for subsetting - at least in this in this case. 您不需要
dplyr
函数filter
进行子设置-至少在这种情况下如此。
EDITED: 编辑:
I converted the numeric variable date
into a date
format: 我将数字变量
date
转换为date
格式:
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")
Full example: 完整示例:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.