mysql - R Shiny: Table object not found in reactive RMySQL query in shiny app -


[edit]: solution agstudy worked me.

i have shiny app allows users toggle between 3 tables in mysql database. users can select table used rendertable generate object.

i have put if statements in rendertable respond users' selected table.

when run app, table fails print , cannot viewed.

server.r

library(shiny) library(rmysql)  con <- dbconnect(rmysql::mysql(),user="x",password="x",host="dbhost",dbname="db")  shinyserver(function(input, output) {    data <- reactive({      selectinput("input$data", "choose input:", choices = c('data1'='1','data2'='2', 'data3'='3'))   })      output$table <- rendertable({       if(input$data == data1){         query1 <- reactive({ "select *                             data1                             order var1, var2"})         reactive({dbgetquery(con,query1())})         head(data())        } else if(input$data == data2){         query1 <- reactive({ "select *                             data2                             order var1, var2"})         reactive({dbgetquery(con,query1())})         head(data())        } else if(input$data == data3){         query1 <- reactive({ "select *                             data3                             order var1, var2"})         reactive({dbgetquery(con,query1())})         head(data())        } else print("select dataset")      })  }) 

ui.r

library(shiny) library('rmysql')  shinyui(navbarpage("test",                      tabpanel("test",                             sidebarlayout(                               sidebarpanel(                                 selectinput("data", label = "data set",                                             choices = c("", "data1", "data2", "data3"))                                  ),                                mainpanel(                                 tableoutput("table")                                  )                             )) )) 

your problem here not in case related mysqlserver there no ui logic. programming in shiny not easy if don't isolate code parts:

  • inputs: defined in ui interface
  • reactive functions : should change each time change input
  • outputs: refreshed once input data changed reactive function.

you miss-understand reactive.the basic idea don't need refresh ui manually, should done automatically once input parameter changed.

here rewriting code using shiny logic:

library(rmysql) library(shiny) ## public function used ## in server side connect data base ## , retrieve data get_data <-    function(query){     on.exit(dbdisconnect(conn)) ## important close connection     conn <- dbconnect("mysql",user="xuser",                       password="xpws",                       host="xhost",                       dbname="xdbname")     dbgetquery(con,query)   }  server <- shinyserver(   function(input, output) {     ## reactive engine refresh query      ## each time input changed      query <- reactive( sprintf("select *                      %s                      order var1, var2",input$data)      )     ##  displaying reactive inputs     output$table <- rendertable(       if(input$data!="") get_data(query())     )   })  ## define ui elements step ## easy indenting ui_panel <-    tabpanel("test",            sidebarlayout(              sidebarpanel(                selectinput("data", label = "data set",                            choices = c("","data1", "data2", "data3"))              ),              mainpanel(                tableoutput("table")              )            )   )   ui <- shinyui(navbarpage("test",ui_panel))  runapp(list(ui=ui,server=server)) 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -