The graph does not appear when I put the shiny to rotate

-1

I have this code for a shiny application. Apparently there is no error in the code and when I select only the graph I can plot, but when I put it 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)
    
asked by anonymous 10.06.2018 / 21:27

1 answer

0

To reproduce the problems I have 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 ui .

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

Once the chart is included, the code has a problem with server . In filter , the Estado column is being compared with the text "input$estado" and not with the estado object of the input list. To get the desired result, simply remove the quotation marks 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)
    
15.06.2018 / 18:50