2017-12-13 1 views
1

2 개의 탭이있는 앱이 있으며 각각 액션 버튼이 있습니다. 두 단추 중 하나를 누르면 동일한 프로세스가 실행되기를 원합니다. 그래서 나는 그에게 다음과 같은 방법으로 썼다 :2 개의 탭에 2 개의 액션 버튼이있는 eventReactive

library(shiny) 

ui <- fluidPage(
    tabsetPanel(
    tabPanel("tab1", 
     actionButton("btn1","Button 1"), 
     textOutput("text") 
    ), 
    tabPanel("tab2", 
     actionButton("btn2","Button 2") 
    ) 
) 

) 

server <- function(input, output) { 
    data=eventReactive(c(input$btn1,input$btn2),{ 
    print("Button is pressed!") 
    print(input$btn1) 
    print(input$btn2) 
    iris 
    }) 
    output$text=renderText({ 
    req(data()) 
    print(head(data())) 
    "Really? Button is pressed!" 
    }) 
} 

# Run the application 
shinyApp(ui = ui, server = server) 

을 그리고 eventReactive 표현이 트리거 왜 응용 프로그램은 초기화 상태에서 버튼을 누르면 전에 나는 이해가 안 돼요.

버튼 1 개 (예 : c(input$btn1, input$btn2)) 대신 input$btn1 인 경우 두 가지 종속성이있는 것은 아닙니다.

누군가 나를 밝힐 수 있습니까?

답변

0

내 생각에 식은 자동으로 TRUE로 평가되므로로드시 실행됩니다. 오히려 {}to listen from two buttons을 사용해야합니다.

수정 된 코드 :

library(shiny) 

ui <- fluidPage(
    tabsetPanel(
    tabPanel("tab1", 
      actionButton("btn1","Button 1"), 
      textOutput("text") 
    ), 
    tabPanel("tab2", 
      actionButton("btn2","Button 2") 
    ) 
) 

) 

server <- function(input, output) { 
    data=eventReactive({ 
    input$btn2 
    input$btn1},{ 
    print("Button is pressed!") 
    print(input$btn1) 
    print(input$btn2) 
    iris 
    }) 
    output$text=renderText({ 
    req(data()) 
    print(head(data())) 
    "Really? Button is pressed!" 
    }) 
} 

# Run the application 
shinyApp(ui = ui, server = server) 
+0

이 솔루션이 효과가 있다고 생각하지 않습니다. 방아쇠를 'AND'상태로 만듭니다. 즉, 어떤 일이 일어나기 전에 두 버튼을 모두 눌러야합니다. 물론 이것이 왜 그런지 이해할 수 없습니다. – qoheleth

1

eventReactiveFalse 기본값은 help(eventReactive)를 참조하십시오 인수 ignoreInit을 가지고 있습니다. 난이 도움이되기를 바랍니다

data=eventReactive(c(input$btn1,input$btn2),{ 

data=eventReactive(c(input$btn1,input$btn2),ignoreInit = T,{ 

에 :

문제는 내 다음 줄을 수정 해결!

+0

솔루션이 실제로 작동합니다. 그러나 나는 아직도 그 행동을 이해하지 못합니다. 1. doc은 "ignoreNULL에 대해 일어나는 것과는 달리,'observeEvent'만이'ignoreInit' 인수를 취합니다." 음, 분명히 eventReactive도 그 주장을 취합니다. 2.이 솔루션은 트리거 ('eventExpr')보다는'valueExpr'을 억제하고있는 것처럼 보입니다. 3. 왜 하나의 버튼이 작동합니까? – qoheleth

+0

나는 그것이 문서화의 실수라고 생각한다. 나는이 문제를 [Github] (https://github.com/rstudio/shiny/issues/1907)에서 만들었고 우리가 옳은지 보자.) – Florian

관련 문제