繁体   English   中英

将列添加到Shiny中的反应数据框架并更新它们

[英]Add columns to a reactive data frame in Shiny and update them

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

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

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

如何将计算所得的数据加入原始数据源?

非常感谢

#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不是反应性元素,因此您的代码无法工作。 您在这里有两个选择:

  • 使用reactive()创建一个反应性值来存储该数据帧。
  • 创建一个“全局”反应性值列表,您可以使用reactiveValues()在其中存储更新的数据框。

将全局反应式表达式与reactValues一起使用

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

#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()

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

  • 一个选择变量并计算该比率的变量
  • 将结果与所选变量结合在一起的变量

您的服务器如下所示:

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()特别有用。

您没有为selectedData2reactive返回任何内容,您只需做一个增量<- ,我认为您应该这样做:

   function(input, output, session) {

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


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

    })

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

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM