2017-03-01 74 views
0

首先之間切換,這是我Rshiny應用的剝離下來的骨架,用什麼,我試圖做(但沒有):在Rshiny,renderPlot和renderText

ui <- shinyUI(fluidPage(
    titlePanel("Player Self-Centered Rating"), 
    sidebarLayout(
    sidebarPanel(
     radioButtons("radio", label = h3("Chart Type"), 
        choices = list("RadarPlot" = 1, 
            "Separate Plots" = 2, 
            "Top/Bottom 5 Table" = 3), 
        selected = 1) 

    mainPanel(
     plotOutput('radarPlot', width = 50), 
     textOutput("text2") 
    ) 
) 
)) 

server <- shinyServer(function(input, output) { 
    if (input$radio %in% c(1,2)) { 
     output$radarPlot <- renderPlot({   
     ggplot(data, aes(x = reorder(names, -ft_per_min), y = ft_per_min, col = this_player, fill = this_player)) + 
     geom_bar(stat = "identity") 

     } 
    }, height = 800, width = 900) 
    } 
    else if(input$radio == 3) {  
    output$text2 <- renderText({ 
     paste("print some text") 
    }) 
    } 
}) 

此使用,如果和方法如果/其他情況確定是否調用renderPlot或renderText不起作用。我得到這個錯誤:

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.) 

上午我遙遠的什麼,我想在這裏做什麼?任何關於什麼是錯誤的想法,以及我是否需要這種反應式表達或將不會受到讚賞!

回答

1

input$..值只能在reactiverender*函數內進行評估。

你可以這樣做:

ui <- shinyUI(fluidPage(
    titlePanel("Player Self-Centered Rating"), 
    sidebarLayout(
    sidebarPanel(
     radioButtons("radio", label = h3("Chart Type"), 
        choices = list("RadarPlot" = 1, 
            "Separate Plots" = 2, 
            "Top/Bottom 5 Table" = 3), 
        selected = 1)), 
    mainPanel(
     plotOutput('radarPlot', width = 50), 
     textOutput("text2") 
    ) 
) 
)) 

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

    output$radarPlot <- renderPlot({ 
    if (input$radio %in% c(1,2)) { 
     ggplot(mtcars, aes(x = mpg, y = cyl, col = hp, fill = hp)) + 
     geom_bar(stat = "identity")} 
    }, height = 800, width = 900) 

    output$text2 <- renderText({ 
    if (input$radio == 3) { 
     paste("print some text")} 
    }) 

}) 

shinyApp(ui,server) 
+0

那麼簡單,非常感謝主席先生! – Canovice