0

我想在Shiny应用程序中实现一个功能。我自己的函数get_calculate()将参数 data 和容差作为输入,并list用 adata.frame和 a 重新运行 a plot

我想根据公差显示输出。在我的服务器功能中,我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)
4

3 回答 3

0

您提到的错误很可能来自您尝试从 test$data_kpm 中选择几列的 renderDataTable。检查数据框以获取确切的列名。

于 2016-11-21T10:36:37.780 回答
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)
   }
 )

}
于 2016-11-21T12:01:39.283 回答
0

为什么不使用反应式表达式只运行一次 get_calculate ?然后在你的 output$plot_kpm 和 output$table_kpm 中使用结果?这将优化您的代码。

于 2016-11-22T09:40:16.963 回答