selectInput selected="not working"

Hi there, I am starting in R shiny recently, so I may have some things I cannot catch up yet… Please help me with some doubts…
This is a Little adaptation I did for my job to ask you for help. This are my problems:

1- "selected" in selectInput is not working. I told it to show Spain on the plot and it showing any country...
2- When I run it, the tab "data" is showing number with zeros like "243.00". I tried different things to remove the zeros and I can´t remove them.
3- It is just for the ggplot. I would like the lines color up like c("Borns"="green", "Deaths"= "red" ,"Hospitalized"="blue") in that order. It is just for a better visualization.

If anyone can help with any of my problems, I would apreciate it so much.

Rubén.

#CODE

library(dplyr)

Countries<- c("Spain", "Germany", "Spain", "Germany","Spain", "Germany","Spain", "Germany")
Time<- c("01/05/2020","01/05/2020","02/05/2020","02/05/2020","03/05/2020","03/05/2020","04/05/2020","04/05/2020")
Borns<- c(294,154,231,137,246,149,259,145)
Deaths<-c(120,73,147,87,124,90,156,96)
Hospitalised<- c(185,76,210,106,197,78,198,67)

data_baby<- data.frame(Countries,Time,Borns,Deaths,Hospitalised)
str(data_baby)

#UI
ui <- fluidPage(
    titlePanel(h4("Evolution of population by Country", align = "center")),
    sidebarLayout(    
        sidebarPanel(
            
            selectInput("country",
                        "Choose a country:", 
                        choices = data_baby$Countries, 
                        selected = "Spain")),
        
        
        mainPanel(
            
            tabsetPanel(type="tab",
                        tabPanel("Data", tableOutput("tab")),
                        tabPanel("Plot", textOutput("obs"), plotOutput("myplot")))
            
            
        )
        
    )
    
)




#Server
library(ggplot2)
library(tidyverse)
library(lubridate)


server <- function(input, output) {
    output$obs <- renderText(
        paste("Time series of ", input$country)
    )
    
    
    output$tab <- renderTable({
        data_baby %>%
            filter(.,Countries %in% input$country)
        
    })
    
    
    output$myplot <- renderPlot({
        
        df <- data_baby %>%
            filter(.,Countries %in% input$country)
        
        df$Countries <- NULL
        
        df1 <- df %>% 
            gather(Variable, Value, -Time) %>%
            mutate(Time = mdy(paste0(Time)))
        
        
        max_values <- df1 %>% 
            group_by(Variable) %>% 
            filter(Value == max(Value))
        
        
        df1 %>% 
            ggplot(aes(x = Time, y = Value))+
            geom_line(aes(color = Variable), size=2) +
            geom_point(data = max_values) +
            geom_segment(data = max_values,
                         aes(xend =Time, yend = 0),
                         linetype = "dashed") +
            geom_text(data = max_values,
                      aes(label = Value),
                      vjust = -1) +
            labs(title = "Evolution of population by countries") +
            scale_color_discrete(name = "Data Series",
                                 labels = c("Borns", "Deaths", "Hospitalised")) +
            
            coord_cartesian(clip = "off") +
            theme(axis.text.x = element_text(angle = 90, hjust = 1),
                  plot.title = element_text(hjust = 0.5))        
        
    })
}


shinyApp(ui, server)

I fixed, 243.00 type printing by using DT tables instead of default shiny table, it has better default printing.
I used scale_color_manual rather than scale_color_discrete so I could dictate the color values corresponding with the labels.
I couldnt fix your spain / germany issue, because when I asked for spain I got spain, and when germany i got germany, ... i.e. it worked already.

library(dplyr)
library(shiny)
Countries<- c("Spain", "Germany", "Spain", "Germany","Spain", "Germany","Spain", "Germany")
Time<- c("01/05/2020","01/05/2020","02/05/2020","02/05/2020","03/05/2020","03/05/2020","04/05/2020","04/05/2020")
Borns<- c(294,154,231,137,246,149,259,145)
Deaths<-c(120,73,147,87,124,90,156,96)
Hospitalised<- c(185,76,210,106,197,78,198,67)

data_baby<- data.frame(Countries,Time,Borns,Deaths,Hospitalised)
str(data_baby)

#UI
ui <- fluidPage(
  titlePanel(h4("Evolution of population by Country", align = "center")),
  sidebarLayout(    
    sidebarPanel(
      
      selectInput("country",
                  "Choose a country:", 
                  choices = data_baby$Countries, 
                  selected = "Spain")),
    
    
    mainPanel(
      
      tabsetPanel(type="tab",
                  tabPanel("Data", dataTableOutput("tab")),
                  tabPanel("Plot", textOutput("obs"), plotOutput("myplot")))
      
      
    )
    
  )
  
)




#Server
library(ggplot2)
library(tidyverse)
library(lubridate)
library(DT)

server <- function(input, output) {
  output$obs <- renderText(
    paste("Time series of ", input$country)
  )
  
  
  output$tab <- renderDT({
    data_baby %>%
      filter(.,Countries %in% input$country)
    
  })
  
  
  output$myplot <- renderPlot({
    
    df <- data_baby %>%
      filter(.,Countries %in% input$country)
    
    df$Countries <- NULL
    
    df1 <- df %>% 
      gather(Variable, Value, -Time) %>%
      mutate(Time = mdy(paste0(Time)))
    
    
    max_values <- df1 %>% 
      group_by(Variable) %>% 
      filter(Value == max(Value))
    
    
    df1 %>% 
      ggplot(aes(x = Time, y = Value))+
      geom_line(aes(color = Variable), size=2) +
      geom_point(data = max_values) +
      geom_segment(data = max_values,
                   aes(xend =Time, yend = 0),
                   linetype = "dashed") +
      geom_text(data = max_values,
                aes(label = Value),
                vjust = -1) +
      labs(title = "Evolution of population by countries") +
      scale_color_manual(name = "Data Series",
                         values = c("Green","Red","Blue"),
                           labels = c("Borns", "Deaths", "Hospitalised")) +
      
      coord_cartesian(clip = "off") +
      theme(axis.text.x = element_text(angle = 90, hjust = 1),
            plot.title = element_text(hjust = 0.5))        
    
  })
}


shinyApp(ui, server)

Thank you very much!! That was really helpful!! With the selected value I don´t know what is happening....I´ll continue trying because it´s stil not working to me... But you solved my other 2 problems so I´m really glad.

Rubén.

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