How to read a TextInput in ui.R, process a query with this value in global.R and show in server.R using Shiny

☆樱花仙子☆ 提交于 2019-12-11 04:31:41

问题


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.


回答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.



来源:https://stackoverflow.com/questions/44655720/how-to-read-a-textinput-in-ui-r-process-a-query-with-this-value-in-global-r-and

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!