且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 ui.R 中读取 TextInput,在 global.R 中使用此值处理查询并使用 Shiny 在 server.R 中显示

更新时间:2023-02-18 10:51:00

文件 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.