【问题标题】:Reactive changes in sliderInput for subsets子集的sliderInput中的反应性变化
【发布时间】:2017-01-28 18:31:29
【问题描述】:

我正在尝试开发一个反应式滑块,但我不明白为什么它不起作用并收到错误消息:“警告:[.data.frame: 未定义列已选择”中的错误。 任何帮助将不胜感激。

到目前为止,我已经尝试使用 uiOutput("slider") 从服务器调用对象。

ui.r

    library(shiny)
    DF <- readRDS("data/SF.rds")
    shinyUI(fluidPage(
    titlePanel("Cartera Total - Bancos"),

   sidebarLayout(
   sidebarPanel(
   helpText("Evolución de la cartera total según entidad bancaria"),

  selectInput("var", 
    label = "Entidad Financiera",
    choices = c('B. AZTECA',
                'B. CENCOSUD PERU',
                'B. CONTINENTAL',
                'B. DE COMERCIO',
                'B. DE CREDITO DEL PERU',
                'B. FALABELLA PERU',
                'B. FINANCIERO',
                'B. GNB',
                'B. ICBC',
                'B. INTERAMERICANO DE FINANZAS',
                'B. RIPLEY',
                'B. SANTANDER PERU',
                'CITIBANK',
                'INTERBANK',
                'MIBANCO',
                'SCOTIABANK PERU'),
    selected = "BANCO AZTECA"),

    uiOutput("slider")

),

mainPanel(
    fluidRow(
        column(12,
            splitLayout(cellWidths = c("50%", "50%"),
                        plotlyOutput("deuda_dir"),
                        plotlyOutput("deuda_mora"))
        )
        ,
        column(10,
            tabsetPanel(id = 'Entidad',
                        DT::dataTableOutput("tabla")
        ))
    )
  )
 )
))

server.r:

   library(shiny)
   library(plotly)
   library(ggplot2)
   library(scales)

    DF <- readRDS("data/SF.rds")

    ban_sit <- function(df){
    # Seleccionas y luego : Ctrl+R
   p <- ggplot(data = df, 
          aes(x = fec_cierre,
              y = TotalCreditosDirectos/1000)) +
geom_line(colour = "midnightblue")+
scale_y_continuous(labels = comma)+
xlab("Fecha de Cierre")+
ylab("Créditos Directos (En MM de Soles)")
gg <- ggplotly(p)
gg
}
ban_mora <- function(df){
p <- ggplot(data = df, 
          aes(x = fec_cierre,
              y = Deuda_Mora_porc)) +
geom_line(colour = "firebrick4")+
scale_y_continuous(labels = comma)+
xlab("Fecha de Cierre")+
ylab("Ratio de Mora (%)")
gg <- ggplotly(p)
gg
 }


  shinyServer(
  function(input, output) {
    tabla_seg <- reactive({
    args <- switch(input$var,
    'B. AZTECA'='B001', # Solo entidades activas
    'B. CENCOSUD PERU'='B002',
    'B. CONTINENTAL'='B003',
    'B. DE COMERCIO'='B004',
    'B. DE CREDITO DEL PERU'='B005',
    'B. FALABELLA PERU'='B007',
    'B. FINANCIERO'='B008',
    'B. GNB'='B009',
    'B. ICBC'='B010',
    'B. INTERAMERICANO DE FINANZAS'='B011',
    'B. RIPLEY'='B012',
    'B. SANTANDER PERU'='B014',
    'CITIBANK'='B020',
    'INTERBANK'='B023',
    'MIBANCO'='B024',
    'SCOTIABANK PERU'='B025')

  tabla_seg = DF[DF$cod_ent == args] 
})

output$slider <- renderUI({
    sliderInput("inslider","Slider", 
                min = min(tabla_seg()$fec_cierre), 
                max   = max(tabla_seg()$fec_cierre),
                value = c(min(tabla_seg()$fec_cierre),     
                          max(tabla_seg()$fec_cierre))
)})


tabla_fec <- reactive({

    tabla_fec = tabla_seg()[tabla_seg()$fec_cierre >=  input$inslider[1] &
                         tabla_seg()$fec_cierre <= input$inslider[2],]

    tabla_fec[order(tabla_fec$fec_cierre,
                              decreasing = TRUE),]


})

output$deuda_dir <- renderPlotly({
  ban_sit(tabla_fec())
})

output$deuda_mora <- renderPlotly({
  ban_mora(tabla_fec())
})

output$tabla <- DT::renderDataTable({
    tab = tabla_fec()
    row.names(tab) = NULL
    tab$TotalCreditosDirectos <- formatC(tab$TotalCreditosDirectos,
                                         format="d",
                                         big.mark=',')

    tab$Deuda_Mora_porc <- round(tab$Deuda_Mora_porc, 2)                                             

    tab <-  tab[,c("fec_cierre",
                   "TotalCreditosDirectos",
                   "Deuda_Mora_porc")]
    names(tab) <- c("Fecha de cierre",
                    "Deuda Directa (S/.)", 
                    "Mora (%)")            
    DT::datatable(tab)
})
  }

【问题讨论】:

  • 你有机会提供数据吗?
  • 这里可以下载SF.rds的样本:1drv.ms/u/s!Aiohja7mVQ6xkB14sqWkrR2dvrw6
  • 我现在注意到的是 tabla_seg = DF[DF$cod_ent == args] 没有选择任何列,这可能应该是 tabla_seg = DF[DF$cod_ent == args, ]。让我知道这是否能改善情况...
  • 问题就出在这里!非常感谢您的帮助!
  • 虽然它现在可以工作,但在显示对象之前会出现三个警告消息。它们是:图形的“顺序错误:参数 1 不是向量”,表格的“$

标签: r shiny shiny-server


【解决方案1】:

我已经检查了代码。下面你可以看到修改后的代码。我认为问题在于 input$inslider 在调用 tabla_fec

这是修改后的服务器。R

    library(shiny)
    library(plotly)
    library(ggplot2)
    library(scales)



    ban_sit <- function(df){
            # Seleccionas y luego : Ctrl+R
            p <- ggplot(data = df, 
                        aes(x = fec_cierre,
                            y = TotalCreditosDirectos/1000)) +
                    geom_line(colour = "midnightblue")+
                    scale_y_continuous(labels = comma)+
                    xlab("Fecha de Cierre")+
                    ylab("Créditos Directos (En MM de Soles)")
            gg <- ggplotly(p)
            gg
    }
    ban_mora <- function(df){
            p <- ggplot(data = df,
                        aes(x = fec_cierre,
                            y = Deuda_Mora_porc)) +
                    geom_line(colour = "firebrick4")+
                    scale_y_continuous(labels = comma)+
                    xlab("Fecha de Cierre")+
                    ylab("Ratio de Mora (%)")
            gg <- ggplotly(p)
            gg
    }


    shinyServer(
            function(input, output) {
                    tabla_seg <- reactive({
                            args <- switch(input$var,
                                           'B. AZTECA'='B001', # Solo entidades activas
                                           'B. CENCOSUD PERU'='B002',
                                           'B. CONTINENTAL'='B003',
                                           'B. DE COMERCIO'='B004',
                                           'B. DE CREDITO DEL PERU'='B005',
                                           'B. FALABELLA PERU'='B007',
                                           'B. FINANCIERO'='B008',
                                           'B. GNB'='B009',
                                           'B. ICBC'='B010',
                                           'B. INTERAMERICANO DE FINANZAS'='B011',
                                           'B. RIPLEY'='B012',
                                           'B. SANTANDER PERU'='B014',
                                           'CITIBANK'='B020',
                                           'INTERBANK'='B023',
                                           'MIBANCO'='B024',
                                           'SCOTIABANK PERU'='B025')

                            tabla_seg = DF[DF$cod_ent == args, , drop = FALSE] 
                    })

                    output$slider <- renderUI({
                            sliderInput("inslider","Slider", 
                                        min = min(tabla_seg()$fec_cierre), 
                                        max   = max(tabla_seg()$fec_cierre),
                                        value = c(min(tabla_seg()$fec_cierre),     
                                                  max(tabla_seg()$fec_cierre))
                            )})


                    tabla_fec <- reactive({

                            if (!is.null(input$inslider[1]) || !is.null(input$inslider[2])) {
                            tabla_fec = tabla_seg()[tabla_seg()$fec_cierre >=  input$inslider[1] &
                                                            tabla_seg()$fec_cierre <= input$inslider[2], ]
                            } else {
                                    tabla_fec <-  tabla_seg()        
                            }

                            tabla_fec[order(tabla_fec$fec_cierre,
                                            decreasing = TRUE), ]


                    })

                    output$deuda_dir <- renderPlotly({
                            ban_sit(tabla_fec())
                    })

                    output$deuda_mora <- renderPlotly({
                            ban_mora(tabla_fec())
                    })

                    output$tabla <- DT::renderDataTable({
                            tab = tabla_fec()
                            row.names(tab) = NULL
                            tab$TotalCreditosDirectos <- formatC(tab$TotalCreditosDirectos,
                                                                 format="d",
                                                                 big.mark=',')

                            tab$Deuda_Mora_porc <- round(tab$Deuda_Mora_porc, 2)

                            tab <-  tab[,c("fec_cierre",
                                           "TotalCreditosDirectos",
                                           "Deuda_Mora_porc")]
                            names(tab) <- c("Fecha de cierre",
                                            "Deuda Directa (S/.)",
                                            "Mora (%)")
                            DT::datatable(tab)
                    })
                    output$letsee <- renderText({
                            class(tabla_fec())
                    })
            })

如果这有帮助,请告诉我。

【讨论】:

    猜你喜欢
    • 2015-04-10
    • 2017-04-13
    • 1970-01-01
    • 2021-06-02
    • 2015-08-07
    • 2022-08-19
    • 2018-08-25
    • 1970-01-01
    • 2014-04-14
    相关资源
    最近更新 更多