【问题标题】:shiny app with own function具有自己功能的闪亮应用
【发布时间】:2017-04-04 15:16:52
【问题描述】:

我想在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)

【问题讨论】:

  • 试试source("01-get_calculate.R", local = TRUE)
  • 感谢您的回答。闪亮的应用程序仍然无法正常工作。闪亮应用程序中的错误消息是:“'closure' 类型的对象不是子集”
  • 可能是infile &lt;- req(input$datafile) 而不是if(is.null(infile))

标签: r shiny shinydashboard reactive


【解决方案1】:

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

【讨论】:

  • 名称正确。当我在 renderDatatable 和 renderplot 中使用我自己的函数时,闪亮的应用程序可以工作,但对于大型数据集,它的效率非常低。
【解决方案2】:

我的闪亮应用程序的这个版本运行。但它效率低下,因为闪亮必须运行 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)
   }
 )

}

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 感谢您的回答。这就是我在第一个示例中所做的?但是闪亮的应用程序不起作用。在绘图和表格上闪亮的应用程序中的错误消息是:“'闭包'类型的对象不是子集”
    • 这是一种很难回答的问题,没有任何可重复的例子。
    • 你试过用 test()$kpm_chart 代替 test$kpm_chart 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    相关资源
    最近更新 更多