1

以下示例应用程序以用户必须填写的空表开始。我想在他可以为 Nation 和 City 列选择的值中添加一些依赖项,因此,例如,如果他在单击 City 列时选择 Nation=Italy,则只会出现 Milan 和 Rome;反之亦然,如果他首先选择米兰,则只有意大利应该出现在自动完成列表中。建议?谢谢

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

1 回答 1

1

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

 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)
于 2017-06-23T09:42:04.287 回答