ggplot2无法识别闪亮的反应数据

时间:2018-03-03 03:24:40

标签: r ggplot2 shiny

大家好!

我在这里努力获取我按“fc”函数分组的数据,并且它应该在这个新的数据集'reac'中,我希望它在用户输入变量时更新他/她想在闪亮的UI中使用(输入$ x,输入$ y,输入$ color)。然后我想在ggplot中使用这个新的rem数据集,但是'aes_string()不起作用,编译器不能将数据类型识别为数据帧而且我得到一个“无法将NULL转换为quosure “错误和”未知输入:tbl_df“错误。

抱歉英语不好,这不是我的箴言! :(

提前致谢!

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)

1 个答案:

答案 0 :(得分:1)

当我加载.Rdata并使用 Alejandro 建议的更改运行代码时,我的图表发生了变化。

加载了除sidrar和rsconnect之外的库,并运行代码:

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

<强>负载(&#34; /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