光沢のあるアプリで不要な負荷を回避する

Tim_Utrecht

私が持ってeventReactiveいる2つのデータセット内の負荷機能を。各データセットを選択できますが、どちらも同じ内に読み込まれますeventReactiveただし、ユーザーが2つのデータセットの一方のみを変更する場合は、両方をロードするのではなく、変更されたデータセットのみをロードする必要があります。これは2つのアクションボタンを使用するとはるかに簡単になることはわかっていますが、1つにする必要があります。以下のサンプルコードを見つけてください。

したがって、この例では、変数が変更されたコードのその部分のみを再度実行したいと思います。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...}しかし、おそらく同じ関数で作成されたリアクティブ関数内の値を使用しているため、それは機能しません...

jamieRowen

2つのデータ読み込みステップを、依存関係のreactiveある式に分けることができますeventReactiveそうすれば、[実行]をクリックすると、入力値が変更されている場合は2つのリアクティブ式が呼び出されますが、入力が更新されていない場合は呼び出されません。例えば:

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)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Rの光沢のあるアプリでelseを使用する

分類Dev

光沢のあるアプリで親URLを取得する方法

分類Dev

Windowsで光沢のあるアプリをホストする

分類Dev

光沢のあるアプリでリアクティブなデータフレームを作成する

分類Dev

光沢のあるアプリでリアクティブな表現を削除する

分類Dev

光沢のあるアプリでリアクティブな表現を削除する

分類Dev

光沢のある: 光沢のあるアプリにリアクティブ バーを追加する方法

分類Dev

光沢のあるアプリでfileInputをリセットする

分類Dev

光沢のあるアプリで選択リストを動的に更新する

分類Dev

大きな光沢のあるアプリを整理する方法は?

分類Dev

光沢のあるアプリでモーダルでdataTableOutputを表示する

分類Dev

Rの光沢のあるアプリで星を評価する

分類Dev

光沢のあるアプリでbsModalの背景を変更する

分類Dev

光沢のあるアプリでggvisを使用して散布図を作成する

分類Dev

光沢のあるアプリでHTMLファイルを表示する

分類Dev

光沢のあるアプリでMutationObserveを正しく使用する方法

分類Dev

光沢のあるアプリでMutationObserveを正しく使用する方法

分類Dev

光沢のあるアプリでプロット警告を抑制します

分類Dev

光沢のあるアプリの入力値を `actionButton`で更新します

分類Dev

光沢のあるアプリ内でデータと入力を渡す

分類Dev

ANOVA光沢のあるアプリ

分類Dev

光沢のあるアプリの外部でリアクティブ関数を作成する

分類Dev

光沢のあるアプリでイメージマップのクリックを処理する

分類Dev

光沢のあるアプリでイメージマップのクリックを処理する

分類Dev

光沢のあるアプリ内でanovaが機能しないのはなぜですか

分類Dev

光沢のあるアプリで複数のプロットを並べて

分類Dev

光沢のあるアプリの起動時に通知を表示する

分類Dev

光沢のあるアプリで閉じるときにモーダルをリセットする

分類Dev

光沢のあるアプリでHTMLプロットをレンダリングする方法

Related 関連記事

  1. 1

    Rの光沢のあるアプリでelseを使用する

  2. 2

    光沢のあるアプリで親URLを取得する方法

  3. 3

    Windowsで光沢のあるアプリをホストする

  4. 4

    光沢のあるアプリでリアクティブなデータフレームを作成する

  5. 5

    光沢のあるアプリでリアクティブな表現を削除する

  6. 6

    光沢のあるアプリでリアクティブな表現を削除する

  7. 7

    光沢のある: 光沢のあるアプリにリアクティブ バーを追加する方法

  8. 8

    光沢のあるアプリでfileInputをリセットする

  9. 9

    光沢のあるアプリで選択リストを動的に更新する

  10. 10

    大きな光沢のあるアプリを整理する方法は?

  11. 11

    光沢のあるアプリでモーダルでdataTableOutputを表示する

  12. 12

    Rの光沢のあるアプリで星を評価する

  13. 13

    光沢のあるアプリでbsModalの背景を変更する

  14. 14

    光沢のあるアプリでggvisを使用して散布図を作成する

  15. 15

    光沢のあるアプリでHTMLファイルを表示する

  16. 16

    光沢のあるアプリでMutationObserveを正しく使用する方法

  17. 17

    光沢のあるアプリでMutationObserveを正しく使用する方法

  18. 18

    光沢のあるアプリでプロット警告を抑制します

  19. 19

    光沢のあるアプリの入力値を `actionButton`で更新します

  20. 20

    光沢のあるアプリ内でデータと入力を渡す

  21. 21

    ANOVA光沢のあるアプリ

  22. 22

    光沢のあるアプリの外部でリアクティブ関数を作成する

  23. 23

    光沢のあるアプリでイメージマップのクリックを処理する

  24. 24

    光沢のあるアプリでイメージマップのクリックを処理する

  25. 25

    光沢のあるアプリ内でanovaが機能しないのはなぜですか

  26. 26

    光沢のあるアプリで複数のプロットを並べて

  27. 27

    光沢のあるアプリの起動時に通知を表示する

  28. 28

    光沢のあるアプリで閉じるときにモーダルをリセットする

  29. 29

    光沢のあるアプリでHTMLプロットをレンダリングする方法

ホットタグ

アーカイブ