将列添加到Shiny中的反应数据框架并更新它们 [英] Add columns to a reactive data frame in Shiny and update them

查看:47
本文介绍了将列添加到Shiny中的反应数据框架并更新它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够通过将一列除以另一列,并用用户输入选择两个原始列来计算新的数据列。我希望将这些计算出的数据加入到原始表(或它的副本)中。

I would like to be able to calculate a new column of data based on dividing one column by another, with both original columns selected by a user input. I would like to have this calculated data joined to the original table (or a copy of it).

我设法弄清楚了如何使一个数据框对列输入选择,并且我设法进行了将一列除以另一列的计算,但是我无法生成一个包含所有原始列以及新计算出的列的最终数据框。

I have managed to figure out how to make a dataframe that reacts to the column input selection, and I have managed to make a calculation that divides one column by the other, but I have not been able to make a final dataframe that includes all of the original columns as well as the new calculated one.

这里是我使用内置的Iris数据制作的模型。它显示第一个表中所选列的数据,第二个表中的计算(您将需要向下滚动才能看到它)。

Here is a mock up I have made using the built in Iris data. It displays the data for the columns selected in the first table, and the calculation in the second table (you will need to scroll down quite far to see this).

操作方法我可以将此计算的数据加入原始数据源吗?

How can I join this calculated data to the original source?

非常感谢

#Ui        
pageWithSidebar(
      headerPanel('Calculate Column'),
      sidebarPanel(

        #select variables from iris dataset
        selectInput('xcol', 'X Variable', names(iris)),
        selectInput('ycol', 'Y Variable', names(iris),
                    selected=names(iris)[[2]])
      ),
      mainPanel(
        #display the selected variables
            tableOutput("view"),
         #display the calculated variable
            tableOutput("view2")
      )
    )


#Server
        function(input, output, session) {

      # Combine the selected input variables into a new data frame
      selectedData <- reactive({
        iris[, c(input$xcol, input$ycol),]
      })


      # divide one variable selection by the other
      selectedData2 <- reactive({
                iris$new<-iris[, c(input$xcol)]/iris[, c(input$ycol)]

        })

      # create data output for selected variables
      output$view <- renderTable({selectedData()
      })

      # create data output for calculated variable
      output$view2 <- renderTable({selectedData2()
      })

    }


推荐答案

您忘记了 iris 不是反应性元素,因此您的代码无法正常工作。您在这里有两个选择:

You forget that iris is NOT a reactive element, so your code can't work. You have two options here:


  • 使用 reactive()创建反应性值来存储该数据帧

  • 使用 reactiveValues()

  • creating a reactive value to store that data frame, using reactive().
  • creating a list of "global" reactive values in which you can store the updated data frame, using reactiveValues().

使用 reactiveValues ,您可以创建一个反应式列表,就像 input output 一样。在下面的示例中,我将其用于将数据框 iris 存储为 globals $ mydf 。然后,您可以使用例如 observe 来被动地更改值,如以下服务器功能所示:

Using reactiveValues you can make a list of reactive expressions much like input and output are. In the example below I use it to store the data frame iris as globals$mydf. Then you can use eg observe to change the value reactively, as in the following server function:

#Server
server <- function(input, output, session) {

  globals <- reactiveValues(
    mydf = iris
  )

  observe({
    thedf <- globals$mydf
    newvar <- thedf[[input$xcol]] / thedf[[input$ycol]]
    globals$mydf$ratio <- newvar
  })


  # create data output for selected variables
  output$view <- renderTable({
    globals$mydf
  })
}



使用react()



您可以创建两个相互依赖的反应表达式:

using reactive()

You can make two reactive expressions that depend on eachother:


  • 一个选择变量并计算该比率

  • 将这个结果与所选变量组合起来的比率

您的服务器将如下所示:

Your server would look like this :

server <- function(input, output, session) {

  newdf <- reactive({
    cbind(
      iris[c(input$xcol, input$ycol)],
      Ratio = newvar()
    )
  })

  newvar <- reactive({
    iris[[input$xcol]] / iris[[input$ycol]]
  })

  # create data output for selected variables
  output$view <- renderTable({
    newdf()
  })

}

尽管您认为这不是您要找的东西,但是您可以在其他代码中使用 newdf()就像在上一个示例中使用 globals $ mydf 一样。 reactiveValues()特别是在代码的不同部分必须能够更改数据框架时会有所回报。

Although you beliefe this is not what you're looking for, you can use newdf() in other code just like you would use globals$mydf in the previous example. reactiveValues() especially pays off if different parts of your code have to be able to change the data frame.

这篇关于将列添加到Shiny中的反应数据框架并更新它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆