Search code examples
rshinyshinyapps

How can I structure this specific r-code to get a running shiny-app?


Please mind that this is a question from a total Shinyapp beginner. Im trying to practice for my exam and just can't figure this out my self (I've researched a lot but it just doesn't make sense in my mind)

library(shiny)
library(ggplot2)
library(dplyr)


#Creating the title and an Output with the id "exampleOne"
ui <- fluidPage(
    titlePanel("Titanic"),
    plotOutput(outputId = "exampleOne")
    )


server <- function(input, output) {

    output$exampleOne <- renderPlot({
#opening my dataset of the titanic, mutating the data for the next step
      titanic <- read.csv("titanic_data.csv",header = TRUE, sep=",")
      newdata <- mutate(titanic, Status = ifelse (Survived > 0,
                                                  'Survived',
                                                  'Dead'))
#Trying to create a graph, that shows how many people survived/died separated by sex
      ggplot( newdata, 
              aes(x = Sex, fill = Status)) 
              + geom_bar(position = "fill")
      labs(y= "Proportion")
    })
}


shinyApp(ui = ui, server = server)

As I already said I'm a total beginner and totally lost! R and Shiny are kind of not my thing I still hope someone can help me to get this going.


Solution

  • It is very simple: Though it took me some efforts to find:

    You miss a + in the last but one ggplot() line: Here is an example with the train.csv dataset

    library(shiny)
    library(ggplot2)
    library(dplyr)
    
    
    #Creating the title and an Output with the id "exampleOne"
    ui <- fluidPage(
      titlePanel("Titanic"),
      plotOutput(outputId = "exampleOne")
    )
    
    
    server <- function(input, output) {
      
    
      
      
      output$exampleOne <- renderPlot({
        titanic <- read.csv("train.csv",header = TRUE, sep=",")
        newdata <- mutate(titanic, Status = ifelse (Survived > 0,
                                                    'Survived',
                                                    'Dead')) 
        ggplot( newdata, 
                aes(x = Sex, fill = Status)) +
         geom_bar(position = "fill")+ ### THIS + IS MISSING
        labs(y= "Proportion")
      })
    }
    
    
    shinyApp(ui = ui, server = server)
    

    enter image description here