【问题标题】:How can I autopopulate values of a column based on another column in a Shiny rHandsontable?如何根据 r Shiny Handsontable 中的另一列自动填充列的值?
【发布时间】:2021-09-05 01:50:18
【问题描述】:

我正在努力通过类似 vlookup 的解决方案在 RShiny 中逐行填充闪亮的表 (rhandsontable)。

我需要通过使用下拉菜单选择 region 列中的区域来替换 manager 列中的 NA 值。

我有主集(mainData)和查找集(lupData),如下:

主数据

# A tibble: 15 x 5
   centre              date     sales region manager
   <chr>               <chr>    <dbl> <chr>  <chr>  
 1 east shopping mall  1/1/2021    50 NA     NA     
 2 west shopping mall  1/1/2021    20 NA     NA     
 3 west shopping mall  1/1/2021    10 NA     NA     
 4 east shopping mall  1/2/2021    15 NA     NA     
 5 east shopping mall  1/2/2021    40 NA     NA     
 6 west shopping mall  1/2/2021    20 NA     NA     
 7 west shopping mall  1/3/2021    15 NA     NA     
 8 east shopping mall  1/4/2021     5 NA     NA     
 9 east shopping mall  1/4/2021    60 NA     NA     
10 south shopping mall 1/5/2021    66 NA     NA     
11 south shopping mall 1/6/2021    44 NA     NA     
12 south shopping mall 1/7/2021     3 NA     NA     
13 west shopping mall  1/5/2021    75 NA     NA     
14 west shopping mall  1/6/2021    14 NA     NA     
15 east shopping mall  1/7/2021     8 NA     NA

lupData

# A tibble: 3 x 2
  region manager
  <chr>  <chr>  
1 East   Mxolisi
2 West   Tom    
3 South  Philani

我想在 region 列上添加一个下拉列表,在阅读了销售额来自哪个购物中心(即第一列 centre)之后。 然后我希望 manager 列在选择(使用下拉菜单)我认为/决定特定购物中心来自的 region 后自动填充。

请参考我的 sn-p 代码,我在下拉列表中获得的输出似乎没有显示到 manager 功能所需的链接:

    library(shiny)
    library(rhandsontable)
    
    ui <- fluidPage(
      p(),
      rHandsontableOutput("hot", width = "100%", height = "100%")
    )
    
    server = function(input, output, session){
      
      DF <- data.frame("centre" = as.factor(c("east shopping mall",  "west shopping mall",  "west shopping mall",  "east shopping mall",  "east shopping mall", 
                                             "west shopping mall",  "west shopping mall",  "east shopping mall",  "east shopping mall",  "south shopping mall",
                                             "south shopping mall", "south shopping mall", "west shopping mall",  "west shopping mall",  "east shopping mall")),
                       "date" = as.Date(c("1/1/2021", "1/1/2021", "1/1/2021", "1/2/2021", "1/2/2021", "1/2/2021", "1/3/2021", "1/4/2021", "1/4/2021", "1/3/2021",
                                          "1/9/2021", "1/11/2021", "1/5/2021", "1/6/2021", "1/7/2021")),
                       "sales" = as.integer(c(50, 20, 10, 15, 40, 20, 15,  5, 60, 66, 44,  3, 75, 14,  8)),
    
    #my dropdown column region (must also populate manager) 
                       "region"= rep(as.factor(c("East",  "West",  "South")), length = length(c(50, 20, 10, 15, 40, 20, 15,  5, 60, 66, 44,  3, 75, 14,  8)))#i.e. max rows on the main table
                       
    ,"manager"= rep(as.factor(c("Mxolisi",  "Tom",  "Jerry")), length = length(c(50, 20, 10, 15, 40, 20, 15,  5, 60, 66, 44,  3, 75, 14,  8)))
                       )
      
    values <- reactiveValues(data = DF)
      
      observe({
        if(!is.null(input$hot)){
          values$data <- as.data.frame(hot_to_r(input$hot))
        }
      })    
      
      output$hot <- renderRHandsontable({
        
        myTable <- rhandsontable(values$data, width = 1750, height = 5500, selectCallback = TRUE) %>%
          hot_table(highlightCol = TRUE, highlightRow = TRUE, stretchH = "all") %>% 
          hot_col(col = "region", type = "dropdown", source = NULL, readOnly = TRUE, allowInvalid = FALSE)
        
        if(!is.null(input$hot_select$select$r) ){
          myTable <- hot_col(myTable, col = "region", type = "dropdown", source = values$DF$region[input$hot_select$select$r], readOnly = T)%>% 
            hot_cell(input$hot_select$select$r, col = "region", readOnly = FALSE)
        }
        
    myTable
        
      })
      
    }
    
    shinyApp(ui, server)

【问题讨论】:

    标签: r shiny vlookup rhandsontable rshiny


    【解决方案1】:

    您需要根据“区域”列有条件地定义您的“经理”列,您可以通过使用以下代码观察和更新reactiveValues 数据框来实现(插入到values 对象的创建下方) :

      observe({
        df <- values$data
        df$manager <- ifelse(df$region == "East", "Mxolisi",
                             ifelse(df$region == "West", "Tom", "Jerry"))
        values$data <- df
      })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-17
      • 1970-01-01
      • 2021-03-16
      • 2019-07-13
      • 1970-01-01
      • 2020-11-26
      • 1970-01-01
      相关资源
      最近更新 更多