0

I make an updateSelectInput on shiny, it's working. But after I can't use the new input as a variable for an output... The input is always empty. I give you the code for the SelectInput in Ui.R and the update in server.R. I can't give more, because the updating is made via an access database. And if I create data.frame just for the example, it will work...

selectInput("indic","Indicateur :",
                                    choices = NULL,selected = NULL),

observeEvent(input$Source,{
          indicateurs<-as.character(voila_source(input$Source)$Indice)


          updateSelectInput(session,"indic",
                            choices = indicateurs)
     })

output$summary<-renderTable({     
          information<-voila_source(input$Source)
          information<-information[,-1]
          indica<-input$indic   ##here is empty...
          print(indica)
          description<-filter(information,Indice==indica)
          description
     })

Maybe I forgot something, I don't know. I want select an input and print a data.frame corresponding at the input selected.

EDIT : Answer found

Ok my code and your code work... It have to push on the submitbutton... But I don't want to push on submitbutton for that, I want just to click on selectInput to print my output, that is a description of the selectInput, and if I want this one, I push on the button to display a graph.

I found the error, the submitbutton, I replaced by actionbutton and it's working... I was not aware about the submitbutton and actionbutton.

If it could help you, This is my code for call the access database and all the server.R code and ui.R code :

    library(shiny)
    library(anytime)
    library(plotly)
    library(ggplot2)
    library(dplyr)
    library(RODBC)
    library(ecb)


    channel<-odbcConnectAccess("H:\\Analyse Macro\\Base Macro live.mdb")
    listee<-sqlQuery(channel,paste("Select * from Liste_source"))
    liste_server<-list()
    for (i in 1:length(listee$Table)){
         liste_server[i]<-as.character(listee$Table[i])
    }
    names(liste_server)<-as.character(listee$Table)

    for (i in 1:length(listee$Table)){
         liste_server[[i]]<-sqlQuery(channel,paste("Select * from ",liste_server[i]))
    }

    voila_source<-function(selection){
         x<-as.character(selection)
         liste_donnee<-liste_server[[x]]
         #liste_donnee<-as.character(liste_donnee$Indice)
         liste_donnee$Indice<-as.character(liste_donnee$Indice)
         liste_donnee$Description<-as.character(liste_donnee$Description)
         liste_donnee$Unite<-as.character(liste_donnee$Unite)
         liste_donnee$Frequence<-as.character(liste_donnee$Frequence)
         liste_donnee$Code<-as.character(liste_donnee$Code)
         liste_donnee$Pays<-as.character(liste_donnee$Pays)

         liste_donnee
    }



    # Define server logic required to draw a histogram
    shinyServer(function(input, output,session) {




         observeEvent(input$Source,{
              indicateurs<-as.character(voila_source(input$Source)$Indice)


              updateSelectInput(session,"indic",
                                choices = indicateurs)
         })


         output$summary<-renderTable({     
              information<-voila_source(input$Source)
              information<-information[,-1]
              reactives$indica<-input$indic
              print(reactives$indica)

              description<-filter(information,Indice==reactives$indica)
              description<-data.frame(test=indica)
              description
         })

    })


ui.R

    library(shiny)
    #library(quantmod)
    library(lubridate)
    library(plotly)
    library(ggplot2)
    library(RODBC)

    channel<-odbcConnectAccess("H:\\Analyse Macro\\Base Macro live.mdb")
    liste<-sqlQuery(channel,paste("Select * from Liste_source"))
    liste<-as.character(liste$Table)

    # Define UI for application that draws a histogram
    #shinyUI(fluidPage(
    ui<-tagList(
         navbarPage(
              "Evolutions Economiques",
              tabPanel("Observation",
                       # Application title
                       titlePanel("Evolutions Economiques"),

                       # Sidebar with a slider input for number of bins
                       #sidebarLayout(
                       sidebarPanel(
                            h1("Selection des donnees"),
                            selectInput("Source","Source :",
                                        choices =liste),
                            selectInput("indic","Indicateur :",
                                        choices = NULL,selected = NULL),
                            selectInput("pays","Pays :",
                                        choices = NULL),
                            selectInput("partenaire","Partenaire :",
                                        choices = NULL),
    #### replace by actionbutton submitButton("Ajouter"),
    actionButton("add","Ajouter"),
                            hr(),
                            img(src="logo.png",height=80,width=200),
                            br(),
                            br(),
                            helpText("Application realisee pour l'exploration des donnees macroeconomiques")
                       ),

                       # Show a plot of the generated distribution
                       mainPanel(
                            tabsetPanel(type="tabs",
                                        tabPanel("Description",tableOutput("summary"))
                                                 #,
                                        #plotlyOutput("graph"))
                       ))
              ),

              tabPanel("Extraction",
                       sidebarPanel(

                            selectizeInput("Index","Indice",c("ok")),
                            textInput("Nom","Nom fichier"),
                            actionButton("save","Sauver"),
                            hr(),
                            img(src="logo.png",height=80,width=200),
                            br(),
                            br(),
                            helpText("Application realisee pour l'exploration des donnees macroeconomiques")
                       ),

                       mainPanel(
                            tabsetPanel(type="tabs",
                                        tabPanel("liste",tableOutput("source")))
                       )


              ))
    )
Aurélien
  • 103
  • 3
  • 12
  • Without being able to look at the code, I don't think anyone's going to be able to solve your problem definitively. Are you sure the database access is working correctly and the values of `indicateurs` are what you're expecting? – divibisan Sep 10 '18 at 17:25
  • I put the full code below. The database access works correctly and the values of "indicateur" are what I expect. When I launch the app, the SelectInput are not empty, so my input$source is not empty, just my input$indic is it, because I have no output after selection...And the print() print "" on the console – Aurélien Sep 11 '18 at 13:37
  • Answers are just for attempts to answer the question. You should `edit` your question to insert the code by clicking the `edit` button under the tags – divibisan Sep 11 '18 at 14:41

1 Answers1

0

Judging from your example, it seems you have not initialised your indicateurs or indica variables is that correct?

If so you would need a couple of extra lines. The reason your solution (creating data.frame) works is that when you're testing your app, the variable already exists for the observeEvent or renderTable functions to act on. So simply add some lines in your script to do so before they are called.

Here is an example using reactiveValues (which would be better to work with when using a shiny app):

selectInput("indic","Indicateur :",
                                    choices = NULL,selected = NULL),

# goes in your server.R
reactives <- reactiveValues(indicateurs = NULL, indica = NULL)
observeEvent(input$Source,{
          reactives$indicateurs <-as.character(voila_source(input$Source)$Indice)


          updateSelectInput(session,"indic",
                            choices = reactives$indicateurs)
     })

output$summary<-renderTable({     
          information<-voila_source(input$Source)
          information<-information[,-1]
          reactives$indica<-input$indic   ##here is empty...
          print(reactives$indica)
          description<-filter(information,Indice==reactives$indica)
          description
     })
MattMyint
  • 66
  • 7
  • thank you for your comment, but it doesn't work... input$indic is still empty... I don't understand, my selectInput in the app is not empty but the output is it... – Aurélien Sep 11 '18 at 13:23
  • Ok my code and your code work... It have to push on the action button... But I don't want to push on action button for that, I want just to click on selectInput to print my output, that is a description of the selectInput, and if I want this one, I push on the button to display a graph. – Aurélien Sep 11 '18 at 13:47
  • Your action button input isn't linked to anything in your example code. Is that intentional? – MattMyint Sep 12 '18 at 01:38