gpt4 book ai didi

shiny - R Shiny 应用 : Reactive/Calculate column in Rhandsontable

转载 作者:行者123 更新时间:2023-12-01 12:22:39 26 4
gpt4 key购买 nike

我正在使用 Rhansontable 包,我想创建一个表,如果我更改一列中的值,则另一列会自动计算。例如:
DF = data.frame(num = 1:10, price = 1:10,stringsAsFactors = FALSE)
在上面的数据框中,当我更改“num”列中的值时,另一列“Total”(num*price)应该自动计算

可以请帮助示例 Shiny 代码吗?

最佳答案

我想这就是你想要的

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

## Create the dataset
DF = data.frame(num = 1:10, price = 1:10,Total = 1:10,stringsAsFactors = FALSE)
numberofrows <- nrow(DF)

server <- shinyServer(function(input, output, session) {

# Initiate your table
previous <- reactive({DF})

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(rHandsontableOutput("hotable1")))
shinyApp(ui, server)

enter image description here

关于shiny - R Shiny 应用 : Reactive/Calculate column in Rhandsontable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42665720/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com