Home > OS >  I cannot seem to produce a graph
I cannot seem to produce a graph

Time:11-11

I am trying to create a shiny app. My data set has 45 different countries in a variable called location. I am trying to subset the data to each country while choosing a variable from the sidebar panel to create a scatter plot; when I run the app, the graph doesn't appear. Can you help me where I have gone wrong?

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)
library(plotly)
library(DT)
covid <- read.csv("D:/R/EuropeIndia.csv")
title <- tags$a(href='https://ourworldindata.org/covid-vaccinations?country=OWID_WRL',
                'COVID 19 Vaccinations')

# Define UI for application that draws a histogram
ui <- fluidPage(
  headerPanel(title = title),

    # Application title
    titlePanel("COVID vaccinations: Deaths Vs All variables"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
          selectInput("location", "1. Select a country",
                      choices = covid$location, selectize = TRUE, multiple = FALSE),
          br(),
          helpText("Select variables to plot"),
          selectInput(inputId = "y", label = "Y-axis:",
                      choices = c("total_deaths", "new_deaths"),
                      selected = "Deaths"),
          br(),
          selectInput(inputId = "x", label = "X- axis:",
                      choices = names(c(covid)),
                      selectize = TRUE,
                      selected = "Comparator variables"),
         
          br(),
          helpText("Select the Download Format"),
          radioButtons("type", "2. Format type:",
                       choices = c("Excel (csv)", "Text(tsv)", "Doc")),
          br(),
          helpText("Click on the download button to download dataset"),
          downloadButton("downloadData", "Download"),
          helpText("READ ME: Click on the title to open data source")
            
        ),

        # Show a plot of the generated distribution
        mainPanel(
          textOutput("location"),
          plotlyOutput("scatterplot"),
          tabsetPanel(
            type = "tabs",
            tabPanel("Summary of COVID data", verbatimTextOutput("summary")),
            tabPanel("Dataset", DTOutput("dataset"))
            )
            
        )
    )
)

# Define server logic 
server <- function(input, output) {
  output$location <- renderPrint({
    locationfilter <- subset(covid, covid$location == input$location)
  })
  
  output$scatterplot <- renderPlotly({
    #ggplot(subset(covid, covid$location == input$location),aes(y= input$y,x=input$x)) geom_point()
  plot_ly(subset(covid, covid$location == input$location), y= input$y,x=input$x,
          type = 'scatter', mode = "markers")
    })
  output$summary <- renderPrint({
    summary(covid)
  })
  datasetinput <- reactive({covid})
  fileExt <- reactive({
    switch(input$type,
           "Excel (csv)" = "csv", "Text (tsv)" = "tsv", "Doc" = "doc")
  })
  output$dataset <- renderDT(
    covid, options = list(
      pageLength = 50,
      initComplete = JS('function(setting, json) { alert("done"); }')
    )
  )
  
  output$downloadData <- downloadHandler(
    filename = function(){
      paste("covid", fileExt(),sep = ".")
    },
    content = function(file){
      sep <- switch(input$type,
                    "Excel (csv)" = ",", "Text (tsv)" = "\t", "Doc" = " ")
      write.table(datasetinput(), file, sep = sep, row.names = FALSE)
    }
    
  )
}

# Run the application 
shinyApp(ui = ui, server = server)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

enter image description here

CodePudding user response:

Have a look at the plotly documentation. plot_ly required that you define variables as x = ~variable_name where variable_name is a symbol and not a string, however input$x is a string.

If you first use ggplot, you can make use of the .data[[]] notation which accepts a string. Then you can use something like

output$scatterplot <- renderPlotly({
  ggplotly(
    ggplot(subset(covid, covid$location == input$location),
           aes(y = .data[[input$y]], x = .data[[input$x]]))   geom_point()
  )
})
  • Related