有光泽:在数据框中反应一列

时间:2015-11-09 20:39:16

标签: shiny reactive-programming

我找到了包裹" rhandsontable"在闪亮的输出表是非常有用的。这是我的剧本:

library(shinydashboard)
library(shiny)
library(data.table)
library(rhandsontable)
DF = data.frame(num = 1:10, price = 1:10,
            stringsAsFactors = FALSE)
ui = fluidPage(
titlePanel("sample"),
fluidRow(box(rHandsontableOutput("hot", height = 400)))  
)
server = function(input, output) {
 output$hot = renderRHandsontable({
 DF$total=DF$num*DF$price
 rhandsontable(DF)
})
}
shinyApp(ui, server)

我的问题是,当我修改价格列中的值时,我如何反应总列中的值。为清楚起见,如果num是常量,当我将价格从2更改为4时,总列中的值将自动更改。有没有人有解决方案?

2 个答案:

答案 0 :(得分:1)

使您的数据具有反应性,将更改函数绑定到表中,然后更新更改后的值(现在使用sum列):

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

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

ui = fluidPage(
  titlePanel("sample"),
  fluidRow(box(rHandsontableOutput("hot", height = 400)))  
)
server = function(input, output) {
  data <- reactiveValues(df=DF)

  output$hot <- renderRHandsontable({
    isolate({
      data$df$total       <- data$df$num*data$df$price
      print(sum(data$df$num*data$df$price) )
      data$df$total[11]   <- sum(data$df$num*data$df$price) 
    })
    rhandsontable(data$df, selectCallback = TRUE) 
  })

  observeEvent(input$hot$changes,{
    print('Change')

    # Get changed value
    row.i <- input$hot_select$select$r
    col.i <- input$hot_select$select$c
    new.v <- unlist( input$hot$changes$changes )
    new.v <- new.v[[length(new.v)]]

    # Save and update the value
    data$df[row.i,col.i] <- new.v
    data$df$total <- data$df$num[row.i]*data$df$price[row.i]

    # Calculate Sum 
    data$df$total[11] <- sum(data$df$total)
  })

}
shinyApp(ui, server)

答案 1 :(得分:0)

@PSraj,我不知道这是不是你问的问题。但是这里是@Oskar Forsmo发布的带有转换hot_to_r的示例,以便将rhandsontable转换为r数据框对象。

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

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

ui = fluidPage(
  titlePanel("sample"),
  fluidRow(box(rHandsontableOutput("hot", height = 400)),
           textOutput("text1"),
           box(width = 6,dataTableOutput("text2")))  
)
server = function(input, output) {
  data <- reactiveValues(df=DF)

  output$hot <- renderRHandsontable({
    isolate({
      data$df$total       <- data$df$num*data$df$price
      print(sum(data$df$num*data$df$price) )
      data$df$total[11]   <- sum(data$df$num*data$df$price) 
    })
    rhandsontable(data$df, selectCallback = TRUE) 
  })

  observeEvent(input$hot$changes,{
    print('I have change a value')

    # Get changed value
    row.i <- input$hot_select$select$r
    col.i <- input$hot_select$select$c
    new.v <- unlist( input$hot$changes$changes )
    new.v <- new.v[[length(new.v)]]

    data$df[row.i,col.i] <- new.v
    data$df$total <- data$df$num[row.i]*data$df$price[row.i]


    output$text1 <- renderText({ 
      sprintf("You changed line %s value to %s",row.i,new.v)
    })

    output$text2 <- renderDataTable({ 
     hot_to_r(input$hot) #Here we get the dataframe from rhandsontable
                         # You can store it as a reactive variable to do
                         # anything you want
    })

  })



}
shinyApp(ui, server)