0
votes

I tried to read dataframe rows from one column to feel a list in a SelectInput objet in a UI.R Shiny I have a problem with global or local ref between the UI and Server and I don't know if the format is right to import items in the selectInput List

Here my DF Ref_comp with just one column (STEP_NAME):

!   STEP_NAME   !
-----------------
  L1_2_3_LR
  C46-C77-OTHERS
  R4
  R10
  C56
  Q4
  L4

Here my UI.R

 shinyUI(pageWithSidebar(

  headerPanel("My header Text"),
  sidebarPanel(  
    radioButtons("test", "Select a DataBase", 
                       c("test1"="test1",
                        "test2"="test2")),
          textInput("card", "Enter the code card", "CARD.EX"),
          textInput("comp", "Enter the ref comp", "R3"), 
          ######## Here what I tried to do ########
          selectInput("comp_sel","Component", choices= 
          as.character(unique(unlist(Ref_comp$STEP_NAME)))),
          ########################################## 
           downloadButton("plot_export", "Save PDF")
                        ),  

 mainPanel(
    #h4("Text2"),
       library(plotly),
       plotlyOutput("plot"))


     ))

Here my Server.R

shinyServer(function(input,output){

output$plot <- renderPlotly({

con <- odbcConnect(con, uid="xxx")

##### Here the SQL Query to have my items ######################
sql_ref = paste("select DISTINCT ...") # My SQL query on distant server
###### Output in DF Ref_comp ##############
Ref_comp <- sqlQuery(db, paste (sql_ref))
##########################################
 odbcClose(data_testeur)

 #### An other SQL Query for the graph #######

 graph <- ggplot(...
 ggplotly(graph) # Print graph
 }

 ) 
  })

Thank you for your help

1
Will the Shiny UI found dataframe object where query is placed in the server code ? I mean about scope object betwen UI and Servertrayvou jba

1 Answers

2
votes

Your problem is that you the data_frame Ref_comp is generated in server.r when this is the case we have to generate the selectInput dynamic with a renderUI and uiOutput() like this:

shinyUI(pageWithSidebar(

  headerPanel("My header Text"),
  sidebarPanel(  
    radioButtons("test", "Select a DataBase", 
                 c("test1"="test1",
                   "test2"="test2")),
    textInput("card", "Enter the code card", "CARD.EX"),
    textInput("comp", "Enter the ref comp", "R3"), 
    ######## Here what I tried to do ########
    uiOutput("selectComp"),
    ########################################## 
    downloadButton("plot_export", "Save PDF")
  ),  

  mainPanel(
    #h4("Text2"),
    library(plotly),
    plotlyOutput("plot"))


))

and server

shinyServer(function(input,output){

  refDataFrame <- reactive({
    con <- odbcConnect(con, uid="xxx")

    ##### Here the SQL Query to have my items ######################
    sql_ref = paste("select DISTINCT ...") # My SQL query on distant server
    ###### Output in DF Ref_comp ##############
    Ref_comp <- sqlQuery(db, sql_ref)
    odbcClose(data_testeur)
    Ref_comp
  })

  output$selectComp <- renderUI(
    selectInput("comp_sel","Component", choices= 
                  as.character(unique(unlist(refDataFrame()[["STEP_NAME"]]))))
  )

  output$plot <- renderPlotly({
    Ref_comp <- refDataFrame()

    #### An other SQL Query for the graph #######

    graph <- ggplot(...)
                    ggplotly(graph) # Print graph
  }

    ) 
})

Since we now need the result of the database query in two places have put it in a separate reactive function