我希望能够根据用户输入选择的原始列来计算一列新数据,该新数据是将一个列除以另一个列所得到的结果。我希望可以将这个计算出的数据加入到原表格(或其副本)中。
我已经找到了如何创建一个数据框来响应列输入选择,并且也已经成功做出了将一个列除以另一个列的计算,但我无法创建一个最终的数据框,它包括所有原始列以及新计算出的列。
以下是我使用内置的鸢尾花数据制作的模拟。它显示了在第一个表格中选择的列的数据以及在第二个表格中的计算结果(您需要向下滚动相当远才能看到此处)。
我该如何将这些计算出的数据连接到原始来源?
非常感谢。
我已经找到了如何创建一个数据框来响应列输入选择,并且也已经成功做出了将一个列除以另一个列的计算,但我无法创建一个最终的数据框,它包括所有原始列以及新计算出的列。
以下是我使用内置的鸢尾花数据制作的模拟。它显示了在第一个表格中选择的列的数据以及在第二个表格中的计算结果(您需要向下滚动相当远才能看到此处)。
我该如何将这些计算出的数据连接到原始来源?
非常感谢。
#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()
})
}