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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ggplot2无法识别闪亮的反应数据相关的知识,希望对你有一定的参考价值。

大家!

我正努力在这里获取我按'fc'函数分组的数据,它应该在这个新的数据集'reac'中,我希望在用户输入他/她想要的变量时更新它在shiny的UI中使用(输入$ x,输入$ y,输入$ color)。然后我想在ggplot中使用这个新的重新数据集,但是'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)
答案

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

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

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

负载( “/用户/用户名/下载/ 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

以上是关于ggplot2无法识别闪亮的反应数据的主要内容,如果未能解决你的问题,请参考以下文章

如何在闪亮中使用 ggplot2 绘制数据框的子集

如何在R中闪亮的数据帧中强制反应

由于反应性错误,闪亮的 RStudio 应用程序无法正常工作

闪亮的:如何在闪亮的应用程序中添加反应栏

观察事件中闪亮的反应数据集

对 GGplot2 使用反应式数据集?