data frame not available outside observe event

good day , have a problem hoping someone could kindly help me out.
Wrote this code it runs a query and stores results in a variable "mydata" , the problem is that when I try to use this data frame outside the observer it isnt available, already declared it outside de observer as null and with reactivebind and reactivevalues with no luck so far, theres my code

library(shiny)
    library(DT)
    library(RODBC)
    library(shinythemes)
    library(shinydashboard)
    filename="C:/Users/mg3rami/Documents/apptienda.txt"
    sql=readChar(filename,file.info(filename)$size)


    ui=navbarPage(theme=shinytheme("sandstone"),title=h3("App Tienda V2"),
                  tabPanel(
                    ("Consulta"),
                    sidebarPanel(
                      textInput("Tienda","Tienda"),
                      textInput("depto","depto"),
                      textInput("Articulo","Articulo"),
                      actionButton("mybutton","Consultar Tienda")

                    ),
                    mainPanel(


                    ),
                    DT::dataTableOutput("tableDT1")


                  ),
                  tabPanel(("DETALLE"),
                           DT::dataTableOutput("tableDT")),

                  tabPanel(("text"),

                           shinyUI(fluidPage(
                             tags$head(tags$style(HTML("
                                                       #final_text {
                                                       text-align: center;
                                                       }
                                                       div.box-header {
                                                       text-align: center;
                                                       }
                                                       "))),
                             box(verbatimTextOutput("Store_Nbr"), status = "primary", solidHeader = TRUE, collapsible = TRUE, width = 2, title = "Collapsable text")
                             )))







                  )





    server=function(input,output,session){
#also used this values <- reactiveValues(mydata = NULL) / values$mydata insted of only mydata
      mydata=NULL
makeReactiveBinding("mydata")
      observeEvent(input$mybutton,{
        withProgress({
          setProgress(message = "Procesando consulta espere...")
          if(is.null(input$Tienda)){
            return(NULL)
          }else if((input$Articulo=="") & (input$depto=="")){ 
            my_sql=gsub("condicionales","Store_Nbr IN (Tienda)",sql)
          }
          else if(input$depto==""){
            my_sql=gsub("condicionales","Store_Nbr IN (Tienda) AND Old_Nbr IN (Articulo)",sql)
          }
          else if(input$Articulo==""){
            my_sql=gsub("condicionales","Store_Nbr IN (Tienda) AND DEPT_nbr IN (depto)",sql)
          }
          else {
            my_sql=gsub("condicionales","Store_Nbr IN (Tienda) AND Old_Nbr IN (Articulo) AND DEPT_nbr IN (depto)",sql)
          }


          my_sql=gsub("depto",input$depto,gsub("Tienda",input$Tienda,gsub("Articulo",input$Articulo,my_sql)))
          ch=odbcConnect("WM3",  uid="***",pwd="***") 
          resultset=sqlQuery(channel=ch,query=my_sql)

          odbcClose(channel=ch)  
          output$tableDT=DT::renderDataTable(resultset[1:nrow(resultset),],
                                             options=list(paging=T),
                                             rownames=F,
                                             filter="top",
                                             selection="single")

          mydata=resultset[,c("Store_Nbr","Store_Name","Old_Nbr","UPC","Dept_Nbr","Category_Nbr","Dept_Name","Category_Name","Item_Desc","Vendor_Name",
                              "Type_Code","Status_Code","Ordbk_Flag","Effective_Date","Expire_Date","Vnpk_Qty","Whpk_Qty","MBM","Carry_Option","Carried_Status",
                              "Channel_Mthd_Desc","Cedis","CWO","OH","Oh_Cedis","IT","IW","OO","Ttl_Cad1","Fr_Cedis","Fr_Item","Last_Receive","Last_Sale",
                              "SSCov_Pzas","Presentation_Qty","Min_SS_Qty_01","Max_SS_Qty_01","Display_Qty","Smo_04","Smo_03","Smo_02","Smo_01","Smo_00",
                              "Sell_Price","Unit_Cost","AAVentas_04","AAVentas_03","AAVentas_02","AAVentas_01","AAVentas_00","Ventas_52","Ventas_51","Ventas_50",
                              "Ventas_49","Ventas_04","Ventas_03","Ventas_02","Ventas_01","Ventas_00","Fcst_X04_X04","Fcst_X03_X03","Fcst_X02_X02","Fcst_X01_X01",
                              "Fcst_Ttl_Curr","Fcst_Ttl_01","Fcst_Ttl_02","Fcst_Ttl_03","Fcst_Ttl_04","Ship_04","Ship_03","Ship_02","Ship_01","Arrive_00","Arrive_01",
                              "Arrive_02","Arrive_03","Arrive_04","HOHWLY_4","HOHWLY_3","HOHWLY_2","HOHWLY_1","HOHW_52","HOHW_51","HOHW_50","HOHW_49","HOHW_48","HOHW_4",
                              "HOHW_3","HOHW_2","HOHW_1","ITOrd_4","ITRec_4","ITOrd_3","ITRec_3","ITOrd_2","ITRec_2","ITOrd_1","ITRec_1","STOrd_4","STRec_4","STOrd_3",
                              "STRec_3","STOrd_2","STRec_2","STOrd_1","STRec_1","Vta$_04","Ventas_04","Vta$_03","Ventas_03","Vta$_02","Ventas_02","Vta$_01","Ventas_01",
                              "AAVta$_04","AAVentas_04","AAVta$_03","AAVentas_03","AAVta$_02","AAVentas_02","AAVta$_01","AAVentas_01")]



          output$tableDT1=DT::renderDataTable(mydata[1:nrow(mydata),],
                                              options=list(paging=T),
                                              rownames=F,
                                              filter="top",
                                              selection="single")

        })
      })

      origTable_selected <- reactive({
        ids <- input$tableDT1_rows_selected
        mydata[ids,1]
      })


      output$Store_Nbr=renderText({origTable_selected()})



    }
    shinyApp(ui=ui,server=server)

thanks a lot

1 Like

Thanks for sharing your code, but it would be much easier to help you if you constructed a small reproducible example (reprex) that contains only the part of the code that is causing your problem.

In terms of your described problem, try creating a small Shiny app using a built-in data set like mtcars and a minimal set of inputs and transformations to reproduce the problem you've encountered.

Thanks a lot, did and have found the problem now it works.

1 Like

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

solution was to declare as reactivevalues :
values <- reactiveValues(mydata = NULL,myplot=NULL)

and store data this way:
values$mydata<-resultset[,c("Store_Nbr","Store_Name","Old_Nbr","UPC")]

thanks a lot

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.