0

I have a Shiny dashboard app where the user inputs their name into a text box.

The dashboard then reads in the users dataset after retrieving the data from a connected database and assigns it to a variable that includes their user name

Example

assign(paste0(input$user_name, "_dataset"), 
       sqlQuery(connectionString, str_paste("Select * from ",user_name,"_table;"))
       )

Question: Without knowing input$user_name how can I refer to the variable when preparing plots and charts from the data?

I tried this,

plot_ly(paste0(input$user_name, "_dataset"), x = ..., y = ... etc)

but it does not work.

Andrew Scotchmer
  • 313
  • 3
  • 12
  • I'm sure there's a way to achieve what you want to do, but that depends on a lot of factors, can you please specify more what you want to plot, which libraries you're using, ..... etc. , or even better provide a reproducible example we can try with (this could help https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – DS_UNI May 11 '20 at 12:18

1 Answers1

0

As said, we would need a full reproducible example, but in a general way you should use a reactive expression.

Example:

data <- reactive({
  req(input$user_name) # loads only if an input username is available
  sqlQuery(connectionString, str_paste("Select * from ",user_name,"_table;"))
})

If the data downloading has to be triggered via a specific action - like a button -, you can instead define the reactive by :

data <- eventReactive(eventExpr = input$trigger_button,
                      { 
                        req(input$user_name) # loads only if an input username is available
                        sqlQuery(connectionString, str_paste("Select * from ",user_name,"_table;"))
                      })

Then, call this expression in your renderPlot via:

plot_ly(data(), x = ..., y = ... etc) # note the parentheses after `data`
Jrm_FRL
  • 1,394
  • 5
  • 15