简体   繁体   English

基于用户输入的 R-Shiny 更新数据框

[英]R-Shiny Update data frame based off of user input

If I have a data frame with 4 different columns, 3 of them being categorical and 1 being continuous, I want to get the continuous values of the data frame depending on what categorical values are selected.如果我有一个包含 4 个不同列的数据框,其中 3 个是分类的,1 个是连续的,我想根据选择的分类值来获取数据框的连续值。

That is, I want to stratify/update the data frame whenever the user checks off another radiobutton (or any input really)也就是说,每当用户检查另一个单选按钮(或任何输入)时,我都想对数据框进行分层/更新

For example, if one of the columns was whether they ate food or not, I would like to stratify based off of this, but I don't know how I can continuously update the data frame, I have the option as a radiobutton but例如,如果其中一列是他们是否吃过食物,我想以此为基础进行分层,但我不知道如何不断更新数据框,我可以选择作为单选按钮但

mydata = read_excel('thisdata.xlsx')

ui <- .....

server <- function(input, output){
  observeEvent(input$Food, {
    if (input$Food == "Yes"){
      mydata = mydata[mydata$Food == "Yes", ]
    }
    if (input$Food == "No"){
      mydata = mydata[mydata$Food == "No", ]
    }
  })
  output$distPlot <- renderPlot({
    plot(mydata$numCals)
  })
}

I was expecting the plot to change as I selected Yes/No on the radiobuttons but the plot remains the same, it's actually as if I were not stratifying at all, it's just taking it all together.当我在单选按钮上选择是/否时,我期待情节发生变化,但情节保持不变,实际上好像我根本没有分层,只是将它们放在一起。 How can I obtain something like this?我怎样才能获得这样的东西?

Note: I changed your code for the filter.注意:我更改了您的过滤器代码。 You don't need the if statement.您不需要 if 语句。 You could even change to dplyr::filter() if you wanted.如果需要,您甚至可以更改为dplyr::filter()

The answer using eventReactive() :使用eventReactive()的答案:

server <- function(input, output) {

  mydata_new <- eventReactive(input$Food, {
    mydata[mydata$Food == input$Food, ]
  })

  output$distPlot <- renderPlot({
    plot(mydata_new()$numCals)
  })

}

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

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