I want to generate an animated plotly graph in my shiny app. I get the error message:
First argument, data
, must be a data frame or shared data.
Why is my df not getting recognised?
The data:
dput(LifeExpCH$Age)
c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
111)
> dput(LifeExpCH$Die)
c(380, 16, 11, 9, 9, 8, 7, 7, 7, 8, 7, 7, 8, 8, 8, 10, 11, 13,
16, 19, 20, 20, 21, 20, 19, 21, 20, 21, 23, 24, 25, 27, 30, 31,
35, 37, 41, 44, 48, 52, 57, 63, 70, 76, 84, 94, 104, 115, 129,
143, 159, 176, 195, 215, 237, 258, 283, 307, 334, 363, 392, 424,
458, 495, 534, 578, 624, 677, 734, 798, 869, 952, 1044, 1149,
1271, 1411, 1569, 1750, 1955, 2184, 2440, 2723, 3032, 3363, 3711,
4064, 4404, 4710, 4952, 5106, 5143, 5041, 4795, 4408, 3908, 3342,
2753, 2190, 1679, 1243, 888, 612, 406, 259, 158, 93, 51, 27,
13, 5, 2, 1)
> dput(LifeExpCH_M$Age)
c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110
)
>dput(LifeExpCH_M$Die)
c(413, 16, 13, 11, 11, 9, 9, 8, 7, 8, 8, 8, 10, 11, 15, 18, 26,
33, 43, 51, 55, 57, 54, 51, 50, 48, 48, 49, 49, 51, 52, 55, 57,
61, 65, 69, 73, 79, 85, 92, 100, 108, 118, 130, 141, 156, 172,
190, 209, 231, 256, 282, 312, 345, 379, 418, 458, 503, 551, 601,
656, 715, 776, 842, 911, 985, 1063, 1147, 1237, 1335, 1441, 1557,
1687, 1828, 1987, 2162, 2352, 2558, 2776, 3005, 3239, 3472, 3698,
3903, 4079, 4213, 4290, 4298, 4227, 4072, 3831, 3513, 3129, 2702,
2256, 1820, 1415, 1059, 763, 528, 351, 224, 137, 80, 45, 24,
13, 6, 3, 1, 1)
The code:
library(shiny)
library(shinydashboard)
library(readxl)
library(plotly)
library(dplyr)
library(purrr)
#With this function I want to generate a df showing deaths per age.
Die <- function(Gender) {
if(Gender == 0){
People_D <- LifeExpCH$Die[1:111]
Age_in_Years <- LifeExpCH$Age[1:111]
df2 <- data.frame(Age_in_Years, People_D)
} else {
People_D <- LifeExpCH_M$Die
Age_in_Years <- LifeExpCH_M$Age
df2 <- data.frame(Age_in_Years, People_D)
}
}
header <- dashboardHeader()
sidebar <- dashboardSidebar(
selectInput("Gender", "Please select gender:", c("Female" = 0, "Male" = 1))
)
#Here I want to show the animated plotly graph.
body <- dashboardBody(
box(width = 13, plotlyOutput(outputId = "DI"))
)
UI
ui <- dashboardPage(skin = "blue",
header = header,
sidebar = sidebar,
body = body)
SERVER
server <- function(input, output){
#Here I am using the function from the top to generate the df.
DieN <- reactive({
Die(input$Gender)
})
#HERE I AM USING THE DF, BUT IT IS NOT WORKING?
output$DI <- renderPlotly({
plot_ly(DieN() %>% split(.$Age_in_Years) %>% accumulate(~bind_rows(.x, .y)) %>%
set_names(0:110) %>%
bind_rows(.id = "frame") %>%
plot_ly(x = ~Age_in_Years, y = ~People_D) %>%
mutate(frame = as.numeric(frame)) %>%
add_lines(frame = ~frame, showlegend = FALSE))
})
}
shinyApp(ui = ui, server = server)
Error message:
First argument, data
, must be a data frame or shared data.