在闪亮的rstudio

时间:2017-05-19 15:46:33

标签: shiny rstudio rhandsontable

我正在尝试将自定义验证添加到以下交互式表中。我想要做的是如下。该表包含三个变量,即mpg,cyl和disp。假设我编辑第一行的mpg值。然后,一旦我按下回车按钮,第一行的显示值应自动更改,计算为disp = mpg / cyl。这里mpg值是我编辑的新值。同样,如果我在特定行中编辑cyl,则应该更改该特定行的disp,自动计算为disp = mpg / cyl。此外,如果我改变特定行的disp值,那么该特定行的cyl应该自动更改为mpg / cyl。我试图找出一种方法来做到这一点。

library(shiny)
library(datasets)

ui=fluidPage(

rHandsontableOutput("table1")

)

server=function(input, output, session) {

mt=reactive({

datacopy= mtcars[, names(mtcars) %in% c("mpg" , "cyl" , "disp")]
datacopy=data.table(datacopy)
})  


output$table1=renderRHandsontable({

rhandsontable(mt())



})


}



shinyApp(ui,server)

1 个答案:

答案 0 :(得分:0)

我想这会达到你想要的效果:

library(shiny)
library(datasets)
library(rhandsontable)
library(data.table)

ui=fluidPage(

  rHandsontableOutput("table1")

)

server=function(input, output, session) {

  mt=reactive({

    datacopy <- NULL

    #For initial data upload
    if(is.null(input$table1)){
      datacopy= mtcars[, names(mtcars) %in% c("mpg" , "cyl" , "disp")]
      datacopy=data.table(datacopy) 

    }else{
      datacopy = hot_to_r(input$table1)

      #If there is change in data
      if(!is.null(input$table1$changes$changes)){

        row.no <- unlist(input$table1$changes$changes)[1]
        col.no <- unlist(input$table1$changes$changes)[2]
        new.val <- unlist(input$table1$changes$changes)[4]
        #If the changed value is mpg or cyl
        if(col.no == 0 || col.no == 1){
          datacopy[(row.no+1), 3] = datacopy[(row.no+1), 1]/datacopy[(row.no+1), 2]
        }else{
          datacopy[(row.no+1), 2] = datacopy[(row.no+1), 1]/datacopy[(row.no+1), 3]
        }

      }

    }

    datacopy

  })  


  output$table1=renderRHandsontable({

    rhandsontable(mt())

  })

}



shinyApp(ui,server)

希望它有所帮助!

<强> [编辑]: 根据您编辑的答案,我修改了以下代码。希望这会有所帮助。

 library(shiny)
    library(datasets)
    library(rhandsontable)
    library(data.table)



    c1= c(rep("0112",3), rep("0113",3),rep("0114",3))
    c2= c( rep(c("A", "B","C"),3))
    c3= c(rep(c("5312", "5421", "5510"),3)) 
    c4=c(rep(c("Area", "Yield", "Prod"),3))
    c5 = c( rep(2010,9))
    c6= c( 4,68,16169,1062,1.5,43225,800,1.25,100)
    data_=cbind(c1,c2,c3,c4,c5,c6)
    data_=as.data.frame(data_)
    names(data_) = c("CPCCode", "Item","ElementCode","El","Year","Value")
    data_$Value=as.numeric(levels( data_$Value))[ data_$Value]
    data_$Year= as.integer(levels( data_$Year))[data_$Year]
    data_$ElementCode = as.character(data_$ElementCode)
    rownames(data_) = NULL


    ui=fluidPage(

      rHandsontableOutput("table1")

    )

    server=function(input, output, session) {

      mt=reactive({

        datacopy <- NULL

        #For initial data upload
        if(is.null(input$table1)){ 

          datacopy <- data_

        }else{
          datacopy = hot_to_r(input$table1)

          #If there is change in data
          if(!is.null(input$table1$changes$changes)){

            row.no <- unlist(input$table1$changes$changes)[1]
            element = datacopy[(row.no + 1), "ElementCode"]
            year= datacopy[(row.no + 1), "Year"]
            cpccode= datacopy[(row.no + 1), "CPCCode"]
            col.no <- unlist(input$table1$changes$changes)[2]
            if(element == "5421"){#Yield

              datacopy$Value[datacopy$CPCCode == cpccode & datacopy$Year == year &  
                               datacopy$ElementCode == "5510"] =
                datacopy$Value[datacopy$CPCCode == cpccode & datacopy$Year == year & 
                                 datacopy$ElementCode == "5312"] *
                datacopy$Value[datacopy$CPCCode == cpccode & datacopy$Year == year & 
                                 datacopy$ElementCode == "5421"]

            }else if(element == "5510"){#Area

              datacopy$Value[datacopy$CPCCode == cpccode & datacopy$Year == year & 
                               datacopy$ElementCode == "5312"] = 
                datacopy$Value[datacopy$CPCCode == cpccode & datacopy$Year == year &  
                                 datacopy$ElementCode == "5510"] /
                datacopy$Value[datacopy$CPCCode == cpccode & datacopy$Year == year & 
                                 datacopy$ElementCode == "5421"]

            }

          }

        }

        datacopy

      }) 

      output$table1=renderRHandsontable({

        rhandsontable(mt())

      })

    }



    shinyApp(ui,server)