简体   繁体   中英

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.

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. You could even change to dplyr::filter() if you wanted.

The answer using eventReactive() :

server <- function(input, output) {

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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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