繁体   English   中英

R ggplot无响应数据集

[英]R ggplot unresponsive dataset

我需要特定的值来随选择进行更改。

#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]等自动启动。但是如何在使代码响应用户输入的同时设置它呢?

我不想浪费太多代码,但是如果有人对如何使值对输入做出反应有任何想法,我将非常感激。

为了让情节是被动 ,你打算,你必须做出的subsetting的部分是被动。 它必须获取用户输入的值(在您的情况下为“运动员”和“生物标志物”),并在绘制数据之前使用这些值对数据进行子集化。 请注意,您的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))
          )
  })

})

这会产生您想要的反应性曲线(如下所示)。 当您更改输入时,绘图也会更改。 在此处输入图片说明 希望能帮助您前进。

暂无
暂无

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

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