是否可以让rhandsontable中的一行使用下拉菜单供用户选择,而其余行需要手动输入数据?

mzsu5hc0  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(164)

在下面的代码中,我尝试将post Is there a way to have different dropdown options for different rows in an rhandsontable?的答案调整为适合我的情况。我希望呈现表的前两行要求用户手动输入数值(在由此导出的更完整的代码中,用户能够手动输入数值来代替所呈现的默认值),和最后一行(行名称"select_option")需要在下拉列表中进行选择。目前,下面的代码只为列中的所有行提供下拉列表。
是否可能?(前2行手动输入数值,最后一行下拉)。
从我对rhandsontable的包文档的回顾来看,函数hot_row()hot_col()的限制要多得多。另外,一个额外的转折是,这个更大的应用程序允许通过actionButton()添加列,我需要添加的列也具有相同的下拉列表,用于"selection_option"行。
代码:

library(shiny)
library(rhandsontable)

ui <- fluidPage(hr(),mainPanel(rHandsontableOutput("ExampleTable")))

server <- function(input, output) {
  
  DF <- reactiveVal(data.frame(Object = c("enter_data", "enter_data", "select_option"), Needs = NA_character_, stringsAsFactors = FALSE))
  
  observeEvent(input$ExampleTable, {
    DF(hot_to_r(input$ExampleTable))
  })
  
  output$ExampleTable <- renderRHandsontable({
    
    select_optionOptions <- c(NA_character_, "dog", "cat") # defines the dropdown options
    
    tmpExampleTable <- rhandsontable(DF(), rowHeaders = NULL, stretchH = "all", selectCallback = TRUE, width = 300, height = 300) %>%
      hot_col("Object", readOnly = TRUE) %>%
      hot_col("Needs", allowInvalid = FALSE, type = "dropdown", source = NA_character_, readOnly = TRUE)
    
    if(!is.null(input$ExampleTable_select$select$r)){
      
      selectedObject <- DF()[input$ExampleTable_select$select$r, "Object"]
      
      if(selectedObject == "select_option"){
        tmpExampleTable <- hot_col(tmpExampleTable, col = "Needs", allowInvalid = FALSE, type = "dropdown", source = select_optionOptions) %>% hot_cell(row = input$ExampleTable_select$select$r, col = "Needs", readOnly = FALSE)
      }
    }
    tmpExampleTable
    
  })
}

shinyApp(ui = ui, server = server)
rdrgkggo

rdrgkggo1#

从我的研究来看,在rhandsontable中,至少通过使用hotcols()hotrow()(后者的选项特别有限),让一行具有下拉功能,而同一个表中的其他行具有手动覆盖值,似乎是不太可能的。
如有异议,请告知。
在任何情况下,一个合理的替代方案是将单个表拆分为两个并行运行的表,其中一个表显示可以被用户覆盖的值,另一个表显示指定项的下拉列表。

library(shiny)
library(rhandsontable)

ui <- fluidPage(hr(),
        mainPanel(
          rHandsontableOutput("Tbl1"),
          br(),
          rHandsontableOutput("Tbl2")
        )
      )

server <- function(input, output) {
  DF1 <- reactiveVal(data.frame(Object = c("enter_data", "enter_data"), Needs = c(10,20))) 
  DF2 <- reactiveVal(data.frame(Object = c("select_option"), Needs = NA_character_, stringsAsFactors = FALSE))
  
  observeEvent(input$Tbl1,{DF1(hot_to_r(input$Tbl1))})
  observeEvent(input$Tbl2,{DF2(hot_to_r(input$Tbl2))})
  
  output$Tbl1 <- renderRHandsontable({
    tmp1 <- rhandsontable(DF1(),rowHeaders=NULL,useTypes=TRUE)%>%
      hot_context_menu(allowRowEdit = FALSE, allowColEdit = FALSE) %>%
      hot_cols(colWidths = 100)
    tmp1
  })
  
  output$Tbl2 <- renderRHandsontable({
    select_option <- c(NA_character_, "dog", "cat") # defines the dropdown options
    
    tmp2 <- rhandsontable(DF2(), rowHeaders = NULL, selectCallback = TRUE, height = 300) %>%
      hot_cols(colWidths = 100) %>%
      hot_col("Object", readOnly = TRUE) %>%
      hot_col("Needs", allowInvalid = FALSE, type = "dropdown", source = NA_character_, readOnly = TRUE)
    
    if(!is.null(input$Tbl2_select$select$r)){
      selectedObject <- DF2()[input$Tbl2_select$select$r, "Object"]
      if(selectedObject == "select_option"){
        tmp2 <- hot_col(tmp2, col = "Needs", allowInvalid = FALSE, type = "dropdown", source = select_option) %>% 
          hot_cell(row = input$Tbl2_select$select$r, col = "Needs", readOnly = FALSE)
      }
    }
    tmp2
  })
}

shinyApp(ui = ui, server = server)

相关问题