2016-08-25 12 views
0
で新しい変数を作成

ui.Rコンディショニングとシャイニー

library(shiny) 

shinyUI(fluidPage(
    checkboxGroupInput("Check1",label=h4 ("Coin Type:"), choices = c("Fair","Unfair (p = 60% for heads)")) 
)) 

server.R

library(shiny) 

shinyServer(function(input, output) { 
    if (input$Check1 == "Fair"){ 
    x <- 1 
    output$Value <- renderTable(as.data.frame(x)) 
    } else { 
    x <- 5 
    output$Value <- renderTable(as.data.frame(x)) 
    } 
}) 

これは非常に単純でなければなりません:2つのチェックがあるはずですボックス。 "Fair"がチェックされている場合は、xに値1を与えます。それ以外の場合は、5.データフレームに表示します。

問題は、変数xを割り当てていると思います。

Warning: Error in .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.) 
Stack trace (innermost first): 
    44: .getReactiveEnvironment()$currentContext 
    43: .subset2(x, "impl")$get 
    42: $.reactivevalues 
    41: $ [-- this has a folder directory --] 
    40: server [-- this has a folder directory --] 
    1: runApp 
Error in .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.) 

[私は上記4140を省略していることに注意してください]:ここで私は取得エラーです。

どのようにこの問題に対処しますか?検索で何も表示されず、reactiveValuesの使い方が分かりません。

答えて

1

あなたはそうのようにやりたいxは反応性にする必要があります。

x <- reactive({ 
    if (input$Check1 == "Fair"){ 
    1 
} else { 
    5 
} 
# pass reactive x() to renderTable 
output$Value <- renderTable(as.data.frame(x()))