4

I need to change the mydb(string) value based on input selected from sidebar.

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

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. 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?

Community
  • 1
  • 1
Anish
  • 1,920
  • 11
  • 28
  • 48
  • you have to put all your if statements into the reactive expression or observe. so put the if and else if into the renderText before the paste – Pork Chop Nov 25 '14 at 08:20
  • @pops I am supposed to use the value of mydb in dbname in con <- dbConnect(MySQL(),dbname=mydb, user="root", host="127.0.0.1", password="root")| Argument dbname must be a string or NULL – Anish Nov 25 '14 at 08:23

1 Answers1

7

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. 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):

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))
Pork Chop
  • 28,528
  • 5
  • 63
  • 77
  • Thanks, one more question if i add query like test<-query('select * from testdb_abc'), how can i retrieve test from query_output() – Anish Nov 25 '14 at 09:23
  • The entire output is going to be in the query_output(), so just make sure you put this output into some table or something. like so: output$table<- renderDataTable({query_output()}) and then add this output to ui.r like so: dataTableOutput("table") – Pork Chop Nov 25 '14 at 09:28
  • For more info, please read this book (Free to download) http://it-ebooks.info/book/3206/ – Pork Chop Nov 25 '14 at 09:31