Shiny app: No error reported, but why the VAR6A_2 is empty?

Multi tool use
Multi tool use


Shiny app: No error reported, but why the VAR6A_2 is empty?



Hey I have a question with shiny. After I click on 'Run App", no error is reported, but the tabpanel VAR 6A_2 is empty.. I tried the code outside the app, the code works. But after I move the code inside the app, no result is showed. So confusing.



Another question is, I need to create more than 30 tabpanels for the app. Each tabpanle has one variable. What makes thing complicated is, for some of the variables, they are computed with their above variables. For example, Var 4 is computed from Var 2 and Var 3.



Here I store all the variables in one app. It is a little confusing because there are so many variables. Is there a better way to organize them?



Thanks a lot.



Here is the code, thanks.


library(shiny)
library(plotly)
#library(d3heatmap)
library(dplyr)
#library(ggplot2)
#library(ggthemes)
#library(gplots)
library(readxl)
library(tidyr)
#source('./utils.R')

Index_2 <- read_xlsx('Index 2.xlsx')
Index_3 <- read_xlsx('Index 3.xlsx')
Index_11 <- read_xlsx('Index 11.xlsx')
whole_country_list <- read_xlsx('whole country list.xlsx')
country_list <- sort(unique(whole_country_list$`Country Name`))

Index_2 <- filter(Index_2, Country %in% whole_country_list$`whole list`) %>%
left_join(whole_country_list, by = c("Country" = "whole list" )) %>%
select(- Country) %>%
select(`Country Name`, everything())
names(Index_2)[names(Index_2) == "Country Name"] <- "Country"
# fill out missing value and predict value for years after 2020
library(zoo)
Index_2 <-cbind(Index_2[c(1:2)], t(apply(Index_2[3:ncol(Index_2)], 1, zoo::na.locf)))


Index_3 <- filter(Index_3, Country %in% whole_country_list$`whole list`) %>%
left_join(whole_country_list, by = c("Country" = "whole list" )) %>%
select(- Country) %>%
select(`Country Name`, everything())
names(Index_3)[names(Index_3) == "Country Name"] <- "Country"


Index_11 <- filter(Index_11, Country %in% whole_country_list$`whole list`) %>%
left_join(whole_country_list, by = c("Country" = "whole list" )) %>%
select(- Country) %>%
select(`Country Name`, everything())
names(Index_11)[names(Index_11) == "Country Name"] <- "Country"
#country_name_list <- select(left_join(country_list, df, by = 'country_code'), country_name)
#series_name_list <- select(left_join(indicator_list, df, by = 'series_code'), series_name)

ui <- shinyUI(fluidPage(

# Application title
titlePanel("Input panel"),

# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("country_in",
"Choose one country:",
choices = country_list,
selected = 'Benin')
),

# Display plot

mainPanel(
tabsetPanel(
tabPanel("VAR 2:Meta data on school system, computed from INDEX 2", dataTableOutput("table_var2")),
tabPanel("VAR 3: Population data, computed from INDEX 11", dataTableOutput("table_var3")),
tabPanel("VAR 6A:Historical enrolment data, computed from INDEX 3",dataTableOutput("table_var6a")),
tabPanel("VAR 6A_2:Historical enrolment data, computed from VAR 6A and VAR 3",dataTableOutput("table_var6a*")),
tabPanel("VAR 7A: Percent repeaters, computed from INDEX 3", dataTableOutput("table_var7a")),
tabPanel("VAR 4: School-age population data, computed from VAR 2 AND 3", dataTableOutput("table_var4"))
)
)
)
)
)


server <- shinyServer(function(input, output) {

data_var2 <- reactive({
out <- dplyr::filter(Index_2, Country == input$country_in)
## fill out missing value in column k using the value in column k-1
#out <- cbind(out[1:2], t(apply(out[3:11], 1, zoo::na.locf)))
out})

data_var3 <- reactive({
out <- filter(Index_11, Country == input$country_in & `INDEX 11: Popuiation indicators` %in%
c("Age population, age 03, female, interpolated",
"Age population, age 03, male, interpolated",
"Age population, age 04, female, interpolated",
"Age population, age 04, male, interpolated",
"Age population, age 05, female, interpolated",
"Age population, age 05, male, interpolated",
"Age population, age 06, female, interpolated",
"Age population, age 06, male, interpolated",
"Age population, age 07, female, interpolated",
"Age population, age 07, male, interpolated",
"Age population, age 08, female, interpolated",
"Age population, age 08, male, interpolated",
"Age population, age 09, female, interpolated",
"Age population, age 09, male, interpolated",
"Age population, age 10, female, interpolated",
"Age population, age 10, male, interpolated",
"Age population, age 11, female, interpolated",
"Age population, age 11, male, interpolated",
"Age population, age 12, female, interpolated",
"Age population, age 12, male, interpolated",
"Age population, age 13, female, interpolated",
"Age population, age 13, male, interpolated",
"Age population, age 14, female, interpolated",
"Age population, age 14, male, interpolated",
"Age population, age 15, female, interpolated",
"Age population, age 15, male, interpolated",
"Age population, age 16, female, interpolated",
"Age population, age 16, male, interpolated",
"Age population, age 17, female, interpolated",
"Age population, age 17, male, interpolated",
"Age population, age 18, female, interpolated",
"Age population, age 18, male, interpolated",
"Age population, age 19, female, interpolated",
"Age population, age 19, male, interpolated",
"Age population, age 20, female, interpolated",
"Age population, age 20, male, interpolated",
"Age population, age 21, female, interpolated",
"Age population, age 21, male, interpolated",
"Age population, age 22, female, interpolated",
"Age population, age 22, male, interpolated",
"Age population, age 23, female, interpolated",
"Age population, age 23, male, interpolated",
"Age population, age 24, female, interpolated",
"Age population, age 24, male, interpolated"
)) %>%
separate(col = `INDEX 11: Popuiation indicators`, into = c("X1", "X2", "X3", "Age", "Sex", "X6", "X7"), sep = ' ')%>%
mutate(
Age = as.numeric(gsub(",","", Age)),
Sex = Hmisc::capitalize(gsub(",","", Sex)))%>%
select(Country, Sex, Age, `2008`:`2050`) %>%
arrange(Sex, Age)
out})

data_var6a <- reactive({
temp1 <- filter(Index_2, Country == input$country_in & `Index 2: Meta indicators` != 'Official entrance age to primary education (years)') %>%
mutate(n_grade = max(colSums(.[,-c(1:2)], na.rm = TRUE)))
temp_seq <- as.data.frame(seq(from = 1, to = unique(temp1$n_grade), by = 1))
colnames(temp_seq)[1] = 'By Grade'
t <- rbind(temp_seq, temp_seq)
out <- dplyr::filter(Index_3, Country == input$country_in &
!`Index 3: Enrolment and repetition` %in%
c("Percentage of repeaters in primary","Percentage of repeaters in secondary")) %>%
filter(rowSums(is.na(.)) != ncol(.) - 4) %>%
filter(Grade != 'All')%>%
arrange(Sex, `Index 3: Enrolment and repetition`, Grade) %>%
select(-Grade) %>%
mutate(`By Grade` = t$`By Grade`)%>%
select(Country, `By Grade`, Sex, `2008`:`2020`)
both <- dplyr::filter(Index_3, Country == input$country_in &
!`Index 3: Enrolment and repetition` %in%
c("Percentage of repeaters in primary","Percentage of repeaters in secondary", "Enrolment in primary", "Enrolment in secondary")) %>%
filter(Grade == 'All') %>%
select(-Grade) %>% rename(`By Grade` = `Index 3: Enrolment and repetition`)
out <- rbind(both, out)
out})

data_var6a_2 <- reactive({


temp1 <- filter(Index_2, Country == input$country_in & `Index 2: Meta indicators` == 'Official entrance age to primary education (years)')
temp2 <- filter(Index_2, Country == input$country_in & `Index 2: Meta indicators` != 'Official entrance age to primary education (years)') %>%
mutate(n_grade = max(colSums(.[,-c(1:2)], na.rm = TRUE)))
for (i in 1:unique(temp2$n_grade)) {
for (j in 3:unique(temp2$n_grade))
temp1[i,j] = temp1[1,j] + seq(0,unique(temp2$n_grade)-1, by = 1)[i]
}
temp1$Country= input$country_in

###### deal with Female first

##Get the population data for the country, the tricky thing here is for some country, the official entrance age to primary education changes, so the corresponding age
# for each grade changes too
data_var3_female <- filter(data_var3(), Sex == 'Female')

temp3 <- data_var3_female %>%
gather(year,value,-c(Country, Sex, Age)) %>%
inner_join(gather(temp1,year, Age, -c(Country, `Index 2: Meta indicators`))) %>%
group_by(Country,year) %>%
mutate(Age_fake = letters[row_number()]) %>%
select(Age_fake,year,value) %>%
spread(year,value) %>%
arrange(Age_fake) %>%
select(-Age_fake)

data_var6a_female <- filter(data_var6a(), Sex == 'Female')

# Find the column number of the last column with non-na value
ind <- !is.na(data_var6a_female)
t1 <- tapply(data_var6a_female[ind], col(data_var6a_female)[ind],tail, 1)
last_non_na_col <- as.numeric(tail(unlist(dimnames(t1)), n = 1))

for (i in 1:nrow(data_var6a_female)) {
for (j in 5:(last_non_na_col+1)) {
if (is.na(data_var6a_female[i,j])) {
data_var6a_female[i,j] = data_var6a_female[i,j-1]*temp3[i,j-2]/temp3[i,j-3]
}
}
}

data_var6a_2_female <- data_var6a_female

out <- data_var6a_2_female

out})


data_var7a <- reactive({
out <- dplyr::filter(Index_3, Country == input$country_in &
`Index 3: Enrolment and repetition` %in%
c("Percentage of repeaters in primary","Percentage of repeaters in secondary")) %>%
arrange(Sex, `Index 3: Enrolment and repetition`, Grade)
out})

data_var4 <- reactive({
out <- as.data.frame(mapply(function(x,y) {
c(y[x[1]-3], #Children of preschool access age, male
y[x[1]+22-3], #children preschool access age, female
y[x[1]-2], #Children of primary entry age, male
y[x[1]+22-2], #children primary entry age, female
sum(y[(x[1]-2):(x[1]-2+x[2]-1)]), #Chilren of primary age, male
sum(y[(x[1]+22-2):(x[1]+22-2+x[2] -1)]), #Children primary age, female
y[x[1]-2-1+x[2]], # Children of primary comleting age, male
y[x[1]+22-2-1+x[2]], #Children primary completing age, female
sum(y[(x[1]+x[2]-2):(x[1]+x[2]-2+x[3]-1)]),# children of lower secondary age, male
sum(y[(x[1]+x[2]+22-2):(x[1]+x[2]+22-2+x[3]-1)]), # children of low secondary age, female
y[x[1]-2 -1+x[2]+x[3]], #children of low secondary comleting age, male
y[x[1]+22-2 -1+x[2]+x[3]], #Children of low secondary completing age, female
sum(y[(x[1]+x[2]+x[3]-2):(x[1]+x[2]-2+x[3]+x[4]-1)]),# Children upper secondary age, male
sum(y[(x[1]+x[2]+x[3]+22-2):(x[1]+x[2]+22-2+x[3]+x[4]-1)]), # Children upper secondary age, female
y[x[1]-2 -1+x[2]+x[3]+x[4]], #children of upper secondary comleting age, male
y[x[1]+22-2 -1+x[2]+x[3]+x[4]], #Children of upper secondary completing age, female
y[15-2],#Children age 15,Male
y[15+22-2],#Children age 15,Female
sum(y[(15-2):(24-2)]),# Population age 15-24 Male
sum(y[(15+22-2):(24+22-2)]), # Population age 15-24 Male
y[24-2],#Children age 15,Male
y[24+22-2]#Children age 15,Female
)},
data_var2()[-c(1:2)],data_var3()[-c(1:3)]))
out <- cbind(c('Children of preschool access age, male',
'children preschool access age, female',
'Children of primary entry age, male',
'children primary entry age, female',
'Chilren of primary age, male',
'Children primary age, female',
'Children of primary comleting age, male',
'Children primary completing age, female',
'children of lower secondary age, male',
'children of low secondary age, female',
'children of low secondary comleting age, male',
'Children of low secondary completing age, female',
'Children upper secondary age, male',
'Children upper secondary age, female',
'children of upper secondary comleting age, male',
'Children of upper secondary completing age, female',
'Children age 15,Male',
'Children age 15,Female',
'Population age 15-24 Male',
'Population age 15-24 female',
'Children age 24,Male',
'Children age 15,Female'
),out)
colnames(out)[1] <- ''
out})

output$table_var2 <- renderDataTable({
data_var2()
})

output$table_var3 <- renderDataTable({
data_var3()
})

output$table_var6a <- renderDataTable({
data_var6a()
})

output$table_var6a_2 <- renderDataTable({
data_var6a_2()
})

output$table_var7a <- renderDataTable({
data_var7a()
})

output$table_var4 <- renderDataTable({
data_var4()
})
})

shinyApp(ui, server)





The thing I like about this question is that it appears to contain all the necessary data and code (thank you!). The thing that's not ideal is that you as 3 different questions, only the first 2 of which are really appropriate for StackOverflow.
– Hack-R
Jul 2 at 0:37






I disagree; the app reads .xlsx spreadsheets which we have no access to, and looks like far too much code, most of which is likely unnecessary to reproduce the problem. I think you'd do well to start a very small shiny with a couple of tabpanels and build it up from scratch.
– SymbolixAU
Jul 2 at 2:03


.xlsx


tabpanels









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

qyBb cR
F4dHRJ TCbxYuQr mwsTfQHxiHqOGnj,Nrei

Popular posts from this blog

Rothschild family

Cinema of Italy