闪亮:反应数据框中的一列

shiny: reactive a column in a data frame

我发现包 "rhandsontable" 对在 shiny 中输出 table 非常有用。这是我的脚本:

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)

我这里的问题是,当我修改price栏中的值时,如何反应total栏中的值。说清楚一点,如果 num 是常量,当我把价格从 2 改成 4 时,total 列中的值会自动改变。有人有解决办法吗?

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

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)

@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)