2016-01-15 7 views
5

光沢のある反応式で計算された変数の値に出力が依存する光沢のあるアプリを書いています。代わりに、私は次のような単純なアプリで私の問題を再現していると信じて、実際のアプリ再現:光沢のあるif文の反応式を使用

ui.R file: 

    library(shiny) 

    shinyUI(pageWithSidebar(

    headerPanel("Illustrating problem"), 

    sidebarPanel(

    numericInput('id1', 'Numeric input, labeled id1', 0, min = 0, max = 100, step  =5) 

), 

    mainPanel(

    h4('You entered'), 

    verbatimTextOutput("oid1"), 

    verbatimTextOutput("odic") 

) 
)) 

server.R file 


library(shiny) 

shinyServer(

    function(input, output) { 

    output$oid1 <- renderPrint({input$id1}) 

    x<-reactive({5*input$id1}) 

    if (x()>50) output$oid2<-renderPrint({"You entered a number greater than 10"}) 

    else output$oid2<-renderPrint({"You entered a number less than or equal to 
10"}) 

    } 
) 

を、私はこのようにそれを実行すると、私はエラーを取得する:.getReactiveEnvironment()$currentContext()エラー: `

Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

私はif文を変更する場合:if (x>50) ...その後、私はエラーを取得する:

Error in x > 50 : comparison (6) is possible only for atomic and list types

私はif文を変更する場合:if (reactive({(x>50)})) ...その後、私は、エラーメッセージが表示されます。

Error in if (reactive({ : argument is not interpretable as logical

私は非常に光沢のある作品はイベントドリブンモデルに従ってベースという任意のヘルプ

答えて

3

注意をいただければと思います - グラフィカルに指向されたすべてのUI(今日では大多数)も同様です。それは新しいコンセプトではなく、少なくとも80年代以来のことでしたが、プログラミングには単一のフローチャートに従うよりも複雑です。これは慣れています。

とにかく、シャイニーreactiveoutputobserveのステートメントがトップレベルにある必要がありますが、これに例外はないと思います。

library(shiny) 

u <- shinyUI(pageWithSidebar(

    headerPanel("Illustrating problem"), 

    sidebarPanel(
    numericInput('id1', 'Numeric input, labeled id1', 0, min = 0, max = 100, step =5) 
), 

    mainPanel(

    h4('You entered'), 
    verbatimTextOutput("oid1"), 
    verbatimTextOutput("oid2") 

) 
)) 

s <- shinyServer(function(input, output) { 

    output$oid1 <- renderPrint({input$id1}) 

    x<-reactive({5*input$id1}) 

    output$oid2<-renderPrint({ 
     if (x()>50){ 
     "You entered a number greater than 10" 
     } else { 
     "You entered a number less than or equal to 10" 
     } 
    } 
) 
} 
) 
shinyApp(ui = u, server = s) 

収量:

enter image description here

+0

(私は役に立たないUI要素の束を取り出し)しかし、あなたは確かに出力文をオブザーバーの中に置くことができます/反応的です。しかし、しないでください。 –

+0

あなたの説明をありがとう。私の問題は、私の実際のアプリケーションでは、if else文の中にたくさんのコマンドがあり、単なるrenderPrintコマンドではないということです。どのようにそれを行う上の任意のアイデアですか? – MSR

+0

上記の編集を参照してください。 –

8

夫婦の問題

は、おそらくこのような何かをしたいです。第一の大きな問題は、反応性がどのように働くかを理解していないように見えることです。エラーは「アクティブなリアクティブ・コンテキストなしでは操作が許可されていません」と言われています。それは問題です。サーバー・ファンクションの本体内にあるx()にアクセスしていますが、任意のrender*関数は、例えば、反応的なコンテキストです。したがって、これを解決するには、renderPrintのif文を単純に移動する必要があります。

その他の小さな問題は、出力IDが一致しないことです(verbatimTextOutput("odic")output$oid2)。

あなたが反応性を理解していない場合、私は30分以上それをよりよく理解することを強く勧めます。 This tutorialには反応に役立つセクションがあります。また、RStudioによる公式の光り輝くチュートリアルを再読み込みすることもできます。

はここにあなたの固定されたアプリケーションのためのコードだ、ところで私は、それが奨励さはないと思う...あなたはスクリーンショットの努力のために、あまりにもポイントを得る

library(shiny) 
ui <- fluidPage(
    numericInput('id1', 'Numeric input, labeled id1', 0, min = 0, max = 100, step=5), 
    verbatimTextOutput("oid1"), 
    verbatimTextOutput("oid2") 

) 
server <- function(input, output, session) { 
    output$oid1 <- renderPrint({input$id1}) 

    x<-reactive({5*input$id1}) 

    output$oid2<-renderPrint({ 
    if (x()>50) { 
     "You entered a number greater than 10" 
    } else { 
     "You entered a number less than or equal to 10" 
    } 
    }) 
} 
shinyApp(ui = ui, server = server) 
+0

ああ、誰もこれを見ていないと思った。とにかくあなたのためのポイント:) –

関連する問題