0

I'm quite new to R and Shiny and wanted to create a boxplot with Shiny. Here is my code

UI part

ui <- shinyUI(fluidPage(
  titlePanel("Shiny app"),
  tabsetPanel(
    tabPanel(
      "Lab data (boxplot)",
      titlePanel("Lab data over time"),
      sidebarLayout(
        sidebarPanel(
          selectInput(
            "lab_df",
            "Choose lab dataframe",
            choices = ls(),
            selected = "lb"
          ),
          selectInput(
            "lab_test_var",
            "Choose lab test column",
            choices = NULL,
          ),
          selectInput(
            "lab_test_lvl",
            "Choose lab test performed",
            choices = NULL,
          ),
          tags$br(),
        ),
        mainPanel(plotOutput("lab_boxplot"))
      )
    )

  )
))

Server part

server <- shinyServer(function(input, output, session) {

  lab_df <- reactive({
    get(input$lab_df)
  })

  observe({
    col <- colnames(lab_df())
    updateSelectInput(session, "lab_test_var", choices = col, selected = "LBTEST")

  })

  observe({
    var <- lab_df()[[input$lab_test_var]]
    lvl <- unique(var)
    updateSelectInput(session, "lab_test_lvl", choices = lvl)
  })

  output$lab_boxplot <- renderPlot(ggplot(data=(labelled_dfs[[input$lab_df]] %>% filter(labelled_dfs[[input$lab_df]]$(input$lab_test_var) == input$lab_test_lvl)), aes(group=VISIT, y=LBORRES, x = VISIT)) +
    geom_boxplot()

})

I have a list of dataframes imported from SAS with package haven, I have in my global environment all of my dataframes as objects + a list (labelled_dfs) containing same dataframes with labels. Here is an example of my lab dataframe : Here my input$lab_test_var would be LAB and my input$lab_test_lvl would be 1 (or "Haemoglobins" in labelled dataframe for example) So I would like to make a boxplot for each "lab_test_lvl"

VISIT    LAB
1          1
2          1
3          1
1          2
2          2
3          2

Thank you for your help

Reeza
  • 17,138
  • 4
  • 16
  • 32
akben2
  • 19
  • 5
  • Shiny should not use your global enviroment – Bruno May 29 '20 at 13:49
  • I did this to simplify the code and not have to add a fileInput – akben2 May 29 '20 at 13:55
  • It shouldn't work, shiny is like another instance of r unless you really work for it the scope is all defined in your script, no objects outside of the script can be easily used – Bruno May 29 '20 at 14:03
  • My apps work with global env but I just wanted to make this one work, my problem is in the ggplot and with my inputs – akben2 May 29 '20 at 14:12
  • 1
    Create a reproducible example as it stands, No one can debug your code without going though a wall of text – Bruno May 29 '20 at 14:22
  • Removing SAS tag as SAS is not relevant to the question. – Reeza May 31 '20 at 19:53

0 Answers0