我有一个闪亮的应用程序,我建立了显示代理排名的用户随着时间的推移。闪亮的应用程序有一个下拉列表,您可以在其中选择一个用户,然后一个新的线图与该用户的数据将被绘制。然而,下面的代码只在5个用户身上进行了测试,这对编写代码来说并不痛苦。但我希望有成千上万的用户。有没有更好的方法来调整这个代码,这样我就不必为每个用户在代码中添加单独的Plotly图了?
数据样本:
devicereceipttime sourceusername GlobalRank
0 2018-12-04T09:26:54.000Z 1111 8507790
1 2018-12-04T09:27:05.000Z 2222 2648
2 2018-12-04T09:27:05.000Z 3333 156433
3 2018-12-04T09:27:10.000Z 5555 295
4 2018-12-04T17:14:51.000Z 1111 1
5 2018-12-04T17:14:51.000Z 4444 1
6 2018-12-04T17:15:11.000Z 2222 373436
7 2018-12-04T17:15:20.000Z 1111 250639
8 2018-12-04T17:15:32.000Z 1111 1
9 2018-12-04T17:15:38.000Z 2444 2965900
10 2018-12-04T17:16:00.000Z 2222 250639
11 2018-12-04T10:52:17.000Z 1111 799963
12 2018-12-04T10:52:26.000Z 3333 1
13 2018-12-04T10:52:26.000Z 1111 799963
14 2018-12-04T17:16:20.000Z 2222 250639
15 2018-12-04T10:52:26.000Z 4444 1
16 2018-12-04T10:52:27.000Z 4444 1
17 2018-12-04T10:52:26.000Z 1111 2648
R代码:
library(shiny)
library(plotly)
library(tidyr)
library(bindrcpp)
ui <- bootstrapPage(
selectInput(inputId = "n_breaks",
label = "Select Data:",
choices = c("User1","User2","User3","User4","User5"),
selected = "User1"),
plotlyOutput(outputId = "main_plot", height = "500px")
)
server <- function(input, output) {
df1 = read.csv("Data/all_5_test_users.csv")
df2 <- df1 %>%
gather(variable, value, -(devicereceipttime:sourceusername)) %>%
unite(temp, sourceusername, variable) %>%
group_by(temp) %>%
mutate(id=1:n()) %>%
spread(temp, value)
df2 <- subset(df2, select = -c(id))
names(df2) = gsub(pattern = "_GlobalRank*", replacement = "", x = names(df2))
output$main_plot <- renderPlotly({
if (input$n_breaks == "User1") {
x <- df2$devicereceipttime[!is.na(df2$`1111`)]
y <- df2$`1111`[!is.na(df2$`1111`)]
plot_ly(x = x, y = y, type = 'scatter', mode = 'lines') %>%
layout(
margin = list(b = 190, l = 50))
}
else if (input$n_breaks == "User2") {
x <- df2$devicereceipttime[!is.na(df2$`2222`)]
y <- df2$`2222`[!is.na(df2$`2222`)]
plot_ly(x = x, y = y, type = 'scatter', mode = 'lines') %>%
layout(
margin = list(b = 190, l = 50))
}
else if (input$n_breaks == "User3") {
x <- df2$devicereceipttime[!is.na(df2$`3333`)]
y <- df2$`3333`[!is.na(df2$`3333`)]
plot_ly(x = x, y = y, type = 'scatter', mode = 'lines') %>%
layout(
margin = list(b = 190, l = 50))
}
else if (input$n_breaks == "User4") {
x <- df2$devicereceipttime[!is.na(df2$`4444`)]
y <- df2$`4444`[!is.na(df2$`4444`)]
plot_ly(x = x, y = y, type = 'scatter', mode = 'lines') %>%
layout(
margin = list(b = 190, l = 50))
}
else if (input$n_breaks == "User5") {
x <- df2$devicereceipttime[!is.na(df2$`5555`)]
y <- df2$`5555`[!is.na(df2$`5555`)]
plot_ly(x = x, y = y, type = 'scatter', mode = 'lines') %>%
layout(
margin = list(b = 190, l = 50))
}
})
}
shinyApp(ui = ui, server = server)
编辑:我尝试了以下方法:
library(shiny)
library(plotly)
library(tidyr)
library(bindrcpp)
ui <- bootstrapPage(
selectInput(choices = c("User1" = "1111", "User2" = "2222", "User3" = "3333", "User4" = "4444", "User5" = "5555")),
plotlyOutput(outputId = "main_plot", height = "500px")
)
server <- function(input, output) {
df1 = read.csv("Data/all_5_test_users.csv")
df2 <- df1 %>%
gather(variable, value, -(devicereceipttime:sourceusername)) %>%
unite(temp, sourceusername, variable) %>%
group_by(temp) %>%
mutate(id=1:n()) %>%
spread(temp, value)
df2 <- subset(df2, select = -c(id))
names(df2) = gsub(pattern = "_GlobalRank*", replacement = "", x = names(df2))
output$main_plot <- renderPlotly({
observeEvent(input$n_breaks,{
x <- df2$devicereceipttime[!is.na(df[[input$n_breaks]])]
y <- df[[input$n_breaks]][!is.na(df[[input$n_breaks]])]
plot_ly(x = x, y = y, type = 'scatter', mode = 'lines') %>%
layout(
margin = list(b = 190, l = 50))
updatePlotly("your plot id")
})
})
}
shinyApp(ui = ui, server = server)
Listening on http://127.0.0.1:7673
Warning: Error in UseMethod: no applicable method for 'ggplotly' applied to an object of class "c('Observer', 'R6')"
Stack trace (innermost first):
82: "plotly"::"ggplotly"
81: func
80: origRenderFunc
79: output$main_plot
4: <Anonymous>
3: do.call
2: print.shiny.appobj
1: <Promise>
Warning: Error in [[: object of type 'closure' is not subsettable
df2
devicereceipttime `1111` `2222` `3333` `4444` `5555`
<fct> <int> <int> <int> <int> <int>
1 2018-12-04T00:00:00.000Z 0 1 0 0 0
2 2018-12-04T00:00:05.000Z 0 1 0 0 0
3 2018-12-04T00:00:24.000Z 0 1 0 0 0
4 2018-12-04T00:00:26.000Z 0 1 0 0 0
5 2018-12-04T00:00:45.000Z 0 1 0 0 0
6 2018-12-04T00:00:50.000Z 0 1 0 0 0
7 2018-12-04T00:01:00.000Z 0 1 0 0 0
8 2018-12-04T00:01:26.000Z 0 1 0 0 0
9 2018-12-04T00:01:45.000Z 0 1 0 0 0
10 2018-12-04T00:01:46.000Z 0 1 0 0 0
x轴是日期时间,y轴是所选用户的计数。