2016-11-21 42 views
0

我想在Shiny應用程序中實現功能。我自己的功能get_calculate()具有參數數據和容差作爲輸入並且使用data.frameplot重新生成list具有自己功能的閃亮應用程序

我想根據公差顯示輸出。在我的服務器功能中,我使用reactive()來運行get_calculate(),但它不起作用。

如果我寫在renderPlot()renderDataTable()get_calculate()的作品。 但是,對於大型數據集,效率很低,因爲Shiny必須運行get_calculate()兩次。

library(shiny) 
library(shinydashboard) 
library(foreign) 
#load my own function 
source("01-get_calculate.R") 


ui <- dashboardPage(

    dashboardHeader(title = "Analysis"), 
    dashboardSidebar(
    sidebarMenu(
     menuItem("Load data", tabName = "data", icon = icon("database")), 
     menuItem("Mainboard", tabName = "Mainboard", icon = icon("dashboard")) 
    ) 
), 
    dashboardBody(
    tabItems(
     tabItem(tabName = "data", 
       fileInput("datafile", "Choose file", 
         accept = c("text/csv/rds/dbf", 'text/comma-separated-values,text/plain')), 

       dataTableOutput("mytable") 

    ), 
     tabItem(tabName = "Mainboard", 
       fluidRow(
       box(
        title = "Input", status = "primary", solidHeader = TRUE, collapsible = TRUE, 
        sliderInput(inputId = "tol", 
           label = "Tolerance", 
           value = 4, min = 1, max = 15, step = 1) 
       )), 
       fluidRow(
       box(
        title = "Adherence Curve", status = "warning", solidHeader = TRUE, collapsible = TRUE, 
        plotOutput("plot_kpm") 
       ), 

       box(
        title = "Overview Table", status = "primary", solidHeader = TRUE, collapsible = TRUE, 
        tableOutput("table_kpm") 
      ) 
    ) 
    ) 
) 
) 
) 



server <- function(input, output) { 


    filedata <- reactive({ 
    infile <- input$datafile 
    if (is.null(infile)) { 
     return(NULL) 
    } 
    read.dbf(infile$datapath) 
    }) 



    output$mytable <- renderDataTable({ 
    filedata() 
    }) 

    **test <- reactive({ 
    get_calculate(filedata(), tolerance = input$tol) 
    }) 

    output$plot_kpm <- renderPlot({ 
    test$kpm_chart 
    }) 

    output$table_kpm <- renderDataTable({ 
    test$data_kpm[, c("Time", "numbers", "Percent")] 
    })** 


} 

shinyApp(ui = ui, server = server) 
+2

嘗試'源(「01-get_calculate.R」,本地= TRUE') –

+0

感謝您的回答。閃亮的應用程序仍然無法正常工作。閃亮的應用程序中的錯誤消息是:「類型'關閉'的對象不是子集」 – user5308682

+0

也許'infile < - req(input $ datafile)'而不是'if(is.null(infile))' – user5029763

回答

0

您提到的錯誤很可能來自renderDataTable,您嘗試從test $ data_kpm中選取幾列。檢查數據幀的確切列名稱。

+0

名稱是正確的。當我在renderDatatable和renderplot中使用我自己的函數時,閃亮的應用程序可以工作,但對於大型數據集,效率非常低。 – user5308682

0

這個版本的閃亮應用程序運行。但它不夠高效,因爲閃亮必須運行兩次get_calculate。

server <- function(input, output) { 

    #This function is repsonsible for loading in the selected file 
    filedata <- reactive({ 
    infile <- input$datafile 
    if (is.null(infile)) { 
     # User has not uploaded a file yet 
     return(NULL) 
    } 
    read.dbf(infile$datapath) 
    }) 



    output$mytable <- renderDataTable({ 
    filedata() 
    }) 

    output$plot_kpm <- renderPlot({ 
    get_calculate(filedata(), tolerance = input$tol)$kpm_chart 
    }) 

output$table_kpm <- renderTable({ 
    get_calculate(filedata(), tolerance = input$tol)$data_kpm[, c("Time", "Percent", "Patients")] 
    }) 


output$download_mainboard_adherence_table <- downloadHandler(
               filename = paste("adherence_table", '.csv', sep=''), 
               content = function(file) { 
               write.csv(get_calculate(filedata(), tolerance = input$tol)$data_kpm[, c("Time", "Percent", "Patients")], file) 
    } 
) 

} 
0

爲什麼不使用反應式表達式來運行get_calculate一次?然後在輸出$ plot_kpm中使用結果並輸出$ table_kpm? 這將優化您的代碼。

+0

感謝您的回答。這就是我在第一個例子中所做的事情?但閃亮的應用程序不起作用。在劇情和桌面上閃亮的應用程序中的錯誤消息是:「類型'閉'的對象不是子集」 – user5308682

+0

這是一種很難回答,沒有任何可重複的例子。 – Pauline

+0

您是否嘗試過使用test()$ kpm_chart而不是測試$ kpm_chart? – Pauline

相關問題