2017-08-22 24 views
1

我想讓一個Shiny應用程序做計算,如[本文檔 有3種計算,稱爲「calcprior」,「calcpval」和「calcFPR」。帶有條件面板中的數值輸入的單選按鈕

要完成哪個計算是由radioButton選擇的。每個計算需要不同的輸入。輸入放置在conditionalPanels中。正確的名字出現在onPanel上,但數字值不會傳遞給服務器,例如輸入$ pval沒有在條件面板的numericInput中輸入的值。

相反,所有3次計算所需的nsamp值正確地傳遞到服務器。

我是Shiny的初學者,所以我希望有人能解釋出了什麼問題。不能夠看到變量的值使得調試比較正規R.

sidebarLayout(

    sidebarPanel(
    radioButtons("calctype", "Choose calculation:",selected = "calcFPR", 
    choices = c(
    "prior (for given FPR and P val)" = "calcprior", 
    "P value (for given FPR and prior)" = "calcpval",  
    "FPR (for given P value and prior)" = "calcFPR")), 

    conditionalPanel(
    condition = "input.calctype == 'calcprior'", 
    numericInput("pval", label = h5("observed P value"), value = 0.05, min 
    = 0, max = 1), 
    numericInput("FPR", label = h5("false positive risk"), value = 0.05, 
    min = 0, max = 1) 
    ), 

    conditionalPanel(
    condition = "input.calctype == 'calcpval'", 
     numericInput("FPR", label = h5("false positive risk"), value = 0.05, 
     min = 0, max = 1), 
     numericInput("prior", label = h5("prior probability of real effect"), 
    value = 0.5, min  = 0, max = 1) 
    ), 

    conditionalPanel(
    condition = "input.calctype == 'calcFPR'", 
     numericInput("pval",label = h5("observed P value"),value = 0.05, min = 
     0, max = 1), 
     numericInput("prior", label = h5("prior probability of real effect"), 
     value = 0.5, min = 0., max = 1.) 
    ), 

    inputPanel(
     numericInput("nsamp",label = h5("Number in each sample"), step = 1, 
     value = 16, min = 2) 
    ), 

    mainPanel(

難一萬倍(我還需要解決如何使用calctype服務器直接到R代碼裏面適當的塊。)

+0

我搞砸了具有原始R代碼的論文的鏈接,它位於 http://www.biorxiv.org/content/biorxiv/early/2017/08/07/144337.full.pdf –

回答

0

這是給你一個工作示例:

library(shiny) 


ui <- fluidPage(

    titlePanel("|Species Table"), 
    sidebarLayout(

     sidebarPanel(
     radioButtons("calctype", "Choose calculation:",selected = "calcFPR", 
        choices = c(
         "prior (for given FPR and P val)" = "calcprior", 
         "P value (for given FPR and prior)" = "calcpval",  
         "FPR (for given P value and prior)" = "calcFPR")), 
     uiOutput("test1"), 
     uiOutput("test2"), 
     inputPanel(
      numericInput("nsamp",label = h5("Number in each sample"), step = 1, 
         value = 16, min = 2) 
     )), 

    mainPanel(textOutput("text1"), 
       textOutput("text2")) 

)) 


server <- function(input, output) { 

    test <- reactive({ 
    if(input$calctype == 'calcprior'){ 
     label1 <- paste("observed P value") 
     label2 <- paste("false positive risk") 
    }else if(input$calctype == 'calcpval'){ 
     label1 <- paste("false positive risk") 
     label2 <- paste("prior probability of real effect") 
    }else{ 
     label1 <- paste("observed P value") 
     label2 <- paste("prior probability of real effect") 
    } 
    list(label1 = label1, label2 = label2) 
    }) 

    output$test1 <- renderUI({ 
    numericInput("test1", label = h5(test()$label1), value = 0.05, min = 0, max = 1) 
    }) 

    output$test2 <- renderUI({ 
    numericInput("test2", label = h5(test()$label2), value = 0.05, min = 0, max = 1) 
    }) 

    output$text1 <- renderText({ 
    input$test1 
    }) 

    output$text2 <- renderText({ 
    input$test2 
    }) 

} 




shinyApp(ui = ui, server = server) 

你不需要那麼多conditionalPanels,我注意到你的代碼,唯一改變的事情就是根據choosen計算numericInput標籤類型。因此,在這種情況下,我使用server中的if...else...語句更改了兩個`numericInputs(test1和test2)的標籤並選擇了計算類型。

此外,我已經打印出numericInput's(text1和text2)的輸出,以向您顯示用戶輸入被精確傳遞給服務器,並且可以在計算中進一步使用。

+0

感謝你的確非常。 R社區真的很棒。今天早上我通過twitter鏈接獲得了另一個解決方案:-) –