2017-04-07 78 views
1

我希望能夠根據一列除以另一列來計算新的數據列,並且通過用戶輸入選擇兩個原始列。我想將這個計算的數據加入到原始表格(或其副本)中。將列添加到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() 
     }) 

    } 
+0

爲了更好地描述實際問題,我冒昧地重新制定了標題。標題中沒有必要提及R和Shiny,它們已經在標籤中。 –

回答

0

你不要在你的reactiveselectedData2返回任何東西,你只是一個增量<-,我認爲你應該這樣做:

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

} 
+0

謝謝,我已經試過使用這段代碼,它似乎仍然以相同的方式運行,還有什麼我需要改變,以最終與所有原始虹膜變量和新計算的數據集? – RobinNN

+1

@RobinNN您可以使用全局變量,如我更新的答案 –

3

你忘了iris是不是反應性元素,所以你的代碼無法工作。您有兩種選擇:

  • 使用reactive()創建一個反應值來存儲該數據幀。
  • 使用reactiveValues()創建一個「全局」無功值列表,您可以在其中存儲更新的數據幀。

使用與reactiveValues

全球活性表達式中使用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 
    }) 
} 

反應()

您可以依賴於海誓山盟兩種反應式:

  • 選擇變量並計算該比率的變量
  • 將該結果與所選變量組合的結果

您的服務器應該是這樣的:

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

} 

雖然你beliefe這是不是你要找的內容,你可以使用newdf()在其他代碼,就像你在前面的例子中使用globals$mydfreactiveValues()尤其是你的代碼的不同部分必須能夠更改數據框。

+0

所示,非常感謝您花時間解答並解釋 – RobinNN

相關問題