【发布时间】: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)
【问题讨论】: