0
votes

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")
       )
     }

   }

})

})

output image below

1
Read about the difference between observe and reactive. Your code contains almost every bad practice you can do in ShinyDSGym
can you please explain which areas I can mend my code. I am new in R and shiny.Advait Kulkarni
stackoverflow.com/questions/53016404/… this is a good read :-)DSGym
I would suggest to break down the code to the problem you have. Seeing all that code does not motivate others to answer the question. Maybe you can shorten the question. For example: is the following really relevant for you question 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
Thank you for heads up, edited now. Please let me know how my problem can be solved ?Advait Kulkarni

1 Answers

1
votes

Like suggested in the comments, taking a look on how to use reactive and observe is a must if you want to use shiny. The official tutorial really talks about it all : https://shiny.rstudio.com/tutorial/

If anyone is interested I got the data from Kaggle: https://www.kaggle.com/gsutters/the-human-freedom-index

What I would do in this case is define card as a reactive value in which the year is filtered, and then use it to render the output, here's what I mean :

Global:

df <-   read.csv("hfi_cc_2018.csv", header = T)

# Replace Null Values
df[is.na(df)] <- 0
df[,5:ncol(df)] <- round(df[,5:ncol(df)], 2)

df1<-  df[, (names(df) %in% 
               c("year", "pf_rol", "pf_ss_homicide", "pf_score"))]

#  added this to global to get the label for the card
select_ops <- c("Rule of Law"= "pf_rol",
                "Homicides Reported" = "pf_ss_homicide")

ui:

library(shiny)
library(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 = c("Rule of Law"= "pf_rol",
                            "Homicides Reported" = "pf_ss_homicide")
    )
  ),

  dashboardBody(
    fluidRow(
      valueBoxOutput("pfrol"),  
      valueBoxOutput("pfrank")
    ),
    fluidRow(
      box(width=15, height=400)
    )
  )
))

server:

library(shiny)
library(dplyr) 

shinyServer(function(input,output){
  card <- reactive(df1 %>%
                     filter(year == input$years))

  output$pfrank <- renderValueBox({
    valueBox(round(mean(card()$pf_score), 1),
             "Personal Freedom Score")
  })

  output$pfrol <- renderValueBox({
    lbl <- names(select_ops)[select_ops==input$variable]
    valueBox(round(mean(card() %>%  pull(input$variable)), 1),
             lbl)
  })

})