I want to let the user interface change interactively from a textarea into a selectInput and back again.
This is what i put in server.r. I use counter1 and counter2 to let shinyapp know which dynamic ui to choose. When hitting the submitbutton there will be added either 1 to counter1 or counter2. That way counter1 and counter2 will be alternately the same or not.
library(shiny)
counter1 <- 1
counter2 <- 0
shinyServer(function(input, output) {
output$MainAction <- renderUI( {
dynamicUi()
})
dynamicUi <- reactive({
if (counter11 == counter2){
counter1 <- counter1 + 1
return(
selectInput("choose","Choose yes or no", choices = c("yes"="yes","no"="no"))
)
}
else {
counter2 <- counter2 + 1
return(
tags$textarea(id="textfield", rows=8, cols=90, "put your text here")
)
}
})
})
And this is what I put in ui.r
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("My shiny app"),
sidebarPanel(
uiOutput("MainAction"),
submitButton("action")
),
mainPanel(
tabsetPanel(
tabPanel("Output", uiOutput("outputaction"))
)
)
))
The result is that the shinyapp sticks with the textarea. Obviously that's not what I wanted. Does anyone have an idea what's going wrong here? I guess i missed something.
Many thanks in advance!
Instead of submitButton, use actionButton("counter")
and check if input$counter
is even or odd in dynamicUi
.
The reason your code did not work was because reactive expressions like dynamicUi
only execute when a reactive value (or another reactive expression, or some other reactive thing like invalidateLater
) that they read causes reactivity to be triggered. In this case dynamicUI
does not read any reactive value so it won't execute more than once. But if you use actionButton("counter")
and read input$counter
then whenever input$counter
changes the reactive expression will re-execute.
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.