gpt4 book ai didi

Rhandsontable 从逻辑 == TRUE 收集值

转载 作者:行者123 更新时间:2023-12-04 07:19:47 25 4
gpt4 key购买 nike

我正在尝试根据相邻逻辑列 ('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 bool 值)。如果有,您可以使用基于第一列的行索引 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", "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)

关于Rhandsontable 从逻辑 == TRUE 收集值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68578075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com