2017-03-01 61 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