2016-12-29 25 views

回答

4

這可以通過使用renderUI()來處理,它檢查它得到的輸出類型並呈現相應的輸出。

在UI部分中,您可以將uiOutput放置在想要顯示圖或打印的位置。

uiOutput("Plotorprint") 

,然後在服務器中,你會定義uiOutput像這樣的東西:

output$Plotorprint <- renderUI({ 
    if (is.data.frame(f(x))) { # Check if output of f(x) is data.frame 
     verbatinTextOutput("ISPLMatchPrint") # If so, create a print 
    } else {      # If not, 
     plotOutput("ISPLMatchPlot") # create a plot 
    } 
    }) 

而你也讓你在你的問題在你的服務器發佈,以及定義。

然後這應該檢查輸出f(x)正在獲取,並呈現適當的輸出。

+0

@Marjin - 該解決方案看起來正確的,但我遇到的問題。我加入了ui。 R - uiOutput('Plotorprint')&在server.R中用於僅打印文本的以下行==>輸出$ Plotorprint < - renderUI({ a <-f(x) verbatimTextOutput(a)})。這將打印一個空框 –

+0

我會看看明天這個輸出 –

+0

$ IPLMatchPlot < - renderPlot({ printOrPlot(輸入,輸出,團隊,otherTeam) }) 輸出$ IPLMatchPrint < - renderPlot({ DF < -printOrPlot(輸入,輸出,團隊,其他團隊) df })output $ Plotorprint < - renderUI if {is.data.frame(scorecard < - printOrPlot(input,output,teams,otherTeam))){ verbatimTextOutput(「IPLMatchPrint」) } else { plotOutput(「IPLMatchPlot」) }該圖顯示但不是數據幀 –

0

基於@ Marjin的響應最後的代碼是

server.R

output$IPLMatchPlot <- renderPlot({   
    f(x,y,z) 


}) 
output$IPLMatchPrint <- renderPrint({   
    df <- f(x,y,z) 
    df 
}) 

output$plotOrPrint <- renderUI({ 
    if(is.data.frame(scorecard <- printOrPlot(input, output,teams, otherTeam))){ 
     verbatimTextOutput("IPLMatchPrint") 
    } 
    else{ 
     plotOutput("IPLMatchPlot") 
    } 
}) 

ui.R

uiOutput("plotOrPrint"), 
相關問題