2017-03-08 5 views
1

Rhansontableパッケージを使用しています。テーブルを作成して、あるカラムの値を変更して別のカラムを自動的に計算するテーブルを作成します。例えば:私はコラム「NUM」R Shiny App:RhandsontableのReactive/Calculateカラム

の値を変更したときに、「トータル」(num個の*価格)別の列を自動的に計算する必要があります上記のデータフレームで

DF = data.frame(num = 1:10, price = 1:10,stringsAsFactors = FALSE)

はサンプル光沢のあるコードを助けてくださいことはできますか?

答えて

1

私はこれは何をしたいと思う

#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