简体   繁体   中英

R ggplot unresponsive dataset

I need particular values to change with selection.

#server.R 
bio <- read.csv('bio.csv')


bioSlice <- subset(bio, "Athlete" == "AA" & "Biomarker" == "B1")
h<-bio[,1]
i<-bio[,2]

shinyServer(function(input, output) {

 formulaText <- reactive({
  paste(input$Athlete, "~", input$Biomarker)
 })

 output$caption <- renderText({
   formulaText()
 })

output$bioPlot <- renderPlot({
d=data.frame(x1=c(1, h),x2=c(h, i))
print(ggplot() +
})

})

#ui.R
shinyUI(pageWithSidebar(

  # Application title
   headerPanel("Results"),



  sidebarPanel(
    selectInput("Athlete", "Athlete:",
                list("1" = "1",  
                     "2" = "2")),

    selectInput("Biomarker", "Biomarker:",
                list("B1" = "B1",
                     "B2" = "B2"))
  ),

  mainPanel(
    h3(textOutput("caption")),
    plotOutput("bioPlot")

  )
))

My data.frame doesn't seem to like the way I've set the variables. Maybe I need to get it to start automatically at say h=bio[1,1], i=bio[1,2], etc. But how do I set this while making the code responsive to the users input?

I don't want to give away too much of my code, but if anyone has any ideas as to how to make the values react to the input, I'd be very grateful.

To get the plot to be reactive as you intend, you have to make the subsetting part to be reactive. It has to take the user input values (Athlete and Biomarker in your case) and use those values to subset the data before you plot it. Notice that your d=data.frame(...) statement is not reactive at all.

In your server.R, you have to add a reactive function:

  biomarkerSlice <- reactive({
    subset(biomarker.df, Athlete==input$Athlete & Biomarker==input$Biomarker)
  })

and call this reactive function using biomarkerSlice() when plotting. That way, the resulting plot will change with user input.

Your UI.R is fine. (I just added a tableOutput line to illustrate.) All the reactive elements above go into Server.R.

Here's a fully working version that I tested with some data dummy data.

UI.R

    #ui.R
    # Define UI for Blood Results application
    shinyUI(pageWithSidebar(

     headerPanel("Results"),


  sidebarPanel(
    selectInput("Athlete", "Athlete:",
                list("AA" = "AA",  
                     "BB" = "BB",
                     "CC" = "CC",
                     "DD" = "DD",
                     "EE" = "EE")),

    selectInput("Biomarker", "Bio marker:",
                list("Creatinine" = "Creatinine",
                     "B2" = "B2",
                     "B3" = "B3",
                     "B4" = "B4"))
  ),

  mainPanel(
    h3(textOutput("caption")),
    tableOutput("biomarkerDataTable"),
    plotOutput("biomarkerPlot")

  )
))

Server.R

library(shiny)
library(ggplot2)
library(reshape2)

# dummy data
biomarker.df <- data.frame(Athlete=rep(c("AA","BB","CC","DD","EE"), 4),
                           Biomarker=rep(c("Creatinine","B2","B3","B4"), 5),
                           Outside = sample(50:70,20, replace=T),
                           Lower = sample(40:80,20, replace=T),
                           Value = sample(42:90,20, replace=T),
                           Higher = sample(60:110,20, replace=T))

shinyServer(function(input, output) {

  biomarkerSlice <- reactive({
    subset(biomarker.df, Athlete==input$Athlete & Biomarker==input$Biomarker)
  })

  # Compute the formula text in a reactive expression since it is 
  # shared by the output$caption and output$Plot expressions
  formulaText <- reactive({
    paste(input$Athlete, "~", input$Biomarker)
  })

  # Return the formula text for printing as a caption
  output$caption <- renderText({
    formulaText()
  })

  #subset to the Ath and the BioMarker of interest
  output$biomarkerDataTable <- renderTable({
    biomarkerSlice()
  })

  # Generate a plot of the requested Athlete against requested Biomarker
  output$biomarkerPlot <- renderPlot({
    melted_df <- melt(biomarkerSlice())
    print(ggplot(melted_df, aes(x=variable, y=value, fill=variable))+
            geom_bar(stat="identity") + 
            theme(axis.text.x  = element_text(vjust=0.5, size=20))
          )
  })

})

And that produces a reactive plot as you desire (Shown below). As you change the inputs, the plot changes. 在此处输入图片说明 Hope that helps you move forward.

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