简体   繁体   English

如何在 R Shiny 中创建反应式数据搜索?

[英]How to create a reactive data searching in R shiny?

I want to present a data based on user input.我想根据用户输入呈现数据。 But the problem I am facing is, if the user changes his choice my variable is not changing based on which I am searching data.但我面临的问题是,如果用户改变他的选择,我的变量不会根据我搜索数据的方式改变。 For example if name is changed from a to b, the age is not changing.例如,如果名称从 a 更改为 b,则年龄不会更改。 Could anyone give any solution?任何人都可以提供任何解决方案吗? Thanks.谢谢。

library(shiny) 
library(DBI)
library(RSQLite)

con <- dbConnect (SQLite(),"/user/xyz.db")

ui <- fluidPage(

selectInput("name","What is your name? ", choice = c("a","b","c"))
paste("Age is:"), textOutput("ageout")


server <- shinyServer (function(input, output, session) {

observeEvent(input$name, {
nameSearch <- input$name

})

sqlStatement <- sprintf("SELECT DISTINCT Field2 FROM Tastetable WHERE Field1 = '%s'", nameSearch)

output$ageout <- renderText ({

paste(dbGetQuery(con, sqlStatement))

)}

shinyApp (ui = ui, server = server) 

Your code has a lot of room for improvement.您的代码有很大的改进空间。 Issues:问题:

  • ui was not closed by a bracket ui没有被括号封闭
  • you should only use ui functions in the ui , not a raw paste您应该只在ui使用 ui 函数,而不是原始paste
  • the server function didn't contain all function you want to use server功能未包含您要使用的所有功能
  • sqlStatement <- sprintf("SELECT DISTINCT Field2 FROM Tastetable WHERE Field1 = '%s'", nameSearch) is not in a reactive context sqlStatement <- sprintf("SELECT DISTINCT Field2 FROM Tastetable WHERE Field1 = '%s'", nameSearch)不在响应式上下文中

Please have a look at an introduction to shiny .请看一下闪亮介绍

library(shiny) 
library(DBI)
library(RSQLite)

con <- dbConnect (SQLite(),"/user/xyz.db")

ui <- fluidPage(
  
  selectInput("name","What is your name? ", choice = c("a","b","c")),
  span("Age is:"),
  textOutput("ageout")
)


server <- shinyServer (function(input, output, session) {
  
  sqlStatement <- eventReactive(input$name, {
    nameSearch <- input$name
    sprintf("SELECT DISTINCT Field2 FROM Tastetable WHERE Field1 = '%s'", nameSearch)
  })
  
  
  output$ageout <- renderText ({
    
    dbGetQuery(con, sqlStatement())
    
  )}
  
}

shinyApp (ui = ui, server = server) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM