I have ui.R
, server.R
, global.R
in shiny App.
I want to use a reactive global variable when I select dataset
and press actionButton
.
Example:
ui.R
fluidPage(
titlePanel("Using global variable"),
fluidRow(
uiOutput("ui1"),
uiOutput("ui2"),
uiOutput("ui3")
),
)
)
server.R
function(input, output) {
output$ui1 <- renderUI({
selectInput("dataset", "firstValue", choices = c("first", "second", "third")
})
output$ui2 <- renderUI({
actionButton("doIt", class="btn-primary", "change")
})
output$ui3 <- renderText({
paste(catPath)
})
}
global.R
catPath <<- paste(output$dataset, "/completed", sep="")
The result is first/completed
on ui3 renderText
, when I select first
in dataset
. and press the actionButton
.
How can I complete this process?
I agree with @JohnPaul and @Lee88, your catPath
probably belongs within server.R
. Having said that, I'll keep it here for now (assuming you have other reasons for this in your MWE).
global.R
:
catPath <- ""
I needed to set it to something in order to be reference-able later, otherwise the value used here should be meaningless (though it will be returned if no action taken).
ui.R
:
I added a "stop?" action button so that you can "exit" your app and capture the value of catPath
into the calling environment. Not required if you don't intend to intentionally exit the app.
fluidPage(
titlePanel("Using global variable"),
fluidRow(
uiOutput("ui1"),
uiOutput("ui2"),
uiOutput("ui3"),
actionButton("stopme", "Stop?")
)
)
server.R
:
I changed output$ui3
to create the HTML object (not perform calculations), and then observe two events and act on them. Again, if you don't need the "stop?" button above, you probably don't need the second observe here. (If you do use it, note that the argument to stopApp
is returned invisibly to the caller.)
function(input, output, session) {
output$ui1 <- renderUI({
selectInput("dataset", "firstValue", choices = c("first", "second", "third"))
})
output$ui2 <- renderUI({
actionButton("doIt", class="btn-primary", "change")
})
output$ui3 <- renderUI({
textInput("myinput", "catPath", "")
})
observeEvent(input$doIt, {
catPath <<- paste(input$dataset, "/completed", sep = "")
updateTextInput(session, inputId = "myinput", value = catPath)
})
observeEvent(input$stopme, { stopApp(catPath); })
}
Doing something like newCatPath <- runApp("path/to/dir")
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.