The graph doesn’t appear when I put Shiny to run

Asked

Viewed 202 times

-1

I have this code for a Shiny app. Apparently there is no error in the code and when I select only the graph I can plot, but when I put to run the graph does not appear .

cat(getwd())
library(shiny)
library(leaflet)
library(tidyverse)
library(dplyr)
library(ggplot2)

dados <- ipeadata_taxa_desemprego_05_06_2018_10_39_ 


ui <- fluidPage(


   titlePanel("Porcentagem de Pessoas Desempregadas por Estado"),

   fluidRow(
     selectInput("estado", label = ("Estado1"),
                 choices = list("NULL", "Acre" , "Alagoas", "Amapá", "Amazonas", "Bahia", "Ceará" , "Brasília" , "Espírito Santo", "Goiás",                   "Maranhão", "Mato Grosso do Sul" ,"Cuiabá", "Minas Gerais",                               "Pará", "Paraíba", "Paraná" , "Pernambuco", "Piauí",                                "Rio de Janeiro", "Rio Grande do Norte", "Rio Grande do Sul",                                "Rondônia", "Roraima", "São Paulo", "Santa Catarina",                                "Sergipe", "Tocantins"
                 ),selected = "Acre"
     )
   )
   )
   mainPanel(
     plotOutput("app")
   )


server <- function(input, output) {

     output$app <- renderPlot({

      dados_plot <- dados %>% 
        select(., -Sigla, -Código, -X9) %>%  ### tira as colunas que não vai precisar
        filter(Estado == "input$estado") %>%  ### seleciona o estado
        gather("Anos", "Valor", 2:6) %>%    ### transforma as colunas em linha
         mutate("Anos" = as.factor(Anos))   ### trnasforma num fator

      ggplot(dados_plot, aes(x = reorder(Anos,Valor), y = Valor, fill = Estado)) + 
      geom_bar(stat = "identity") + coord_flip() +
      labs(x = "Anos", y = "Porcentagem (%)", title = "Estado")
   })
}

# Run the application 
shinyApp(ui = ui, server = server)
  • Welcome to Stackoverflow. Could you tell us the error message you receive? And also copy and paste the result of dput(head(dados, 30))? Check out these and other tips how to improve your question.

1 answer

1

To reproduce the problem I created data as follows:

dados <- data_frame(
  Estado =  c("Acre" , "Alagoas", "Amapá", "Amazonas", "Bahia", "Ceará"),
  Sigla = NA, Código = NA, X9 = NA, # código vai remover
  `1995` = abs(rnorm(6)) * 1000, `2000` = abs(rnorm(6)) * 1000, 
  `2005` = abs(rnorm(6)) * 1000, `2010` = abs(rnorm(6)) * 1000, 
  `2015` = abs(rnorm(6)) * 1000
)

There are some problems in the code provided in the question. But the one that motivates the question is in the ui.

In the code provided, the mainPanel(...) is after closing the fluidPage(...) and why it is not included in the page (object ui).

Once the chart is included, the code presents a problem in the server. In the filter, the column Estado is being compared with the text "input$estado" and not with the object estado of the list input. To get the desired result, simply remove the quotes around the object name.

Finally, the code that reproduces the solution is this:

library(shiny)
library(tidyverse)

ui <- fluidPage(
  titlePanel("Porcentagem de Pessoas Desempregadas por Estado"),

  fluidRow(
    selectInput("estado", label = ("Estado1"),
                choices = unique(dados$Estado),
                selected = "Acre"
    ), 
    mainPanel(
      plotOutput("app")
    )
  )
)

server <- function(input, output) {
  output$app <- renderPlot({
    dados_plot <- dados %>% 
      select(., -Sigla, -Código, -X9) %>%  ### tira as colunas que não vai precisar
      filter(Estado == input$estado) %>%  ### seleciona o estado
      gather(Anos, Valor, 2:6) %>%    ### transforma as colunas em linha
      mutate(Anos = as.factor(Anos))   ### trnasforma num fator

    ggplot(dados_plot, aes(x = reorder(Anos, Valor), y = Valor, fill = Estado)) + 
      geom_bar(stat = "identity") + coord_flip() +
      labs(x = "Anos", y = "Porcentagem (%)", title = "Estado")
  })
}

shinyApp(ui = ui, server = server)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.