I would like to select the options from the drop-down list, which are stored as different attributes in my data frame and use the slider to change the years, so that I could see its effect on the valueCard. Where I am stuck is, to find out, how the pf_rol attribute can be called from df1 data-frame to reflects its value on valueCard.
I have successfully created 2 value cards already which can be manipulated by just a slider, but now I want to use the slider and the selectInput to change the values in ValueCard 'pfrol'.
PS: Would really appreciate if someone help with a cartogram. I have all the names of the countries and would like to manipulate the different attributes along with sliders to observe changing trends just like value cards.
global.r
df <- read.csv("hfi_cc_2018.csv", header = T)
summary(df)
sapply(df, function(x) sum(is.na(x)))
#Replace Null Values
df[is.na(df)] <- 0
df[,5:ncol(df)] <- round(df[,5:ncol(df)], 2)
#adding selective columns new df1
#https://stackguides.com/questions/10085806/extracting-specific-columns-from-a-data-frame
df1<- df[, (names(df) %in% c("year","countries","region","pf_rol", "pf_ss_homicide","pf_ss_disappearances_violent",
))]
ui.r
require(shiny)
require(shinydashboard)
shinyUI(
dashboardPage(
dashboardHeader(title = "Human Freedom Index", titleWidth = 300),
dashboardSidebar(
sliderInput("years","Select Year:",
min = min(df1$year),
max = max(df1$year),
value = min(df1$year),
step = 1),
selectInput("variable","Select Freedom Factor:",
choices = list("Rule of Law"= "pf_rol",
"Homicides Reported" = "pf_ss_homicide")
)
),
dashboardBody(
fluidRow(
valueBoxOutput("pfrol"),
valueBoxOutput("pfrank"),
valueBoxOutput("efrank")
),
fluidRow(
box(plotlyOutput("plot1"), width=15, height=400)
)
)
)
)
server.r
require(shiny)
require(dplyr)
require(shinydashboard)
shinyServer(function(input,output){
observe({
(card <- df1 %>%
filter(year == input$years))
output$pfrank = renderValueBox(
valueBox(round(mean(card$pf_score),1),
"Personal Freedom Score")
)
})
observe({
if(input$variable == "Rule of Law"){
if(filter(df1$year == input$years)){
output$pfrol = renderValueBox(
valueBox(round(mean(df1$pf_rol),1),
"Rule of Law")
)
}
}
})
})
plot_ly(card, x = card$pf_score, y = card$ef_score, text = card$countries, type = 'scatter', color = card$region, colors = 'Paired', mode = 'markers',marker = list( size = 20, opacity = 0.4 )) %>% layout(title = 'Personal _Freedom vs Economical Freedom', xaxis = list(showgrid = FALSE), yaxis = list(showgrid = FALSE))
? – machine