Rhandsontable 从逻辑 == TRUE 收集值

Posted

技术标签:

【中文标题】Rhandsontable 从逻辑 == TRUE 收集值【英文标题】:Rhandsontable collect values from logical == TRUE 【发布时间】:2021-10-05 05:43:10 【问题描述】:

我正在尝试根据相邻逻辑列 ('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", "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)

【问题讨论】:

【参考方案1】:

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

注意output$selected中的代码可以移动到单独的reactive表达式中,这样选中的结果就可以在别处使用。

另外,selected2() 需要括号。 selected2() 应返回所选 Type 的字符向量。

要从您的第二个 data.frame 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", "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)

【讨论】:

这很好用,虽然我很难以reactive 表达式的形式访问这些值。我添加了第二个术语selected2 &lt;- reactive( dat &lt;- hot_to_r(input$Intiated) if (any(dat[[1]])) dat[which(dat[[1]]), 2] ),当我尝试对第二个 rhandsontable 进行子集化时,它返回错误“不支持的对象类型:字符无法提取列类型” @ThomasWorthington 请查看已编辑的示例,如果这有帮助,请告诉我。确保以 selected2() 的形式访问字符向量并带有括号。如果这不起作用,请随时编辑问题,详细说明您如何使用 selected2 和第二个 rhandsontable。 @'Ben' 我已经编辑了问题以澄清我的目标 selected2 是一个reactive 表达式,因此要使用它,您需要括号:selected2()。第二个rhandsontable 的某些部分对我来说有点令人困惑,但我能够以简化的形式使其工作:output$Aims2 &lt;- renderRHandsontable( rhandsontable(Aims_DF_NEW[, selected2(), drop = F], rowHeaders = NULL, width = 1500, height = 600) )...在这里您可以使用selected2()Aims_DF_NEW data.frame 进行子集化。 ..drop = F 已添加,因此如果您选择一列,它不会被强制转换为向量(保持数据框)... 您也可以尝试Aims_DF_NEW[, names(Aims_DF_NEW) %in% selected2(), drop = F] 以确保可以从Aims_DF_NEW 获得这些列【参考方案2】:

汇总@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)

【讨论】:

以上是关于Rhandsontable 从逻辑 == TRUE 收集值的主要内容,如果未能解决你的问题,请参考以下文章

获取 Rhandsontable 的选定行

基于 rhandsontable 中单元格值的颜色行

从 sliderInput 更新 rhandsontable

在 Shiny 中将数据从 rhandsontable 对象转换为数据框

r shiny:从另一个 rhandsontable 更新 rhandsontable

闪亮:覆盖rhandsontable,赋值左侧无效(NULL)