【问题标题】:shiny: reactive a column in a data frame闪亮:反应数据框中的列
【发布时间】:2016-02-10 14:43:22
【问题描述】:

我发现包“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 时,总列中的值会自动更改。有没有人有解决方案?

【问题讨论】:

    标签: shiny reactive-programming


    【解决方案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)
    

    【讨论】:

    • 感谢您的帮助。我需要额外的协助吗?我想知道我是否想在底部添加一个新行来总计列。我该如何更改脚本。当total的值发生变化时,sum的值也会随之更新。
    • 我发现当我更改表中的值时,它会创建一个新表。是否有任何解决方案只会更新表或不创建新表?谢谢!
    • 我不知道,我对 rhandsontable 不太熟悉,所以也许有办法做到这一点,但通常你必须通过闪亮的渲染函数来更新 UI
    • 谢谢。我会看看那个。
    • @ChristopheD。您能否根据发布的此示例数据集发布工作代码,因为我无法将您的输入转换为工作示例。谢谢
    【解决方案2】:

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

    【讨论】:

    • 为什么这段代码会崩溃?我得到:警告:$
    猜你喜欢
    • 2015-03-21
    • 2013-12-22
    • 1970-01-01
    • 2015-05-13
    • 2017-04-13
    • 1970-01-01
    • 2017-04-14
    • 2019-07-17
    • 1970-01-01
    相关资源
    最近更新 更多