rhandsontable自动完成依赖列

时间:2017-06-21 14:40:54

标签: r shiny rhandsontable

以下示例应用程序以用户必须填写的空表开头。我想在他可以为国家和城市列选择的值中添加一些依赖,例如,如果他选择Nation =意大利,当他点击城市时只有米兰和罗马应该出现;反之,如果他第一次选择米兰只有意大利应该出现在自动完成列表中。 建议? 谢谢

library(rhandsontable)
library(shiny)

ui = fluidPage(rHandsontableOutput("data"))

server = function(input,output) {
df = data.frame(Nation = character(),City=character(),Num_Clients=integer())
values = reactiveValues(data = df)

observe({
req(input$data)
values$data = hot_to_r(input$data)
})

output$data = renderRHandsontable({
rhandsontable(values$data, height=500, minRows=1,stretchH = "all") %>%
hot_col(col="Nation",type="autocomplete",source=c("England","Spain","Mexico"    ,"Italy"),strict=TRUE,allowInvalid=FALSE) %>% 
hot_col(col="City",type="autocomplete",source=c("Rome","Guadalajara","Madrid    ","London","Milan"),strict=TRUE,allowInvalid=FALSE)  %>% 
hot_validate_numeric(col = "Num_Clients", min = 0)

})
}
shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

也许你可以沿着这条道路做点什么:

 library(rhandsontable)
   library(shiny)

   ui = fluidPage(rHandsontableOutput("data"))

   server = function(input,output) {
     df = data.frame(Nation = character(),City=character(),Num_Clients=integer())
     values = reactiveValues(data = df)

     observe({
       req(input$data)
       values$data = hot_to_r(input$data)
     })

     output$data = renderRHandsontable({
       rhandsontable(values$data, height=500, minRows=1,stretchH = "all") %>%
         hot_col(col="Nation",type="autocomplete",source=c("England","Spain","Mexico"    ,"Italy"),strict=TRUE,allowInvalid=FALSE) %>% 
         hot_col(col="City",type="autocomplete",source=c("Rome","Guadalajara","Madrid    ","London","Milan"),strict=TRUE,allowInvalid=FALSE)  %>% 
         hot_validate_numeric(col = "Num_Clients", min = 0)

     })


     observeEvent(input$data,{

       change <- unlist(input$data$changes$changes)
       if(!is.null(change)){
         #Ifchange in the 1st column(0th Column)
         if(as.numeric(change[2]) == 0 ){#If change in country filter the city
           if(change[4] == "Italy"){#If itality is selected
             output$data = renderRHandsontable({
               rhandsontable(values$data, height=500, minRows=1,stretchH = "all") %>%
                 hot_col(col="Nation",type="autocomplete",source=c("England","Spain","Mexico","Italy"),strict=TRUE,allowInvalid=FALSE) %>% 
                 hot_col(col="City",type="autocomplete",source=c("Rome","Milan"),strict=TRUE,allowInvalid=FALSE)  %>% 
                 hot_validate_numeric(col = "Num_Clients", min = 0)

             })
           }
         }
       }

     })
   }
   shinyApp(ui = ui, server = server)