I've tried different ways to store a variable in server.R and use the same variable in ui.R without much success.(except declaring it global) Is there a way to pass a variable to ui.R from server.R? It could be a hacky way as long as it works.
Asked
Active
Viewed 9,612 times
8
-
what would you like to do with it in ui.r? Did you try renderUI() to create inputs based on variables in server.R and send them to ui.r? – Tonio Liebrand Jan 26 '17 at 18:26
-
I want to pass on a string or an integer for a conditionalPanel, so that I can decide if the panel should be displayed or not.(Regardless of clicking a button) – tonybrown Jan 26 '17 at 19:07
-
2ah ok. You can use something like this on server side : output$serverInfo<- reactive({ TRUE }) ; And on ui side: conditionalPanel(condition="output.serverInfo!=0") – Tonio Liebrand Jan 26 '17 at 19:13
1 Answers
12
This might be useful to you (taken directly from my Shiny tips 'n tricks list):
Use a variable from the server in a UI conditionalPanel()
When using a conditional panel in the UI, the condition is usually an expression that uses an input value. But what happens when you want to use a conditional panel with a more complex condition that is not necessarily directly related to an input field? This example shows how to define an output variable in the server code that you can use in the UI. An alternative approach is to use the show() and hide() functions from the shinyjs
package.
library(shiny)
ui <- fluidPage(
selectInput("num", "Choose a number", 1:10),
conditionalPanel(
condition = "output.square",
"That's a perfect square!"
)
)
server <- function(input, output, session) {
output$square <- reactive({
sqrt(as.numeric(input$num)) %% 1 == 0
})
outputOptions(output, 'square', suspendWhenHidden = FALSE)
}
shinyApp(ui = ui, server = server)

DeanAttali
- 25,268
- 10
- 92
- 118
-
Awesome! One question, does suspendWhenHidden = FALSE ensures that it's run even if the output is not rendered in the UI? – tonybrown Jan 31 '17 at 18:26
-
@DeanAttali: do you have an idea to call dynamically data within ui.R. https://stackoverflow.com/questions/57093296/pass-the-read-data-in-server-r-into-ui-r-using-shiny – maniA Jul 18 '19 at 13:33
-
@tonybrown Yes.. "suspendWhenHidden. When TRUE (the default), the output object will be suspended (not execute) when it is hidden on the web page. When FALSE, the output object will not suspend when hidden, and if it was already hidden and suspended, then it will resume immediately" [https://rdrr.io/cran/shiny/man/outputOptions.html] – Rafs Mar 24 '21 at 16:00