Rhandsontable 从逻辑 == TRUE 收集值

问题描述

我正在尝试根据相邻逻辑列 ('Tick') 的选择来收集 rhandsontable 中的列 ('Type') 的值。我想根据勾选的行创建所有类型的向量。

我将使用向量对其他 rhandsontable 'Aims' 中的列进行子集化

我收到错误

警告:匹配错误:“匹配”需要向量参数

library(rhandsontable)
library(shiny)

orgs <- c("Community leaders/representatives","Members of local community/indigenous committees","Landowners/customary area owners","National government","Sub-national or local government","Managed area manager/personnel","International NGO","Local or national NGO","Community based organizations - women’s groups","Community based organizations - men’s groups","Community based organizations - youth/school groups","Community based organizations - religIoUs groups","Community based organizations - conservation groups","Industry","Private sector","Academic institute or research facility","Other")

proj_aim3 <- data.frame(Category = c("Area","Condition","diversity"))
proj_aim3 <- cbind(proj_aim3,setNames( lapply(orgs,function(x) x=NA),orgs) )

ui <- fluidPage(
  rHandsontableOutput('Intiated'),verbatimtextoutput('selected'),br(),rHandsontableOutput("Aims2")
)

server <- function(input,output,session) {
  
  cats <- c("Community leaders/representatives","Not recorded","Other")
  
  DF <- data.frame(Tick = rep(FALSE,length(cats)),Type = cats,Name = rep("",length(cats)))
  
  output$Intiated <- renderRHandsontable(
    rhandsontable(DF,selectCallback = TRUE,readOnly = FALSE)
  )
  
  selected2 <- reactive({     
    dat <- hot_to_r(input$Intiated)     
    if (any(dat[[1]])) {       
      dat[which(dat[[1]]),2]      
    }   
  })
  
  output$selected <- renderPrint({
    cat(paste(selected2(),collapse = "\n"))
  })
  
  
  Aims_DF_NEW <- proj_aim3
  imps2 <- c("Primary","Secondary","Tertiary")
  
  sel <- selected2
  
  output$Aims2 <- renderRHandsontable({
    
    Aims_DF_NEW <- Aims_DF_NEW[,which(names(Aims_DF_NEW) %in% sel)]

    rhandsontable(Aims_DF_NEW,rowHeaders = NULL,width = 1500,height = 600) %>%
      hot_col(col = "Category",readOnly = T) %>%
      hot_cols(cols = Aims_DF_NEW[,2:ncol(Aims_DF_NEW)],type = "autocomplete",source = imps2,strict = TRUE,colWidths = 200)})
  
}

shinyApp(ui = ui,server = server)

解决方法

您可以尝试以下方法。使用 hot_to_r 从 handsontable 中获取数据作为 R 对象。检查第一列是否有任何选中的项目(这将是 TRUE 布尔值)。如果有,您可以使用基于 TRUE 的第一列的行索引提取第二列数据。

请注意,output$selected 中的代码可以移动到单独的 reactive 表达式中,以便选择的结果可以在其他地方使用。

此外,您还需要为 selected2() 加上括号。 selected2() 应返回所选 Type 的字符向量。

要从第二个数据框 Aims_DF_NEW 中选择适当的列,您可以尝试:

Aims_DF_NEW[,names(Aims_DF_NEW) %in% selected2(),drop = F]

这将仅包括 Aims_DF_NEW 中包含在 selected2() 结果中的列。添加 drop = F 以便在仅选择 1 列(并且仍然是 data.frame)时不会将结果强制转换为向量。

这是根据第一个表(第二个表简化演示)对第二个表进行子集化的修订版本。

library(rhandsontable)
library(shiny)

orgs <- c("Community leaders/representatives","Members of local community/indigenous committees","Landowners/customary area owners","National government","Sub-national or local government","Managed area manager/personnel","International NGO","Local or national NGO","Community based organizations - women’s groups","Community based organizations - men’s groups","Community based organizations - youth/school groups","Community based organizations - religious groups","Community based organizations - conservation groups","Industry","Private sector","Academic institute or research facility","Other")

proj_aim3 <- data.frame(Category = c("Area","Condition","Diversity"))
proj_aim3 <- cbind(proj_aim3,setNames( lapply(orgs,function(x) x=NA),orgs))
Aims_DF_NEW <- proj_aim3
imps2 <- c("Primary","Secondary","Tertiary")

ui <- fluidPage(
  rHandsontableOutput('Intiated'),verbatimTextOutput('selected'),br(),rHandsontableOutput("Aims2")
)

server <- function(input,output,session) {
  
  cats <- c("Community leaders/representatives","Not recorded","Other")
  
  DF <- data.frame(Tick = rep(FALSE,length(cats)),Type = cats,Name = rep("",length(cats)))
  
  output$Intiated <- renderRHandsontable(
    rhandsontable(DF,selectCallback = TRUE,readOnly = FALSE)
  )
  
  selected2 <- reactive({     
    dat <- hot_to_r(input$Intiated)     
    if (any(dat[[1]])) {       
      dat[which(dat[[1]]),2]      
    }   
  })
  
  output$selected <- renderPrint({
    cat(paste(selected2(),collapse = "\n"))
  })
  
  output$Aims2 <- renderRHandsontable({
    rhandsontable(Aims_DF_NEW[,drop = F],rowHeaders = NULL,width = 1500,height = 600) 
  })
  
}

shinyApp(ui = ui,server = server)
,

将@Ben 的答案放在一起。这是一个解决方案

library(rhandsontable)
library(shiny)

orgs <- c("Community leaders/representatives",orgs) )

ui <- fluidPage(
  rHandsontableOutput('Intiated'),collapse = "\n"))
  })
  
  
  Aims_DF_NEW <- proj_aim3
  imps2 <- c("Primary","Tertiary")
  
  Cat <- data.frame(Aims_DF_NEW[,1])
  colnames(Cat) <- c("Category")

  output$Aims2 <- renderRHandsontable({   rhandsontable(cbind(Cat,Aims_DF_NEW[,selected2(),drop = F]),height = 600) %>%
      hot_col(col = "Category",readOnly = T) %>%
      hot_cols(cols = Aims_DF_NEW[,2:ncol(Aims_DF_NEW)],type = "autocomplete",source = imps2,strict = TRUE,colWidths = 200)})
  
}

shinyApp(ui = ui,server = server)