I am building a shiny dashboard showing various life expectancy data.

I have succeeded in creating reactive ValueBoxes and a reactive plot.

In my plot I want to use a reactive output. I get the following error message: replacement has length zero

Everything else is working fine. May I ask for help in this matter?

library(shiny)
library(shinydashboard)
library(ggplot2)
library(readxl)
library(plotly)


LifeExpCH <- read_excel("LifeExpCH.xlsx")
LifeExpCH_M <- read_excel("LifeExpCH_M.xlsx")


LifeExpectancyFromYear <- function(Age, Gender) {
    if(Gender == 0){
        
        Probability_to_live <- LifeExpCH$Plive[1:112]
        Age_in_Years <- seq(0, 111, 1)
        df1 <- data.frame(Age_in_Years, Probability_to_live)
        
    } else {
        
        Probability_to_live <- LifeExpCH_M$Plive[1:111]
        Age_in_Years <- seq(0, 110, 1)
        df1 <- data.frame(Age_in_Years, Probability_to_live)
    }
}


YourExpectancyFromAge <- function(Age, Gender) {
    if(Gender == 0){
        X <- which(LifeExpCH$Age == Age)
        LifeExp <- LifeExpCH$LifeExp[X] + Age
        print(LifeExp)
        
        
    } else {
        X <- which(LifeExpCH_M$Age == Age)
        LifeExp <- LifeExpCH_M$LifeExp[X] + Age
        print(LifeExp)
    }
}

YourProbability <- function(Age, Gender) {
    if(Gender == 0) {
        X <- which(LifeExpCH$Age == Age)
        Prob <- LifeExpCH$Plive[X]
        print(Prob)
        
        
    } else {
        
        X <- which(LifeExpCH_M$Age == Age)
        Prob <- LifeExpCH_M$Plive[X]
        print(Prob)
        
    }
}




header <- dashboardHeader(title = tags$b("Life Expectany in Switzerland, source: BEVNAT, ESPOP"), titleWidth = 750)

sidebar <- dashboardSidebar(
    width = 350,
    sliderInput("Age", "Enter your age:", value = 30, min = 0, max = 111),
    selectInput("Gender", "Enter your gender:", c("Female" = 0, "Male" = 1))
    
)

body <- dashboardBody(
    fluidRow(
        
        box(width = 6, valueBoxOutput(width = 12, "Prob")),
        box(width = 6, valueBoxOutput(width = 12, "LifeExp")),
        
    ),
    
    #Here I include the reactive output ("LifeProb") as I want to use it as input into my reactive plot
    box(width = 12, plotlyOutput(outputId = "Years_to_live")), uiOutput(outputId = "LifeProb")
    
)



ui <- dashboardPage(skin = "blue",
                    header = header,
                    sidebar = sidebar,
                    body = body)




server <- function(input, output){
    
    LifeExpectancy <- reactive({
        LifeExpectancyFromYear(input$Age, input$Gender)
    })
    output$Years_to_live <- renderPlotly({
        
        
        
        plot_ly(LifeExpectancy(), x = ~Age_in_Years, y = ~Probability_to_live, 
        hoverinfo = "text",
        text = ~paste("If age:", Age_in_Years, "<br>", "Your probability to survive:", Probability_to_live)) %>%
        add_lines() %>%
        layout(xaxis = list(title = "Your age in years"),
            yaxis = list(title = "Your propability of surving one more year")) %>%

#yend = here I would like to use the reactive output
            add_segments(x = input$Age, xend = input$Age,  y = 0, yend = LifeProb
                         )
        
        
        
    })
    
    LifeExpectancyText <- reactive({
        YourExpectancyFromAge(input$Age, input$Gender)
    })
    
    output$LifeExp <- renderValueBox({
        valueBox(LifeExpectancyText(), tags$b("is your life expectancy"), color = "blue")
    })
    
#This is the reactive output I want to use for my plot

    LifeProb <- reactive({
        YourProbability(input$Age, input$Gender)
    })
    
    output$Prob <- renderValueBox({
        valueBox(LifeProb(), tags$b("is your probability to live to the next year"), color = "blue")
    })
    
}

shinyApp(ui = ui, server = server)

Hi @Martin123 !

Hard to troubleshoot without at least a mock up data (reprex: FAQ: What's a reproducible example (`reprex`) and how do I do one?).

Question:
What does 'add_lines() %>%' intended to do?
I don't think plotly would like functions called with zero params.

This is what I would like to do:

Hi Martin,
can you do something like :

LifeExpCH <- read_excel("LifeExpCH.xlsx") %>% dplyr::sample_n(30)
LifeExpCH_M <- read_excel("LifeExpCH_M.xlsx")  %>% dplyr::sample_n(30)

dput(LifeExpCH)
dput(LifeExpCH_M)

and provide the result ?

were your responding to me, or another poster Martin ?

Sorry for the confusion Martin,
I was intending for you to share the output to your console of what I wrote.
This would allow the contributors here to attempt to run your code and debug it.

Sorry, to share this? Kind regards,

LifeExpCH <- read_excel("LifeExpCH.xlsx") %>% dplyr::sample_n(30)
LifeExpCH_M <- read_excel("LifeExpCH_M.xlsx") %>% dplyr::sample_n(30)

dput(LifeExpCH)
structure(list(Age = c(54, 22, 23, 40, 68, 6, 46, 36, 82, 91,
44, 77, 63, 95, 100, 27, 103, 15, 25, 19, 14, 104, 90, 75, 98,
67, 31, 73, 32, 64), P_Death = c(0.002431, 0.000204, 2e-04, 0.00058,
0.008046, 7.4e-05, 0.001057, 0.000409, 0.043091, 0.158167, 0.000856,
0.021496, 0.005258, 0.243497, 0.35303, 0.000215, 0.425265, 9.4e-05,
0.000202, 0.000188, 8.5e-05, 0.452829, 0.138922, 0.016712, 0.308856,
0.007362, 0.000275, 0.013243, 0.000296, 0.005709), XXX = c(0.997569,
0.999796, 0.9998, 0.99942, 0.991954, 0.999926, 0.998943, 0.999591,
0.956909, 0.841833, 0.999144, 0.978504, 0.994742, 0.756503, 0.64697,
0.999785, 0.574735, 0.999906, 0.999798, 0.999812, 0.999915, 0.547171,
0.861078, 0.983288, 0.691144, 0.992638, 0.999725, 0.986757, 0.999704,
0.994291), Live = c(97172, 99391, 99370, 98852, 91208, 99567,
98408, 99037, 70359, 31874, 98586, 81411, 94116, 13722, 2515,
99290, 609, 99500, 99331, 99450, 99508, 350, 37017, 84391, 5437,
91885, 99197, 86811, 99170, 93621), Die = c(237, 21, 20, 57,
734, 7, 104, 41, 3032, 5041, 84, 1750, 495, 3342, 888, 21, 259,
10, 21, 19, 8, 158, 5143, 1411, 1679, 677, 27, 1149, 30, 534),
LifeExp = c(31.88, 62.96, 61.97, 45.25, 19.42, 78.84, 39.43,
49.16, 8.62, 4.07, 41.36, 12.14, 23.75, 2.96, 2.08, 58.02,
1.68, 69.89, 60, 65.92, 70.88, 1.56, 4.44, 13.68, 2.38, 20.28,
54.07, 15.27, 53.09, 22.87), Plive = c(99.7569, 99.9796,
99.98, 99.942, 99.1954, 99.9926, 99.8943, 99.9591, 95.6909,
84.1833, 99.9144, 97.8504, 99.4742, 75.6503, 64.697, 99.9785,
57.4735, 99.9906, 99.9798, 99.9812, 99.9915, 54.7171, 86.1078,
98.3288, 69.1144, 99.2638, 99.9725, 98.6757, 99.9704, 99.4291
)), row.names = c(NA, -30L), class = c("tbl_df", "tbl", "data.frame"
))
dput(LifeExpCH_M)
structure(list(Age = c(98, 25, 36, 85, 4, 51, 63, 96, 57, 105,
52, 17, 67, 106, 68, 108, 110, 16, 93, 99, 76, 43, 97, 8, 78,
72, 6, 33, 90, 61), P_Death = c(0.350658, 0.00049, 0.000748,
0.097969, 0.000104, 0.002936, 0.009328, 0.304223, 0.005346, 0.504802,
0.003252, 0.000336, 0.013263, 0.524596, 0.014498, 0.562178, 1,
0.000252, 0.236437, 0.37382, 0.032631, 0.001322, 0.327413, 7.7e-05,
0.04133, 0.021146, 8.6e-05, 0.000617, 0.174938, 0.007786), XXX = c(0.649342,
0.99951, 0.999252, 0.902031, 0.999896, 0.997064, 0.990672, 0.695777,
0.994654, 0.495198, 0.996748, 0.999664, 0.986737, 0.475404, 0.985502,
0.437822, 0, 0.999748, 0.763563, 0.62618, 0.967369, 0.998678,
0.672587, 0.999923, 0.95867, 0.978854, 0.999914, 0.999383, 0.825062,
0.992214), Live = c(2176, 99005, 98401, 43001, 99547, 96261,
90265, 4650, 94067, 48, 95979, 99399, 86464, 24, 85317, 5, 1,
99425, 11428, 1413, 72083, 97746, 3235, 99510, 67173, 79747,
99527, 98596, 21901, 91756), Die = c(763, 48, 73, 4213, 11, 282,
842, 1415, 503, 24, 312, 33, 1147, 13, 1237, 3, 1, 26, 2702,
528, 2352, 130, 1059, 7, 2776, 1687, 9, 61, 3831, 715), LifeExp = c(2.11,
55.82, 45.13, 5.71, 76.48, 30.93, 20.51, 2.42, 25.57, 1.4, 30.02,
63.59, 17.32, 1.33, 16.55, 1.12, 0.5, 64.57, 3.02, 1.98, 10.78,
38.41, 2.25, 72.51, 9.49, 13.56, 74.5, 48.04, 3.82, 22.16), Plive = c(64.9342,
99.951, 99.9252, 90.2031, 99.9896, 99.7064, 99.0672, 69.5777,
99.4654, 49.5198, 99.6748, 99.9664, 98.6737, 47.5404, 98.5502,
43.7822, 0, 99.9748, 76.3563, 62.618, 96.7369, 99.8678, 67.2587,
99.9923, 95.867, 97.8854, 99.9914, 99.9383, 82.5062, 99.2214)), row.names = c(NA,
-30L), class = c("tbl_df", "tbl", "data.frame"))

Hi @Martin123. There are few errors in your code. First, in the plot_ly, the text argument cannot pass the function like this. You can make it easier by adding an extra column for your hover text in LifeExpectancyFromYear function. Second, the reactive expression LifeProb miss a blanket. And the code cannot run in some age inputs (e.g. age=30) but can be run with inputs (age=36 and gender=Female) because when the age input is not exist in your data LifeExpCH or LifeExpCH_M, the YourProbability function will raise error.

library(shiny)
library(shinydashboard)
library(ggplot2)
library(readxl)
library(plotly)


LifeExpCH <- structure(list(Age = c(54, 22, 23, 40, 68, 6, 46, 36, 82, 91,
                                    44, 77, 63, 95, 100, 27, 103, 15, 25, 19, 14, 104, 90, 75, 98,
                                    67, 31, 73, 32, 64), P_Death = c(0.002431, 0.000204, 2e-04, 0.00058,
                                                                     0.008046, 7.4e-05, 0.001057, 0.000409, 0.043091, 0.158167, 0.000856,
                                                                     0.021496, 0.005258, 0.243497, 0.35303, 0.000215, 0.425265, 9.4e-05,
                                                                     0.000202, 0.000188, 8.5e-05, 0.452829, 0.138922, 0.016712, 0.308856,
                                                                     0.007362, 0.000275, 0.013243, 0.000296, 0.005709), XXX = c(0.997569,
                                                                                                                                0.999796, 0.9998, 0.99942, 0.991954, 0.999926, 0.998943, 0.999591,
                                                                                                                                0.956909, 0.841833, 0.999144, 0.978504, 0.994742, 0.756503, 0.64697,
                                                                                                                                0.999785, 0.574735, 0.999906, 0.999798, 0.999812, 0.999915, 0.547171,
                                                                                                                                0.861078, 0.983288, 0.691144, 0.992638, 0.999725, 0.986757, 0.999704,
                                                                                                                                0.994291), Live = c(97172, 99391, 99370, 98852, 91208, 99567,
                                                                                                                                                    98408, 99037, 70359, 31874, 98586, 81411, 94116, 13722, 2515,
                                                                                                                                                    99290, 609, 99500, 99331, 99450, 99508, 350, 37017, 84391, 5437,
                                                                                                                                                    91885, 99197, 86811, 99170, 93621), Die = c(237, 21, 20, 57,
                                                                                                                                                                                                734, 7, 104, 41, 3032, 5041, 84, 1750, 495, 3342, 888, 21, 259,
                                                                                                                                                                                                10, 21, 19, 8, 158, 5143, 1411, 1679, 677, 27, 1149, 30, 534),
                            LifeExp = c(31.88, 62.96, 61.97, 45.25, 19.42, 78.84, 39.43,
                                        49.16, 8.62, 4.07, 41.36, 12.14, 23.75, 2.96, 2.08, 58.02,
                                        1.68, 69.89, 60, 65.92, 70.88, 1.56, 4.44, 13.68, 2.38, 20.28,
                                        54.07, 15.27, 53.09, 22.87), Plive = c(99.7569, 99.9796,
                                                                               99.98, 99.942, 99.1954, 99.9926, 99.8943, 99.9591, 95.6909,
                                                                               84.1833, 99.9144, 97.8504, 99.4742, 75.6503, 64.697, 99.9785,
                                                                               57.4735, 99.9906, 99.9798, 99.9812, 99.9915, 54.7171, 86.1078,
                                                                               98.3288, 69.1144, 99.2638, 99.9725, 98.6757, 99.9704, 99.4291
                                        )), row.names = c(NA, -30L), class = c("tbl_df", "tbl", "data.frame"
                                        ))
LifeExpCH_M <- structure(list(Age = c(98, 25, 36, 85, 4, 51, 63, 96, 57, 105,
                                      52, 17, 67, 106, 68, 108, 110, 16, 93, 99, 76, 43, 97, 8, 78,
                                      72, 6, 33, 90, 61), P_Death = c(0.350658, 0.00049, 0.000748,
                                                                      0.097969, 0.000104, 0.002936, 0.009328, 0.304223, 0.005346, 0.504802,
                                                                      0.003252, 0.000336, 0.013263, 0.524596, 0.014498, 0.562178, 1,
                                                                      0.000252, 0.236437, 0.37382, 0.032631, 0.001322, 0.327413, 7.7e-05,
                                                                      0.04133, 0.021146, 8.6e-05, 0.000617, 0.174938, 0.007786), XXX = c(0.649342,
                                                                                                                                         0.99951, 0.999252, 0.902031, 0.999896, 0.997064, 0.990672, 0.695777,
                                                                                                                                         0.994654, 0.495198, 0.996748, 0.999664, 0.986737, 0.475404, 0.985502,
                                                                                                                                         0.437822, 0, 0.999748, 0.763563, 0.62618, 0.967369, 0.998678,
                                                                                                                                         0.672587, 0.999923, 0.95867, 0.978854, 0.999914, 0.999383, 0.825062,
                                                                                                                                         0.992214), Live = c(2176, 99005, 98401, 43001, 99547, 96261,
                                                                                                                                                             90265, 4650, 94067, 48, 95979, 99399, 86464, 24, 85317, 5, 1,
                                                                                                                                                             99425, 11428, 1413, 72083, 97746, 3235, 99510, 67173, 79747,
                                                                                                                                                             99527, 98596, 21901, 91756), Die = c(763, 48, 73, 4213, 11, 282,
                                                                                                                                                                                                  842, 1415, 503, 24, 312, 33, 1147, 13, 1237, 3, 1, 26, 2702,
                                                                                                                                                                                                  528, 2352, 130, 1059, 7, 2776, 1687, 9, 61, 3831, 715), LifeExp = c(2.11,
                                                                                                                                                                                                                                                                      55.82, 45.13, 5.71, 76.48, 30.93, 20.51, 2.42, 25.57, 1.4, 30.02,
                                                                                                                                                                                                                                                                      63.59, 17.32, 1.33, 16.55, 1.12, 0.5, 64.57, 3.02, 1.98, 10.78,
                                                                                                                                                                                                                                                                      38.41, 2.25, 72.51, 9.49, 13.56, 74.5, 48.04, 3.82, 22.16), Plive = c(64.9342,
                                                                                                                                                                                                                                                                                                                                            99.951, 99.9252, 90.2031, 99.9896, 99.7064, 99.0672, 69.5777,
                                                                                                                                                                                                                                                                                                                                            99.4654, 49.5198, 99.6748, 99.9664, 98.6737, 47.5404, 98.5502,
                                                                                                                                                                                                                                                                                                                                            43.7822, 0, 99.9748, 76.3563, 62.618, 96.7369, 99.8678, 67.2587,
                                                                                                                                                                                                                                                                                                                                            99.9923, 95.867, 97.8854, 99.9914, 99.9383, 82.5062, 99.2214)), row.names = c(NA,
                                                                                                                                                                                                                                                                                                                                                                                                                          -30L), class = c("tbl_df", "tbl", "data.frame"))


LifeExpectancyFromYear <- function(Age, Gender) {
  if(Gender == 0){
    
    Probability_to_live <- LifeExpCH$Plive[1:112]
    Age_in_Years <- seq(0, 111, 1)
    df1 <- data.frame(Age_in_Years, Probability_to_live, text = paste("If age:", Age_in_Years, "<br>", "Your probability to survive:", Probability_to_live))
    
  } else {
    
    Probability_to_live <- LifeExpCH_M$Plive[1:111]
    Age_in_Years <- seq(0, 110, 1)
    df1 <- data.frame(Age_in_Years, Probability_to_live)
  }
}


YourExpectancyFromAge <- function(Age, Gender) {
  if(Gender == 0){
    X <- which(LifeExpCH$Age == Age)
    LifeExp <- LifeExpCH$LifeExp[X] + Age
    print(LifeExp)
    
    
  } else {
    X <- which(LifeExpCH_M$Age == Age)
    LifeExp <- LifeExpCH_M$LifeExp[X] + Age
    print(LifeExp)
  }
}

YourProbability <- function(Age, Gender) {
  if(Gender == 0) {
    X <- which(LifeExpCH$Age == Age)
    Prob <- LifeExpCH$Plive[X]
    print(Prob)
    
    
  } else {
    
    X <- which(LifeExpCH_M$Age == Age)
    Prob <- LifeExpCH_M$Plive[X]
    print(Prob)
    
  }
}




header <- dashboardHeader(title = tags$b("Life Expectany in Switzerland, source: BEVNAT, ESPOP"), titleWidth = 750)

sidebar <- dashboardSidebar(
  width = 350,
  sliderInput("Age", "Enter your age:", value = 30, min = 0, max = 111),
  selectInput("Gender", "Enter your gender:", c("Female" = 0, "Male" = 1))
  
)

body <- dashboardBody(
  fluidRow(
    
    box(width = 6, valueBoxOutput(width = 12, "Prob")),
    box(width = 6, valueBoxOutput(width = 12, "LifeExp")),
    
  ),
  
  #Here I include the reactive output ("LifeProb") as I want to use it as input into my reactive plot
  box(width = 12, plotlyOutput(outputId = "Years_to_live")), uiOutput(outputId = "LifeProb")
  
)



ui <- dashboardPage(skin = "blue",
                    header = header,
                    sidebar = sidebar,
                    body = body)




server <- function(input, output){
  
  LifeExpectancy <- reactive({
    LifeExpectancyFromYear(input$Age, input$Gender)
  })
  output$Years_to_live <- renderPlotly({

    plot_ly(LifeExpectancy(), x = ~Age_in_Years, y = ~Probability_to_live, hoverinfo = "text",
            text = ~text) %>%
      add_lines() %>%
      layout(xaxis = list(title = "Your age in years"),
             yaxis = list(title = "Your propability of surving one more year")) %>%
      add_segments(x = input$Age, xend = input$Age,  y = 0, yend = LifeProb())
  })
  
  LifeExpectancyText <- reactive({
    YourExpectancyFromAge(input$Age, input$Gender)
  })
  
  output$LifeExp <- renderValueBox({
    valueBox(LifeExpectancyText(), tags$b("is your life expectancy"), color = "blue")
  })
  
  #This is the reactive output I want to use for my plot
  
  LifeProb <- reactive({
    YourProbability(input$Age, input$Gender)
  })
  
  output$Prob <- renderValueBox({
    valueBox(LifeProb(), tags$b("is your probability to live to the next year"), color = "blue")
  })
  
}

shinyApp(ui = ui, server = server)

I found the solution!! Everything good now.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.