1

everyone!

I'm trying hard here to get the data I grouped by the 'fc' function and it was supposed to be in this new dataset 'reac', where I wanted it to be updated when a user input which variable he/she wants to use at shiny's UI (input$x, input$y, input$color). Then I want to use this new reac dataset in ggplot, but the 'aes_string() isn't working, the compiler doesn't recognize the data type as a dataframe and I'm getting a "Can't convert NULL to a quosure" error and "Unknown input:tbl_df" error.

Sorry for the bad english, it isn't my mothertongue! :(

Thanks in advance!

PS: Link for the code

library(shiny)
library(plotly)
library(rsconnect)
library(sidrar)
library(dplyr)
library(ggplot2)
#dados261<-get_sidra(api="/t/261/n2/all/n3/all/v/allxp/p/last%2011/c1/allxt/c2/allxt/c58/1140,1141,1142,1143,1144,1145,1146,1147,1148,1149,1150,1151,1152,1153,2792,2793,3244,3245/d/v93%203")
load("C:/Users/Fausto/Desktop/dados_1.RData")

colnames(dados261)<-c("nt_cod","nt","regiao_cod","regiao","va_cod","va","ano_cod","ano","dom_cod","dom","sexo_cod","sexo","id_cod","id","um_cod","um","valor")

names1 <- c("nt_cod","regiao_cod","va_cod","ano_cod","dom_cod","sexo_cod","id_cod","um_cod")
dados261[names1] <- sapply(dados261[names1],as.numeric)



years<-as.numeric(sort(unique(dados261$ano)))



ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      h1("Tabela 261 - SIDRA - Dados Gerais"),
      selectInput("x", label = "Eixo x", choices = list("Região" = "regiao", "Idade" = "id", "Sexo" = "sexo", "Domicílio" = "dom", "Valores" = "valor"), selected = "regiao"),
      selectInput("y", label = "Eixo Y", choices = list("Região" = "regiao", "Idade" = "id", "Sexo" = "sexo", "Domicílio" = "dom", "Valores" = "valor"), selected = "valor")
),
    mainPanel(
      tabsetPanel(
        tabPanel("Gráfico de barras", 
                 plotOutput("plot", width = "80%", height = "80%"), 
                 radioButtons("color", label = "Preenchimento", choices = list("Região" = "regiao", "Idade" = "id", "Sexo" = "sexo", "Domicílio" = "dom", "Nenhuma" = "id"), selected = "nn", inline = TRUE)),
        tabPanel("Série Temporal", 
                 plotOutput("plot2", width = "100%", height = "100%"),
                 sliderInput("ano", label = "Anos", min = min(years), max= max(years), value=c(min(years),max(years)))


        )
      )
    )
  )
)


server <- function(input, output){

  fc<- function(data, ...) {
    data %>% group_by_(...) %>%
      summarise(valor2 = sum(valor, na.rm = TRUE)) -> data 
    return(data)
  }

  reac<-reactive({
   fc(dados261, input$x, input$y, input$color) 
  })

  output$plot <- renderPlot({
    eixox<-as.character(reac()[,1])
    eixoy<-as.numeric(reac()$valor2)
    eixoz<-as.character(reac()[,2])

   p<- reac() %>%
      ggplot() +
      aes_q(eixox, eixoy, fill= eixoz) + 
      geom_bar(stat = "identity")     

   ggplotly(p)
  }, height = 600, width = 900)



}


shinyApp(ui = ui, server = server)
3
  • you don't have an input name color so it input$color is NULL and that's why you are getting the error Commented Mar 3, 2018 at 3:32
  • Thanks for your help, Alejandro! I managed to fix it, but now this is happening. Every graph is exactly the same, it doesn't matter which input I choose! Commented Mar 3, 2018 at 4:06
  • 2
    glas to help you find you update your code and make it reproduzable with out the need of Dropbox data Commented Mar 3, 2018 at 4:33

1 Answer 1

1

When I loaded your .Rdata, and ran the code with changes suggested by Alejandro, my graphs changed.

loaded the libraries excluding sidrar and rsconnect, and ran the code:

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

load("/Users/username/Downloads/RData.RData")

#Renomeando as colunas
colnames(dados261)<-c("nt_cod","nt","regiao_cod","regiao","va_cod","va","ano_cod","ano","dom_cod","dom","sexo_cod","sexo","id_cod","id","um_cod","um","valor")

#transformando as variáveis que estao como "char" em "integer"
names1 <- c("nt_cod","regiao_cod","va_cod","ano_cod","dom_cod","sexo_cod","id_cod","um_cod")
dados261[names1] <- sapply(dados261[names1],as.numeric)



years<-as.numeric(sort(unique(dados261$ano)))



ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      h1("Tabela 261 - SIDRA - Dados Gerais"),
      selectInput("x", label = "Eixo x", choices = list("Regiao" = "regiao", "Idade" = "id", "Sexo" = "sexo", "Domicilio" = "dom", "Valores" = "valor"), selected = "regiao"),
      selectInput("y", label = "Eixo Y", choices = list("Regiao" = "regiao", "Idade" = "id", "Sexo" = "sexo", "Domicilio" = "dom", "Valores" = "valor"), selected = "valor")
    ),
    mainPanel(
      tabsetPanel(
        tabPanel("Grafico de barras", 
                 plotOutput("plot", width = "80%", height = "80%"), 
                 radioButtons("color", label = "Preenchimento", choices = list("Regiao" = "regiao", "Idade" = "id", "Sexo" = "sexo", "Domicilio" = "dom"), selected = "id", inline = TRUE)),
        tabPanel("Serie Temporal", 
                 plotOutput("plot2", width = "100%", height = "100%"),
                 sliderInput("ano", label = "Anos", min = min(years), max= max(years), value=c(min(years),max(years)))


        )
      )
    )
  )
)


server <- function(input, output){

  fc<- function(data, ...) {
    data %>% group_by_(...) %>%
      summarise(valor2 = sum(valor, na.rm = TRUE)) -> data 
    return(data)
  }

  reac<-reactive({
    fc(dados261, input$x, input$y, input$color) 
  })


  output$plot <- renderPlot({
    eixox<-as.character(reac()[,1])
    eixoy<-as.numeric(reac()$valor2)
    eixoz<-as.character(reac()[,3])

    reac() %>%
      ggplot() +
      aes_string(eixox, eixoy, fill= eixoz) + 
      geom_bar(stat = "identity")   

  }, height = 600, width = 900)



}

shinyApp(ui,server)

enter image description here

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.