如何在 ui.R 中读取 TextInput,在 global.R 中使用此值处理查询并使用 Shiny 在 server.R 中显示 [英] How to read a TextInput in ui.R, process a query with this value in global.R and show in server.R using Shiny
问题描述
我在 Shiny R 中有一个应用程序.在 ui.R 上我读取了一个 Textinput,在 global.R 上我使用 sqldf().
I have an application in Shiny R. On ui.R I read a Textinput, On global.R I process a query using sqldf().
如何在 Global.R 中读取 ui.R 的 Textinput ?
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')
)
)
这个名为descritor"的 textInput,我想在 global.R 上的查询中使用
This textInput with name "descritor", I want to use in query on global.R
我试过了:
output$desc <- renderText({
paste(input$descritor)})
sql <- sprintf("SELECT * FROM csv WHERE Assuntos = '%s'", output$desc)
但我无法读取 global.R 上的描述符".
But I can´t to read "descritor" on global.R.
推荐答案
文件 global.R 不是 Shiny 应用程序的响应部分的一部分.只有在用户界面和服务器功能中定义的组件才能用作反应式表达式.global.R 中的所有内容都在应用启动时执行一次,然后不再执行.
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.
因此,如果您想执行 sql 语句,您的服务器中需要以下内容:
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())
})
通过使您的 SQL 语句成为反应式表达式(函数 reactive()
执行此操作),它将在每次更新 output$desc
时更新.这个反应式表达式的行为就像一个函数(!).所以下一个语句中的调用 sql()
将返回更新后的 SQL 语句.然后,您可以使用您选择的函数(我称之为 someSQLfunction
)来处理该语句.
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.
这篇关于如何在 ui.R 中读取 TextInput,在 global.R 中使用此值处理查询并使用 Shiny 在 server.R 中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!