0

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

我需要通过使用下拉菜单选择区域列中的区域来替换管理器列中的 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

卢普数据

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

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

请参考我的代码片段,我在下拉列表中获得的输出似乎没有显示到管理器功能所需的链接:

    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)
4

1 回答 1

0

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

  observe({
    df <- values$data
    df$manager <- ifelse(df$region == "East", "Mxolisi",
                         ifelse(df$region == "West", "Tom", "Jerry"))
    values$data <- df
  })
于 2021-08-23T10:28:12.033 回答