1
TD <- thyroid

library(readxl)
library(shiny)
library(ggplot2)
library(shinythemes)
library(DT)

ui <-shinyUI(fluidPage(pageWithSidebar(
  headerPanel("Test App"),
  sidebarPanel(
    selectInput("xaxis", "Choose a x variable", choices = names(TD)),
    selectInput("T4", "T4 Rate", choices = c("All","0 - 80","80 - 140","> 140")),
    selectInput("T3", "T4 Rate", choices = c("All","0 - 80","80 - 140","> 140")),
    selectInput("TSH", "T4 Rate", choices = c("All","0 - 80","80 - 140","> 140")),
    
    actionButton("goButton","Update")
  ),
  mainPanel(
    tabsetPanel(
      tabPanel('Plot1', plotOutput("plot1")),
      tabPanel('Plot2', plotOutput("plot2"))
    ))
)
))

server <- shinyServer(function(input,output, session){
  
  data1 <- reactive({
    if(input$T4 == "All"){
      TD
    }
    else if(input$T4 == "0 - 80"){
      TD[which(TD$T4 >= 0 & TD$T4 < 80),]
        

    }
    else if(input$T4 == "80 - 140"){
      TD[which(TD$T4 >= 80 & TD$T4 < 140),]
      
    }
    else{
      TD[which(TD$T4 >= 140),]
    
      
        
    }
  })

  x_var<- eventReactive(input$goButton, {
    input$xaxis
  })
  

  output$plot1 <- renderPlot({
    x <- x_var()
    
    p <- ggplot() + geom_bar(aes(x=TD[[x]], fill = TD$ThryroidClass))
    p  #+ 
      #theme(plot.title = element_text(hjust = 0.5, size=20))
  })
  output$plot2 <- renderPlot({
    x <- x_var()
    
    p <- ggplot() + geom_bar(aes(x=data1[[x]], fill = TD$ThryroidClass))
    p  #+ 
      #theme(plot.title = element_text(hjust = 0.5, size=20))
  })
})

shinyApp(ui,server)

I m working on the thyroid dataset i wanna compare groups of patients which are probably sick from thyroid based on the T4 rate. THis is why i have created a subset of my original according to that rate. i have done 2 plos, the one with the whole dataset works perfectly, but the second with the sub set five me the followinf error : Error[object object] How can i handle this please??

  • 1
    what package contains this dataset `thyroid`? please provide a sample of data [reproducible R code](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Vida Jan 08 '23 at 18:27
  • In the second plot, data1 is a reactive, a kind of function, so try `data1()[[x]]` instead of `data1[[x]]` and `fill = data1()[["ThyroidClass"]]` – Ric Jan 08 '23 at 18:29
  • there is no package which contains my data infrtunately – souhail Lyamani Jan 08 '23 at 18:29
  • @RicVillalba is this syntax true?(else if(input$T4 == "0 - 80"){ TD[which(TD$T4 >= 0 & TD$T4 < 80),]) – souhail Lyamani Jan 08 '23 at 18:44
  • 1
    Please provide your dataset using `dput(thyroid)` or use an example dataset (e.g., `mtcars`) to make your post [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – jrcalabrese Jan 08 '23 at 19:37

0 Answers0