【问题标题】:Reactive/Calculate column in dynamic Rhandsontable动态 Rhandsontable 中的反应/计算列
【发布时间】:2021-10-04 16:01:40
【问题描述】:

我正在使用 rhandsonpackage 并使用下面链接中的解决方案来实现以下场景 - “下拉列表的更改应该为用户提供一组不同的输入,他们可以进一步修改,而其他一些列继续重新计算” R Shiny App: Reactive/Calculate column in Rhandsontable

当初始 DF(初始化为先前的

核心原因是在“MyChanges”定义中,即使更新了这个动态 DF,对象也会继续使用旧的 input$hotable1(因为不再满足 is.null(input$hotable1) 条件)。因此,虽然动态 DF 在“以前”中正确更新,但它不会反映在“MyChanges”中。我尝试设置一个标志以捕获下拉列表更改并将 input$hottable1 设置为 NULL 但它是一个只读对象并且该操作出错。

这里是修改后的代码 sn-p 以重现该问题。同样,主要问题是在第 26 行,它忽略了更新的“previous()”对象。非常感谢任何有关解决此问题的帮助!

#rm(list = ls())
library(shiny)
library(rhandsontable)
library(shinyWidgets)

## Create the dataset
getdynamicDF <- function(selection){
  if(selection=="a"){return(data.frame(num = 1:10, price = 1:10,Total = 1:10,stringsAsFactors = FALSE))}
  else if (selection=="b"){return(data.frame(num = 11:20, price = 1:10,Total = 1:10,stringsAsFactors = FALSE))}
  else if (selection=="c"){return(data.frame(num = 21:30, price = 1:10,Total = 1:10,stringsAsFactors = FALSE))}
}
# DF = data.frame(num = 1:10, price = 1:10,Total = 1:10,stringsAsFactors = FALSE)

numberofrows <- 10

server <- shinyServer(function(input, output, session) {
  
  # Initiate your table
  # dynamicDF <- function(option)
  previous <- reactive({
    getdynamicDF(input$mydropdown)
  })
  
  MyChanges <- reactive({
    if(is.null(input$hotable1)){return(previous())}
    else if(!identical(previous(),input$hotable1)){
      # hot.to.df function will convert your updated table into the dataframe
      mytable <- as.data.frame(hot_to_r(input$hotable1))
      # here the second column is a function of the first and it will be multipled by 100 given the values in the first column
      mytable <- mytable[1:numberofrows,]
      
      # Add some test cases
      mytable[,1][is.na(mytable[,1])] <- 1
      mytable[,2][is.na(mytable[,2])] <- 1
      mytable[,3] <- mytable[,1]*mytable[,2]
      mytable
    }
  })
  output$hotable1 <- renderRHandsontable({rhandsontable(MyChanges())})
})

ui <- basicPage(mainPanel(pickerInput(
  inputId = "mydropdown",
  label = "Option", 
  choices = c("a", "b", "c")
),
rHandsontableOutput("hotable1")))
shinyApp(ui, server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    也许这就是你要找的。​​p>

    library(shiny)
    library(rhandsontable)
    library(shinyWidgets)
    
    ## Create the dataset
    getdynamicDF <- function(selection){
      if(selection=="a"){return(data.frame(num = 1:10, price = 1:10,Total = 1:10,stringsAsFactors = FALSE))}
      else if (selection=="b"){return(data.frame(num = 11:20, price = 1:10,Total = 1:10,stringsAsFactors = FALSE))}
      else if (selection=="c"){return(data.frame(num = 21:30, price = 1:10,Total = 1:10,stringsAsFactors = FALSE))}
    }
    #numberofrows <- 10
    
    server <- shinyServer(function(input, output, session) {
      new <- reactiveValues(dt=NULL)
      previous <- eventReactive(input$mydropdown, {
        req(input$mydropdown)
        getdynamicDF(input$mydropdown)
      })
      
      observe({new$dt<-previous()})
      observeEvent(input$hotable1, {
        mytable <- as.data.frame(hot_to_r(input$hotable1))
        mytable[,3] <- mytable[,1]*mytable[,2]
        new$dt <- mytable
      })
      
      output$hotable1 <- renderRHandsontable({
        rhandsontable(new$dt)
      })
    })
    
    ui <- basicPage(mainPanel(pickerInput(
      inputId = "mydropdown",
      label = "Option", 
      choices = c("a", "b", "c")
    ),
    rHandsontableOutput("hotable1")))
    shinyApp(ui, server)
    

    【讨论】:

    • 感谢您的回答 - 它动态选择输入,但不执行“mytable[,3]
    • 请尝试更新后的代码。
    • 这完美!非常感谢!
    猜你喜欢
    • 2017-10-19
    • 1970-01-01
    • 2016-09-14
    • 2020-10-27
    • 1970-01-01
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    相关资源
    最近更新 更多