我希望能夠根據一列除以另一列來計算新的數據列,並且通過用戶輸入選擇兩個原始列。我想將這個計算的數據加入到原始表格(或其副本)中。將列添加到Shiny中的反應數據框中並更新它們
我已經設法弄清楚如何製作一個對列輸入選擇有反應的數據框,並且我設法進行了一個列除以另一列的計算,但是我還沒有能夠做出最終的數據框其中包括所有原始列以及新計算的列。
這裏是我使用內置的虹膜數據進行的模擬。它顯示在第一個表格中選擇的列的數據,並在第二個表格中顯示計算結果(您需要向下滾動以查看此內容)。
如何將此計算數據加入原始來源?
非常感謝
#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()
})
}
爲了更好地描述實際問題,我冒昧地重新制定了標題。標題中沒有必要提及R和Shiny,它們已經在標籤中。 –