Rhandsontable 从逻辑中收集值 == TRUE

Rhandsontable collect values from logical == TRUE

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

我将使用向量对另一个 rhandsontable 中的列进行子集化 'Aims'

我遇到了错误

Warning: Error in match: 'match' requires vector arguments

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", "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",  "Industry", "Private sector", 
            "Academic institute or research facility", "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 表达式中,以便 selected 的结果可以在其他地方使用。

另外,selected2() 需要括号。 selected2() 应该 return Type selected.

的字符向量

到 select 第二个 data.frame Aims_DF_NEW 的相应列,您可以尝试:

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

这将仅包括 Aims_DF_NEW 中包含在 selected2() 结果中的列。添加了 drop = F,因此如果只有 1 列 selected(并且仍然是 data.frame),结果不会被强制转换为向量。

这是基于第一个 table 对第二个 table 进行子集化的修订版本(第二个 table 简化为演示)。

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", "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",  "Industry", "Private sector", 
            "Academic institute or research facility", "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[, names(Aims_DF_NEW) %in% selected2(), drop = F], rowHeaders = NULL, width = 1500, height = 600) 
  })
  
}

shinyApp(ui = ui, server = server)

整理@Ben 的答案。这是一个解决方案

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", "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",  "Industry", "Private sector", 
            "Academic institute or research facility", "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")
  
  Cat <- data.frame(Aims_DF_NEW[, 1])
  colnames(Cat) <- c("Category")

  output$Aims2 <- renderRHandsontable({   rhandsontable(cbind(Cat, Aims_DF_NEW[, selected2(), drop = F]), 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)