2013-12-08 22 views
1

我想讓用戶界面從textarea交互式更改爲selectInput並再次返回。如何在shinyapp中以交互方式更改UI

這是我放在server.r。我使用counter1和counter2讓shinyapp知道選擇哪個動態UI。當按下提交按鈕時,會在計數器1或計數器2上加1。這樣counter1和counter2將交替相同或不相同。

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") 
) 
} 
}) 
}) 

而這就是我把ui.r

library(shiny) 

shinyUI(pageWithSidebar(


headerPanel("My shiny app"), 

sidebarPanel(

uiOutput("MainAction"), 
submitButton("action")  

), 


    mainPanel(
    tabsetPanel(
     tabPanel("Output", uiOutput("outputaction")) 

    ) 
) 
)) 

的結果是shinyapp枝與textarea的。顯然這不是我想要的。有沒有人知道這裏有什麼問題?我想我錯過了一些東西。

非常感謝提前!

回答

3

代替submitButton,使用actionButton("counter")並檢查input$counterdynamicUi中是偶數還是奇數。

您的代碼無法工作的原因是因爲如dynamicUi這樣的反應式表達式僅在他們讀取的反應性值(或其他反應性表達式或其他反應性事件如invalidateLater)導致反應性被觸發時纔會執行。在這種情況下,dynamicUI不會讀取任何反應值,因此它不會執行多次。但是,如果您使用actionButton("counter")並閱讀input$counter,則每當input$counter發生變化時,反應表達式將重新執行。

+0

謝謝喬!我現在開始工作了。我想我太專注於爲什麼我的'解決方案'不起作用。 – rdatasculptor

相關問題