我想在基于“rhandsontable”的表更新时创建验证方案。 我有两列:最小值和最大值,我希望当Max列中的单元格更新时,只允许更新值大于前一列Min的值,反之亦然。 也许有可能使用hot_cols渲染器并通过大于零的减法来验证,等等,但我一般不熟悉“rhandsontable”或“hansontable”,虽然我认为这是可能的,但我不知道如何。 谢谢。
玩具例子:
library(shiny) library(rhandsontable) if (interactive()) { ui <- fluidPage( rHandsontableOutput('table'), tableOutput('table1') ) server <- function(input, output,session) { values <- reactiveValues(df=data.frame(Parameter=c('A','B','C'),Min=c(10,20,30),Max=c(20,30,40))) observe({ if(!is.null(input$table)){ values$df <- hot_to_r(input$table) output$table1<-renderTable(values$df) } }) output$table<-renderRHandsontable({ rhandsontable(values$df)%>% hot_col("Parameter", readOnly = TRUE)%>% hot_validate_numeric(col='Min', min = 1, max = 50,allowInvalid = FALSE)%>% hot_validate_numeric(col='Max', min = 1, max = 50,allowInvalid = FALSE) }) } shinyApp(ui, server) }I'd like to create a validation scheme when a table based on "rhandsontable" is updated. I have two columns: Min and Max and I'd like that when a cell in the column Max is updated, only it's allowed updating with a value greater than the previous column Min in the same row or vice-versa. Maybe it's possible using hot_cols renderer and validating by means of subtraction greater than zero, etc. but I'm not familiar with "rhandsontable" or "hansontable" in general although I think this is possible, but I don't know how. Thanks.
Toy example:
library(shiny) library(rhandsontable) if (interactive()) { ui <- fluidPage( rHandsontableOutput('table'), tableOutput('table1') ) server <- function(input, output,session) { values <- reactiveValues(df=data.frame(Parameter=c('A','B','C'),Min=c(10,20,30),Max=c(20,30,40))) observe({ if(!is.null(input$table)){ values$df <- hot_to_r(input$table) output$table1<-renderTable(values$df) } }) output$table<-renderRHandsontable({ rhandsontable(values$df)%>% hot_col("Parameter", readOnly = TRUE)%>% hot_validate_numeric(col='Min', min = 1, max = 50,allowInvalid = FALSE)%>% hot_validate_numeric(col='Max', min = 1, max = 50,allowInvalid = FALSE) }) } shinyApp(ui, server) }最满意答案
这是我在评论中提到的。 您可能会发现它很复杂,但这是server side质量控制与rhandsontable 。
在这个阶段,你可以自由地想出你需要的最复杂的功能。
作为一种更复杂(但可能更高效)的方法,您可以拥有不同的合并语句,可以像SQL连接一样工作,也可以使用dplyr或data.table非data.table或主/次连接。
library(shiny) library(rhandsontable) ui <- fluidPage(column(6, rHandsontableOutput('table')), column(6, tableOutput('table1')) ) server <- function(input, output,session) { df <- eventReactive( input$table, { if (is.null(input$table)) { dfOld <<- df <- data.frame(Parameter=c('A','B','C'),Min=c(10,20,30),Max=c(20,30,40)) } else { df <- hot_to_r(input$table) # Quality control # Rule 1: if( any(df$Max > 9) & any(df$Min < 3) ) { df$Max <- dfOld$Max df$Min <- dfOld$Min } } dfOld <<- df df }, ignoreNULL = F) output$table<-renderRHandsontable({ if (is.null(df())) return() rhandsontable(df())%>% hot_col("Parameter", readOnly = TRUE)%>% hot_validate_numeric(col='Min', min = 1, max = 50,allowInvalid = FALSE)%>% hot_validate_numeric(col='Max', min = 1, max = 50,allowInvalid = FALSE) }) } shinyApp(ui, server)请让我知道如果有用。
This is what I was referring to in my comment. You may find it complex, but this is what server side quality control would look like with rhandsontable.
At this stage you are at liberty to come up with the most complex functions you need.
As a more complex (but possibly more efficient) way to do this you could have different merge statement that can work like SQL join, or use dplyr or data.table non-equi or primary / secondary joins.
library(shiny) library(rhandsontable) ui <- fluidPage(column(6, rHandsontableOutput('table')), column(6, tableOutput('table1')) ) server <- function(input, output,session) { df <- eventReactive( input$table, { if (is.null(input$table)) { dfOld <<- df <- data.frame(Parameter=c('A','B','C'),Min=c(10,20,30),Max=c(20,30,40)) } else { df <- hot_to_r(input$table) # Quality control # Rule 1: if( any(df$Max > 9) & any(df$Min < 3) ) { df$Max <- dfOld$Max df$Min <- dfOld$Min } } dfOld <<- df df }, ignoreNULL = F) output$table<-renderRHandsontable({ if (is.null(df())) return() rhandsontable(df())%>% hot_col("Parameter", readOnly = TRUE)%>% hot_validate_numeric(col='Min', min = 1, max = 50,allowInvalid = FALSE)%>% hot_validate_numeric(col='Max', min = 1, max = 50,allowInvalid = FALSE) }) } shinyApp(ui, server)Please let me know if useful.
更多推荐
发布评论