简体   繁体   English

根据用户输入从R shiny中的反应上下文中获取价值

[英]Get value from reactive context in R shiny based on user input

I need to change the mydb(string) value based on input selected from sidebar. 我需要根据从侧边栏中选择的输入更改mydb(字符串)值。

ui.R ui.R

library(shiny)
shinyUI(fluidPage(
  titlePanel("Shiny App"),
  sidebarLayout(
    sidebarPanel( selectInput("site", 
                          label = "Choose a site for Analysis",
                          choices = c("abc", "def",
                                      "ghi", "jkl"),
                          selected = "abc")
              ),
    mainPanel(
      textOutput("text"),
     )
))

server.R server.R

library(shiny)
library(ggplot2)
library(RMySQL)

shinyServer(function(input, output) {
    if(input$site=="abc"){
      mydb<-"testdb_abc"}
   else if(input$site=="def"){
     mydb<-"testdb_def"}
      con <- dbConnect(MySQL(),dbname=mydb, user="root", host="127.0.0.1", password="root")
      query <- function(...) dbGetQuery(con, ...)  

      output$text <- renderText({
        paste("You have selected:",input$site)
      })  

})

In above server.R, I need to assign string value to mydb based on selected input. 在上面的server.R中,我需要根据选定的输入为mydb分配字符串值。 I get this error: 我收到此错误:

Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something     that can only be done from inside a reactive expression or observer.) 

How can I do that with reactive in shiny? 我怎么能用闪亮的反应来做到这一点?

as previously mentioned you would have to have your if statements in the reactive expression or observe Below is the working example of the sample App. 如前所述,您必须在反应式表达式中使用if语句或observe以下是示例App的工作示例。 Here I used a reactive expression to check which database you have selected. 在这里,我使用了一个反应式表达式来检查您选择的数据库。 you can then use the mydb() and put it into your query, like so (I think this should work): 然后你可以使用mydb()并将它放入你的查询,就像这样(我认为这应该工作):

con <- dbConnect(MySQL(),dbname=mydb(), user="root", host="127.0.0.1", password="root")
query <- function(...) dbGetQuery(con, ...) 

A sample example is below 下面是一个示例

library(shiny)
library(ggplot2)
library(RMySQL)

ui =fluidPage(
  titlePanel("Shiny App"),
    sidebarPanel(selectInput("site", 
                              label = "Choose a site for Analysis",
                              choices = c("abc", "def","ghi", "jkl"),selected = "abc")),
    mainPanel(textOutput("text"),textOutput("db_select"))
  )


server = (function(input, output) {

  mydb <- reactive({

    if(input$site == "abc")
      {
        test <- c("testdb_abc")
      }
    else if(input$site == "def")
      {
        test <- c("testdb_def")
      } 
  })

  output$text <- renderText({  
    paste("You have selected:",input$site)
  })  

  query_output <- reactive({
    con <- (dbConnect(MySQL(),dbname=mydb(), user="root", host="127.0.0.1", password="root"))
    query <- function(...) dbGetQuery(con, ...)   
  })

  output$db_select <- renderText({  
    paste("My Database is:",mydb())
  })  
})


runApp(list(ui = ui, server = server))

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

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