1

I have an application in Shiny R. On ui.R I read a Textinput, On global.R I process a query using sqldf().

How can I read a Textinput of ui.R in Global.R ?

ui.R

shinyUI(fluidPage(

  #books <<- list("Descritores FOUSP" = "descritor"),

  # Application title
  titlePanel("CRAI"),

  headerPanel(title="Pesquisa de Descritores"),
  sidebarLayout(
    sidebarPanel(

      h5('Qual é o tema da sua pesquisa ?'),
      textInput("descritor", "Digite um descritor",""),
      submitButton('Pesquisar')
)
)

This textInput with name "descritor", I want to use in query on global.R

I tried this:

output$desc <- renderText({

    paste(input$descritor)})

 sql <- sprintf("SELECT * FROM csv WHERE Assuntos = '%s'", output$desc)

But I can´t to read "descritor" on global.R.

Robson Brandão
  • 186
  • 2
  • 9
  • 1
    There are no `input/output` objects defined in global. You need the server function for that. You can pass in parameters from your input to your global function but you can't read directly from `input`. It would be better to have a more complete [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that shows your server code as well. – MrFlick Jun 20 '17 at 14:29

1 Answers1

1

The file global.R is not part of the reactive section of a Shiny app. Only components defined inside a user interface and a server function can work as reactive expressions. Everything in global.R is carried out once at the launch of the app, but then not any more.

So if you want to have your sql statement carried out, you need the following inside your server:

sql <- reactive({
  sprintf("SELECT * FROM csv WHERE Assuntos = '%s'", output$desc)
})

mydata <- reactive({
  someSQLfunction(sql())
})

By making your SQL statement a reactive expression (function reactive() does that), it will get updated at every update of output$desc. This reactive expression behaves like a function(!). So the call sql() in the next statement will return the updated SQL statement. You can then use a function of your choice (I call it someSQLfunction) to process the statement.

You want to make the result of this statement also a reactive expression, so you can use it to create the relevant output of your app.

Joris Meys
  • 106,551
  • 31
  • 221
  • 263
  • Can you help me to organized my code if I submit for you ? – Robson Brandão Jun 20 '17 at 17:19
  • 1
    @RobsonBrandão I'm sorry, but I do have more than enough work as it is. To get a better understanding of how to organize your code in a reactive context, go through the articles about "Reactive Programming" here: http://shiny.rstudio.com/articles/ Organizing code goes best when you understand the underlying principles. – Joris Meys Jun 21 '17 at 09:00