R ggplot无响应的数据集

时间:2014-01-21 23:23:03

标签: r ggplot2 shiny

我需要特定的值来改变选择。

#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")

  )
))

我的data.frame似乎不喜欢我设置变量的方式。也许我需要让它自动启动,例如h = bio [1,1],i = bio [1,2]等。但是如何在使代码响应用户输入的同时设置它呢?

我不想泄露太多的代码,但如果有人对如何使值对输入作出反应有任何想法,我将非常感激。

1 个答案:

答案 0 :(得分:0)

要使情节按照您的意图被动,您必须使subsetting部分成为被动的。它必须采用用户输入值(在您的情况下为Athlete和Biomarker)并在绘制数据之前使用这些值对数据进行子集化。请注意,您的d=data.frame(...)声明根本不具有反应性。

在您的server.R中,您必须添加一个反应函数:

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

并在绘图时使用biomarkerSlice()调用此反应函数。这样,结果图将随用户输入而改变。

你的UI.R很好。 (我刚刚添加了一个tableOutput行来说明。)上面的所有被动元素都进入了Server.R。

这是一个完整的工作版本,我测试了一些数据虚拟数据。

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

})

这会产生你想要的反应情节(如下所示)。当您更改输入时,绘图会更改。 enter image description here 希望能帮助你前进。