2016-11-28 2 views
0

두 데이터 세트로로드하는 eventReactive 함수가 있습니다. 각 데이터 세트를 선택할 수 있지만 모두 동일한 eventReactive에로드됩니다. 그러나 사용자가 두 데이터 집합 중 하나만 변경하면 둘 모두를로드하지 않고 변경 한 데이터 집합 만로드하려고합니다. 나는 이것이 두 개의 액션 버튼으로 훨씬 더 쉬울 것이라는 것을 안다.하지만 나는 하나의 버튼을 가질 필요가있다. 아래 예제 코드를 찾으십시오.반짝이는 앱에서 불필요한로드를 피하십시오

예에서 변수의 변경된 코드 부분 만 다시 실행하고 싶습니다. ui.r :

library(shiny) 

# Define UI for dataset viewer application 
shinyUI(fluidPage(

    # Application title 
    titlePanel("Reactivity"), 

    # Sidebar with controls to provide a caption, select a dataset, 
    # and specify the number of observations to view. Note that 
    # changes made to the caption in the textInput control are 
    # updated in the output area immediately as you type 
    sidebarLayout(
    sidebarPanel(
     textInput("caption", "Caption:", "Data Summary"), 
     actionButton('Go','Go'), 

     selectInput("dataset", "Choose a dataset:", 
        choices = c("rock", "pressure", "cars")), 
     selectInput("dataset2", "Choose a dataset:", 
        choices = c("rock", "pressure", "cars")), 

     numericInput("obs", "Number of observations to view:", 10) 
    ), 


    # Show the caption, a summary of the dataset and an HTML 
    # table with the requested number of observations 
    mainPanel(
     h3(textOutput("caption", container = span)), 
     verbatimTextOutput('one'), 
     verbatimTextOutput('two'), 
     verbatimTextOutput("summary"), 

     verbatimTextOutput("summary2")#, 

     # tableOutput("view"), 
     # tableOutput("view2") 
    ) 
) 
)) 

server.r

library(shiny) 
library(datasets) 

# Define server logic required to summarize and view the selected 
# dataset 
shinyServer(function(input, output) { 

    # By declaring datasetInput as a reactive expression we ensure 
    # that: 
    # 
    # 1) It is only called when the inputs it depends on changes 
    # 2) The computation and result are shared by all the callers 
    # (it only executes a single time) 
    # 
    datasetInput <- eventReactive(input$Go,{ 

    ##Only run if input$dataset has changed## 
    withProgress(message='Loading 1:This should take very long....',value=0,{ 
     for(i in 1:5){ 
     Sys.sleep(1) 
     incProgress(i/5) 
     } 
    x<-switch(input$dataset, 
      "rock" = rock, 
      "pressure" = pressure, 
      "cars" = cars) 

    }) 
###only run if input$dataset2 has changed 
    withProgress(message='Loading 2:This should take even longer....',value=0,{ 
     for(i in 1:5){ 
     Sys.sleep(1.5) 
     incProgress(i/5) 
     }  
     y<-switch(input$dataset2, 
         "rock" = rock, 
         "pressure" = pressure, 
         "cars" = cars) 
    }) 




    return(list(x=x,y=y,input1=input$dataset,input2=input$dataset2)) 
    }) 

    # The output$caption is computed based on a reactive expression 
    # that returns input$caption. When the user changes the 
    # "caption" field: 
    # 
    # 1) This function is automatically called to recompute the 
    #  output 
    # 2) The new caption is pushed back to the browser for 
    #  re-display 
    # 
    # Note that because the data-oriented reactive expressions 
    # below don't depend on input$caption, those expressions are 
    # NOT called when input$caption changes. 
    output$caption <- renderText({ 
    input$caption 
    }) 

    # The output$summary depends on the datasetInput reactive 
    # expression, so will be re-executed whenever datasetInput is 
    # invalidated 
    # (i.e. whenever the input$dataset changes) 
    output$summary <- renderPrint({ 
    dataset <- datasetInput()$x 
    summary(dataset) 
    }) 

    output$summary2 <- renderPrint({ 
    dataset <- datasetInput()$y 
    summary(dataset) 
    }) 

    # The output$view depends on both the databaseInput reactive 
    # expression and input$obs, so will be re-executed whenever 
    # input$dataset or input$obs is changed. 
    output$view <- renderTable({ 
    head(datasetInput()$x, n = input$obs) 
    }) 
    output$view2 <- renderTable({ 
    head(datasetInput()$y, n = input$obs) 
    }) 
    output$one<-renderText(datasetInput()$input1) 
    output$two<-renderText(datasetInput()$input2) 

}) 

이미이 같은 시도 : 아마 당신이 반응 함수 내 값을 사용하고 있기 때문에, if(datasetInput()$input1 != input$dataset){execute...}이 그러나이 작동하지 않습니다 그 동일한 기능에서 생성됩니다 ...

답변

2

두 데이터로드 단계를 eventReactive에 종속되어있는 reactive 식으로 구분할 수 있습니다. 그런 식으로 Go를 클릭하면 입력 값이 변경된 경우 두 개의 반응식이 호출되지만 입력 값이 업데이트되지 않은 경우 반응식이 호출되지 않습니다. 예 :

library(shiny) 
library(datasets) 

ui = fluidPage(
    # Application title 
    titlePanel("Reactivity"), 
    sidebarLayout(
    sidebarPanel(
     textInput("caption", "Caption:", "Data Summary"), 
     actionButton('Go','Go'), 
     selectInput("dataset", "Choose a dataset:", 
        choices = c("rock", "pressure", "cars")), 
     selectInput("dataset2", "Choose a dataset:", 
        choices = c("rock", "pressure", "cars")), 

     numericInput("obs", "Number of observations to view:", 10) 
    ), 

    mainPanel(
     h3(textOutput("caption", container = span)), 
     verbatimTextOutput('one'), 
     verbatimTextOutput('two'), 
     verbatimTextOutput("summary"), 
     verbatimTextOutput("summary2") 
    ) 
) 
) 

server = function(input, output) { 
    dat1 = reactive({ 
    withProgress(message='Loading 1:This should take very long....',value=0,{ 
     for(i in 1:5){ 
     Sys.sleep(1) 
     incProgress(i/5) 
     } 
     switch(input$dataset, 
      "rock" = rock, 
      "pressure" = pressure, 
      "cars" = cars) 
    }) 
    }) 

    dat2 = reactive({ 
    withProgress(message='Loading 2:This should take even longer....',value=0,{ 
     for(i in 1:5){ 
     Sys.sleep(1.5) 
     incProgress(i/5) 
     }  
     switch(input$dataset2, 
      "rock" = rock, 
      "pressure" = pressure, 
      "cars" = cars) 
    }) 
    }) 

    datasetInput <- eventReactive(input$Go,{ 
    x = dat1() 
    y = dat2() 
    return(list(x=x,y=y,input1=input$dataset,input2=input$dataset2)) 
    }) 

    output$caption <- renderText({ 
    input$caption 
    }) 

    output$summary <- renderPrint({ 
    dataset <- datasetInput()$x 
    summary(dataset) 
    }) 

    output$summary2 <- renderPrint({ 
    dataset <- datasetInput()$y 
    summary(dataset) 
    }) 

    output$view <- renderTable({ 
    head(datasetInput()$x, n = input$obs) 
    }) 

    output$view2 <- renderTable({ 
    head(datasetInput()$y, n = input$obs) 
    }) 

    output$one<-renderText(datasetInput()$input1) 

    output$two<-renderText(datasetInput()$input2) 
} 

shiny::shinyApp(ui,server) 
+0

매우 감사합니다. –

관련 문제