在闪亮中编辑过滤的反应数据框,保存用户输入以在应用程序中使用

问题描述

我正在处理一个可编辑的表格,其中包含针对列和行的反应式过滤器。我实现了 YBS 提供的代码,它看起来完全符合我的要求并正确显示。 我的问题是,我无法访问服务器端的更新数据框。我想要一个数据框的原因是我有更改单元格的信息。每个单元格值(和引用)将作为表格的输入。 我尝试在 my_matrix_cell_edit 和 rbind 上使用 observeEvent 创建不同的数据帧,但没有结果

下面是 reprex,当你运行它时,在控制台端我得到“NULL”。 这是由不断重置它的 df

感谢大家的帮助!

country_matrix <- structure(list(industry_group = c("Financial Institutions Group (fig)","Financial Institutions Group (fig)","Financial Institutions Group (fig)"),sector = c("Housing Finance","climate Finance","Trade Finance","Housing Finance","Housing Finance"),component = c("Stakeholder","Stakeholder","Environment & Social","Economywide"),subcomponent = c("Access","Credit: Mitigation","Environment",Afghanistan = c(0,0),Angola = c(0,Albania = c(0,Argentina = c(0,Armenia = c(0,`American Samoa` = c(0,Azerbaijan = c(0,Burundi = c(0,Benin = c(0,`Burkina Faso` = c(0,Bangladesh = c(0,Bulgaria = c(0,`Bosnia and Herzegovina` = c(0,Belarus = c(0,Belize = c(0,Bolivia = c(0,Brazil = c(0,Barbados = c(0,Bhutan = c(0,Botswana = c(0,`Central African Republic` = c(0,0) 
),row.names = c(NA,6L),class = "data.frame")

### 

countrycmx <- names(country_matrix)[5:25]

ui <- fluidPage(
  

  fluidRow(
    column(12,column(
             2,pickerInput(
               "sectormx",'Sector',selected = "climate Finance",choices = unique(country_matrix$sector),#selected = c('SME Finance','Insurance','climate Finance'),multiple = TRUE)
           ),pickerInput(
               'componentmx','Component',choices = unique(country_matrix$component),selected = 'Stakeholder',multiple = TRUE,options = pickerOptions(
                 actionsBox = TRUE,liveSearch = TRUE,liveSearchPlaceholder = 'Search...',liveSearchnormalize = TRUE,liveSearchStyle = 'contains'
               ),width = "100%"
             )
           ),pickerInput(
               'subcomponentmx','Subcomponent',choices = unique(country_matrix$subcomponent),selected = 'Quality',width = "100%"
             )),pickerInput(
               'countrymx','Country',choices = countrycmx,selected = 'Albania',width = "100%"
             ))

           #actionButton("update","Update Data set",class = "btn-primary",style='padding:4px; font-size:120%')
    ),# Show a plot of the generated distribution
    column(12,DTOutput("my_matrix")
           
    )
  )
)


server <- function(input,output,session) {
  
  matrix <- reactive({
    req(input$sectormx,input$componentmx,input$subcomponentmx)
    
    country_matrix %>%
      dplyr::filter(
        sector %in% input$sectormx#,) %>%
      select(sector,component,subcomponent,input$countrymx)
  })
  
  df <- reactiveValues(data=NULL)
                      
  observe({df$data <- matrix()})
  
  output$my_matrix  <- renderDT(df$data,editable = TRUE,rownames = FALSE)
  
  
  #observeEvent(input$my_matrix_cell_edit,{
    
    observeEvent(input$my_matrix_cell_edit,{
      info = input$my_matrix1_cell_edit
      str(info)
      i = info$row
      j = info$col + 1
      v = info$value
      df$data[i,j] <<- editData(info,df$data[i,j])
  })
  
}

shinyApp(ui = ui,server = server)

解决方法

试试这个

country_matrix <- structure(list(industry_group = c("Financial Institutions Group (FIG)","Financial Institutions Group (FIG)","Financial Institutions Group (FIG)"),sector = c("Housing Finance","Climate Finance","Trade Finance","Housing Finance","Housing Finance"),component = c("Stakeholder","Stakeholder","Environment & Social","Economywide"),subcomponent = c("Access","Credit: Mitigation","Environment",Afghanistan = c(1,1,1),Angola = c(1,Albania = c(1,Argentina = c(1,Armenia = c(1,`American Samoa` = c(1,Azerbaijan = c(1,Burundi = c(1,Benin = c(1,`Burkina Faso` = c(1,Bangladesh = c(1,Bulgaria = c(1,`Bosnia and Herzegovina` = c(1,Belarus = c(1,Belize = c(1,Bolivia = c(1,Brazil = c(1,Barbados = c(1,Bhutan = c(1,Botswana = c(1,`Central African Republic` = c(1,1) 
),row.names = c(NA,6L),class = "data.frame")

### 

countrycmx <- names(country_matrix)[5:25]
  
ui <- fluidPage(
  
  # Application title
  #titlePanel("Old Faithful Geyser Data"),# Sidebar with a slider input for number of bins 
  fluidRow(
    column(12,column(
             2,pickerInput(
               "sectormx",'Sector',selected = "Climate Finance",choices = unique(country_matrix$sector),#selected = c('SME Finance','Insurance','Climate Finance'),multiple = TRUE)
           ),pickerInput(
               'componentmx','Component',choices = unique(country_matrix$component),selected = 'Stakeholder',multiple = TRUE,options = pickerOptions(
                 actionsBox = TRUE,liveSearch = TRUE,liveSearchPlaceholder = 'Search...',liveSearchNormalize = TRUE,liveSearchStyle = 'contains'
               ),width = "100%"
             )
           ),pickerInput(
               'subcomponentmx','Subcomponent',choices = unique(country_matrix$subcomponent),selected = 'Quality',width = "100%"
             )),pickerInput(
               'countrymx','Country',choices = countrycmx,selected = 'Albania',width = "100%"
             ))
           
           #selectInput("select","Select country",c('col1','col2'),multiple = TRUE),#actionButton("update","Update Data set",class = "btn-primary",style='padding:4px; font-size:120%')
    ),# Show a plot of the generated distribution
    column(12,DTOutput("my_matrix")
           #DTOutput("table")
    )
  )
)


server <- function(input,output,session) {
  
  matrix <- reactive({
    req(input$sectormx,input$componentmx,input$subcomponentmx)
    
    country_matrix %>%
      dplyr::filter(
        sector %in% input$sectormx#,#component %in% input$componentmx,#subcomponent %in% input$subcomponentmx
      ) %>%
      select(sector,component,subcomponent,input$countrymx)
  })
  
  df <- reactiveValues(data=NULL)
  observe({df$data <- matrix()})
  
  output$my_matrix  <- renderDT(df$data,editable = TRUE,rownames = FALSE)
  
  
  observeEvent(input$my_matrix_cell_edit,{
    info = input$my_matrix1_cell_edit
    str(info)
    i = info$row
    j = info$col + 1
    v = info$value
    df$data[i,j] <<- DT:::coerceValue(v,df$data[i,j])
    
    #replaceData(proxy,x,resetPaging = FALSE,rownames = FALSE)
  })
  
}

shinyApp(ui = ui,server = server)
,

感谢 YBS 和一些额外的帮助,我得到了一个完整的解决方案:

library(data.table)
library(DT)
library(dplyr)
library(tidyr)


country_matrix <- structure(list(industry_group = c("Financial Institutions Group (FIG)",Afghanistan = c(0,0),Angola = c(0,Albania = c(0,Argentina = c(0,Armenia = c(0,`American Samoa` = c(0,Azerbaijan = c(0,Burundi = c(0,Benin = c(0,`Burkina Faso` = c(0,Bangladesh = c(0,Bulgaria = c(0,`Bosnia and Herzegovina` = c(0,Belarus = c(0,Belize = c(0,Bolivia = c(0,Brazil = c(0,Barbados = c(0,Bhutan = c(0,Botswana = c(0,`Central African Republic` = c(0,0) 
),class = "data.frame")

### 

countrycmx <- names(country_matrix)[5:25]

ui <- fluidPage(
  

  fluidRow(
    column(12,width = "100%"
             ))

           #actionButton("update",DTOutput("my_matrix")
           
    )
  )
)


server <- function(input,input$countrymx)
  })
  
  df <- reactiveVal(NULL)
  
  observe({
    df(matrix())
    })
  
  output$my_matrix  <- renderDT(df(),{
    #browser()
    info = input$my_matrix_cell_edit
    print(info)
    t <- df()
    t[info$row,info$col+1] <- as.numeric(info$value)
    df(t)
    
  })
  
}

shinyApp(ui = ui,server = server)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...