我可以管理创建一个反应式箱形图,该盒装在闪亮中采用两个不同的输入

发布于 2025-02-10 05:12:32 字数 5401 浏览 2 评论 0 原文

我设法在代码中使用两个SelectInput过滤数据。但是,当我尝试在框图和直方图中执行相同的操作时,它似乎并没有过滤。有机会有人帮助我吗?我是闪亮的新手。

我的代码是这样的:

afastamentos <- readr::read_csv("base_afastamentos.csv", locale = locale(encoding = "latin1"))

colnames(afastamentos) <- c(
  "Descrição do Cargo", "Nome do Órgão de Origem", "UF", "Cidade da Residência",
  "Nível da Escolaridade", "Início do Afastamento", "Ano/Mês Referência",
  "Valor do Rendimento Líquido", "Descrição do Afastamento", "Ano Início Afastamento",
  "Mês Início Afastamento", "Rendimento Líquido Hora")


    ui <- dashboardPage(
  
  dashboardHeader(title = "COBRADI", 
                  titleWidth = 500,
                  tags$li(class = "dropdown",
                          tags$a(href = "https://www.ipea.gov.br/portal/index.php?option=com_content&view=article&id=39285&Itemid=343",
                                 icon("globe", lib = "glyphicon"),
                                 "Site COBRADI", 
                                 target = "_blank"))
  ),
  dashboardSidebar(
    sidebarMenu(
      id = "sidebar",

      menuItem("Dataset",
               tabName = "data",
               icon = icon("database")),

      menuItem("Visualização",
               tabName = "viz",
               icon = icon("chart-line")),

      menuItem("Informações",
               tabName = "info",
               icon = icon("info-circle"))
    )
  ),dashboardBody(
  tabItems(
     tabItem(tabName = "viz",
          tabBox(id = "t2", width = 12, 
             tabPanel(title = "Distribuição Amostral",
                          icon = icon("fas fa-chart-area"),
                          value = "trends",
                          
                          fluidRow(
                            column(width = 12,
                                   box(title = "Filtros", width = "100%",
                                       column(width = 6,
                                              box(width = "100%",
                                                  selectizeInput(inputId = "select_UF",
                                                                 label =  "Estados:",
                                                                 choices = c("TODOS", unique(afastamentos$UF)),
                                                                 multiple = T, 
                                                                 selected = "TODOS"))
                                       ),
                                       
                                       column(width = 6,
                                              box(width = "100%",
                                                  selectizeInput(inputId = "descricao_2",
                                                                 label = "Descrição do Afastamento:",
                                                                 choices = c("TODOS", unique(afastamentos$`Descrição do Afastamento`)),
                                                                 multiple = T, options = list(maxItems = 5),
                                                                 selected = "TODOS"))),
                                   )
                            )     
                          ),
                          fluidRow(
                            column(width = 12,
                                   box(title = "BoxPlot - Valor do Rendimento Bruto Mensal",
                                       status = "primary", 
                                       solidHeader = TRUE, 
                                       collapsible = TRUE, 
                                       width = "100%",
                                       plotlyOutput("boxplot"))
                            ),
                          
                          
                          
                            column(width = 12,
                                   box(title = "Histograma - Valor do Rendimento Bruto Mensal",
                                       status = "primary", 
                                       solidHeader = TRUE, 
                                       collapsible = TRUE,
                                       width = "100%",
                                       plotlyOutput("histplot")))
        )
      )
    )
  )
)
  )
)

服务器是:

server <- function(input, output, session){

meus_dados <-  reactive({
    ## filtro UF
    print(input)
    if (! "TODOS" %in% input$select_UF){
      a <- a |> 
        filter(`UF` %in% input$select_UF)
    }
    #Filtro Descricao 
    if(! "TODOS" %in% input$descricao_2){
      a <- a |>
        filter(`Descrição do Afastamento` %in% input$descricao_2)
      return(a)
    }
    })


output$boxplot <- renderPlotly({
    boxplot <- meus_dados()|>
      plot_ly() |>
      add_boxplot(~`Valor do Rendimento Líquido`) |> 
      layout(xaxis = list(title = "Valor do Rendimento Bruto"))
  })

  output$histplot <- renderPlotly({
    hist <- meus_dados() |> 
    plot_ly() |> 
    add_histogram(~`Rendimento Líquido Hora`) |> 
    layout(xaxis = list(title = "Valor da Hora Técnica"))})
}

我会收到以下错误:第一个参数数据必须是数据框架或共享数据。

数据可在此处找到:

I've managed to filter a DataTable with two SelectInput in my code. But, when I try to do the same in my boxplot and histogram it doesn't seem to filter. Is there, by any chance, someone to help me with this? I'm new to shiny.

My code is something like this:

afastamentos <- readr::read_csv("base_afastamentos.csv", locale = locale(encoding = "latin1"))

colnames(afastamentos) <- c(
  "Descrição do Cargo", "Nome do Órgão de Origem", "UF", "Cidade da Residência",
  "Nível da Escolaridade", "Início do Afastamento", "Ano/Mês Referência",
  "Valor do Rendimento Líquido", "Descrição do Afastamento", "Ano Início Afastamento",
  "Mês Início Afastamento", "Rendimento Líquido Hora")


    ui <- dashboardPage(
  
  dashboardHeader(title = "COBRADI", 
                  titleWidth = 500,
                  tags$li(class = "dropdown",
                          tags$a(href = "https://www.ipea.gov.br/portal/index.php?option=com_content&view=article&id=39285&Itemid=343",
                                 icon("globe", lib = "glyphicon"),
                                 "Site COBRADI", 
                                 target = "_blank"))
  ),
  dashboardSidebar(
    sidebarMenu(
      id = "sidebar",

      menuItem("Dataset",
               tabName = "data",
               icon = icon("database")),

      menuItem("Visualização",
               tabName = "viz",
               icon = icon("chart-line")),

      menuItem("Informações",
               tabName = "info",
               icon = icon("info-circle"))
    )
  ),dashboardBody(
  tabItems(
     tabItem(tabName = "viz",
          tabBox(id = "t2", width = 12, 
             tabPanel(title = "Distribuição Amostral",
                          icon = icon("fas fa-chart-area"),
                          value = "trends",
                          
                          fluidRow(
                            column(width = 12,
                                   box(title = "Filtros", width = "100%",
                                       column(width = 6,
                                              box(width = "100%",
                                                  selectizeInput(inputId = "select_UF",
                                                                 label =  "Estados:",
                                                                 choices = c("TODOS", unique(afastamentos$UF)),
                                                                 multiple = T, 
                                                                 selected = "TODOS"))
                                       ),
                                       
                                       column(width = 6,
                                              box(width = "100%",
                                                  selectizeInput(inputId = "descricao_2",
                                                                 label = "Descrição do Afastamento:",
                                                                 choices = c("TODOS", unique(afastamentos

I've managed to filter a DataTable with two SelectInput in my code. But, when I try to do the same in my boxplot and histogram it doesn't seem to filter. Is there, by any chance, someone to help me with this? I'm new to shiny.

My code is something like this:

Descrição do Afastamento`)), multiple = T, options = list(maxItems = 5), selected = "TODOS"))), ) ) ), fluidRow( column(width = 12, box(title = "BoxPlot - Valor do Rendimento Bruto Mensal", status = "primary", solidHeader = TRUE, collapsible = TRUE, width = "100%", plotlyOutput("boxplot")) ), column(width = 12, box(title = "Histograma - Valor do Rendimento Bruto Mensal", status = "primary", solidHeader = TRUE, collapsible = TRUE, width = "100%", plotlyOutput("histplot"))) ) ) ) ) ) ) )

And the server is:

server <- function(input, output, session){

meus_dados <-  reactive({
    ## filtro UF
    print(input)
    if (! "TODOS" %in% input$select_UF){
      a <- a |> 
        filter(`UF` %in% input$select_UF)
    }
    #Filtro Descricao 
    if(! "TODOS" %in% input$descricao_2){
      a <- a |>
        filter(`Descrição do Afastamento` %in% input$descricao_2)
      return(a)
    }
    })


output$boxplot <- renderPlotly({
    boxplot <- meus_dados()|>
      plot_ly() |>
      add_boxplot(~`Valor do Rendimento Líquido`) |> 
      layout(xaxis = list(title = "Valor do Rendimento Bruto"))
  })

  output$histplot <- renderPlotly({
    hist <- meus_dados() |> 
    plot_ly() |> 
    add_histogram(~`Rendimento Líquido Hora`) |> 
    layout(xaxis = list(title = "Valor da Hora Técnica"))})
}

And I get the following error: First argument data must be a data frame or shared data.

Data is available here: https://www.dropbox.com/s/kjilkkskggi27vo/base_afastamentos.csv?dl=0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

埋葬我深情 2025-02-17 05:12:32

您的反应性对象是问题。使用原始名称对我来说很好。

ui <- dashboardPage(
  
  dashboardHeader(title = "COBRADI", 
                  titleWidth = 500,
                  tags$li(class = "dropdown",
                          tags$a(href = "https://www.ipea.gov.br/portal/index.php?option=com_content&view=article&id=39285&Itemid=343",
                                 icon("globe", lib = "glyphicon"),
                                 "Site COBRADI", 
                                 target = "_blank"))
  ),
  dashboardSidebar(
    sidebarMenu(
      id = "sidebar",
      
      menuItem("Dataset",
               tabName = "data",
               icon = icon("database")),
      
      menuItem("Visualização",
               tabName = "viz",
               icon = icon("chart-line")),
      
      menuItem("Informações",
               tabName = "info",
               icon = icon("info-circle"))
    )
  ),dashboardBody(
    tabItems(
      tabItem(tabName = "viz",
              tabBox(id = "t2", width = 12, 
                     tabPanel(title = "Distribuição Amostral",
                              icon = icon("fas fa-chart-area"),
                              value = "trends",
                              
                              fluidRow(
                                column(width = 12,
                                       box(title = "Filtros", width = "100%",
                                           column(width = 6,
                                                  box(width = "100%",
                                                      selectizeInput(inputId = "select_UF",
                                                                     label =  "Estados:",
                                                                     choices = c("TODOS", unique(afastamentos$UF_da_UPAG_de_vinculacao)),
                                                                     multiple = T, 
                                                                     selected = "TODOS"))
                                           ),
                                           
                                           column(width = 6,
                                                  box(width = "100%",
                                                      selectizeInput(inputId = "descricao_2",
                                                                     label = "Descrição do Afastamento:",
                                                                     choices = c("TODOS", unique(afastamentos$Descricao_do_afastamento)),
                                                                     multiple = T, options = list(maxItems = 5),
                                                                     selected = "TODOS"))),
                                       )
                                )     
                              ),
                              fluidRow(
                                column(width = 12,
                                       box(title = "BoxPlot - Valor do Rendimento Bruto Mensal",
                                           status = "primary", 
                                           solidHeader = TRUE, 
                                           collapsible = TRUE, 
                                           width = "100%",
                                           plotlyOutput("boxplot"))
                                ),
                                
                                
                                
                                column(width = 12,
                                       box(title = "Histograma - Valor do Rendimento Bruto Mensal",
                                           status = "primary", 
                                           solidHeader = TRUE, 
                                           collapsible = TRUE,
                                           width = "100%",
                                           plotlyOutput("histplot")))
                              )
                     )
              )
      )
    )
  )
)

server <- function(input, output, session){
  
  meus_dados <-  reactive({
    ## filtro UF
    print(input)
    a <- afastamentos
    if (! "TODOS" %in% input$select_UF){
      a <- a |> 
        filter(UF_da_UPAG_de_vinculacao %in% input$select_UF)
    }
    #Filtro Descricao 
    if(! "TODOS" %in% input$descricao_2){
      a <- a |>
        filter(Descricao_do_afastamento %in% input$descricao_2)
      
    }
    return(a)
  })
  
  
  output$boxplot <- renderPlotly({
    boxplot <- meus_dados()|>
      plot_ly() |>
      add_boxplot(~Valor_rendimento_liquido) |> 
      layout(xaxis = list(title = "Valor do Rendimento Bruto"))
  })
  
  output$histplot <- renderPlotly({
    hist <- meus_dados() |> 
      plot_ly() |> 
      add_histogram(~Rendimento_Liquido_Hora) |> 
      layout(xaxis = list(title = "Valor da Hora Técnica"))})
}

shinyApp(ui = ui, server = server)

Your reactive object was the problem. This works fine for me using the original names.

ui <- dashboardPage(
  
  dashboardHeader(title = "COBRADI", 
                  titleWidth = 500,
                  tags$li(class = "dropdown",
                          tags$a(href = "https://www.ipea.gov.br/portal/index.php?option=com_content&view=article&id=39285&Itemid=343",
                                 icon("globe", lib = "glyphicon"),
                                 "Site COBRADI", 
                                 target = "_blank"))
  ),
  dashboardSidebar(
    sidebarMenu(
      id = "sidebar",
      
      menuItem("Dataset",
               tabName = "data",
               icon = icon("database")),
      
      menuItem("Visualização",
               tabName = "viz",
               icon = icon("chart-line")),
      
      menuItem("Informações",
               tabName = "info",
               icon = icon("info-circle"))
    )
  ),dashboardBody(
    tabItems(
      tabItem(tabName = "viz",
              tabBox(id = "t2", width = 12, 
                     tabPanel(title = "Distribuição Amostral",
                              icon = icon("fas fa-chart-area"),
                              value = "trends",
                              
                              fluidRow(
                                column(width = 12,
                                       box(title = "Filtros", width = "100%",
                                           column(width = 6,
                                                  box(width = "100%",
                                                      selectizeInput(inputId = "select_UF",
                                                                     label =  "Estados:",
                                                                     choices = c("TODOS", unique(afastamentos$UF_da_UPAG_de_vinculacao)),
                                                                     multiple = T, 
                                                                     selected = "TODOS"))
                                           ),
                                           
                                           column(width = 6,
                                                  box(width = "100%",
                                                      selectizeInput(inputId = "descricao_2",
                                                                     label = "Descrição do Afastamento:",
                                                                     choices = c("TODOS", unique(afastamentos$Descricao_do_afastamento)),
                                                                     multiple = T, options = list(maxItems = 5),
                                                                     selected = "TODOS"))),
                                       )
                                )     
                              ),
                              fluidRow(
                                column(width = 12,
                                       box(title = "BoxPlot - Valor do Rendimento Bruto Mensal",
                                           status = "primary", 
                                           solidHeader = TRUE, 
                                           collapsible = TRUE, 
                                           width = "100%",
                                           plotlyOutput("boxplot"))
                                ),
                                
                                
                                
                                column(width = 12,
                                       box(title = "Histograma - Valor do Rendimento Bruto Mensal",
                                           status = "primary", 
                                           solidHeader = TRUE, 
                                           collapsible = TRUE,
                                           width = "100%",
                                           plotlyOutput("histplot")))
                              )
                     )
              )
      )
    )
  )
)

server <- function(input, output, session){
  
  meus_dados <-  reactive({
    ## filtro UF
    print(input)
    a <- afastamentos
    if (! "TODOS" %in% input$select_UF){
      a <- a |> 
        filter(UF_da_UPAG_de_vinculacao %in% input$select_UF)
    }
    #Filtro Descricao 
    if(! "TODOS" %in% input$descricao_2){
      a <- a |>
        filter(Descricao_do_afastamento %in% input$descricao_2)
      
    }
    return(a)
  })
  
  
  output$boxplot <- renderPlotly({
    boxplot <- meus_dados()|>
      plot_ly() |>
      add_boxplot(~Valor_rendimento_liquido) |> 
      layout(xaxis = list(title = "Valor do Rendimento Bruto"))
  })
  
  output$histplot <- renderPlotly({
    hist <- meus_dados() |> 
      plot_ly() |> 
      add_histogram(~Rendimento_Liquido_Hora) |> 
      layout(xaxis = list(title = "Valor da Hora Técnica"))})
}

shinyApp(ui = ui, server = server)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文