【问题标题】:Render datatable after click in Shiny点击 Shiny 后渲染数据表
【发布时间】:2019-12-14 05:56:56
【问题描述】:

我需要帮助弄清楚如何在用户点击预测功能时产生一些输出(显示战斗预测结果的数据表)。

我已尝试在网上遵循一些示例,但似乎没有得到我想要的操作。不确定我是否可以分享很多可重现的示例,因为预测按钮接受输入并通过我构建的预测模型运行它。

这是我写的代码

column(offset=5,width = 8,
           actionButton("gobutton","Predict"),
           dataTableOutput("predictions")),

在服务器函数中

observeEvent(input$goButton, {
    output$predictions <- renderDataTable(
      {
        preds3 <- bind_cols(preds,preds2)
        print(cat_model$predict(preds3))
        cat_model$predict(preds3)
      }
    )
  })

目前,如果我点击应用中的预测按钮,什么也不会发生

更新:

 data <- eventReactive(
    input$goButton, {
        preds3 <- bind_cols(preds,preds2)
        print(cat_model$predict(preds3))
        cat_model$predict(preds3)
    }
  )
  output$predictions <- renderDataTable({
    data()
  })

但是当我点击预测时仍然没有得到任何输出 我还尝试将服务器上的输出包装在 eventReactive 中:

这是 cat_model$predict(preds3) 的输出:

            [,1]      [,2]      [,3]
[1,] 8.77947e-06 0.3193044 0.6806869

这是我从 dput(cat_model..) 得到的输出:

structure(c(8.77946992728772e-06, 0.31930435971863, 0.680686860811443
), .Dim = c(1L, 3L))

一瞥:

 num [1, 1:3] 8.78e-06 3.19e-01 6.81e-01

遵循其中一个答案中给出的建议:

data <- eventReactive(input$goButton, {
    preds3 <- bind_cols(preds,preds2)
    print(dput(cat_model$predict_proba(preds3)))
    cat_model$predict_proba(preds3)
  })
  output$predictions <- renderDataTable({
    data()
  })

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我认为eventReactive 在这里比observeEvent 更合适。直接使用您的输出(而不是调用cat$model),DT 在此处正确呈现。

    library(shiny)
    library(DT)
    
    ui <- {
        fluidPage(
            fluidRow(
                actionButton('goButton', 'Predict'),
                DTOutput('predictions')
            )
        )
    }
    
    server <- function(input, output, session) {
    
        data <- eventReactive(input$goButton, {
            structure(c(8.77946992728772e-06, 0.31930435971863, 0.680686860811443
                ), .Dim = c(1L, 3L))
        })
    
        output$predictions <- renderDT({
            data()
        })
    
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 似乎不起作用,已添加 preds3 的输出
    • 粘贴dput(cat_model$predict(preds3))dplyr::glimpse(cat_model$predict(preds3))的输出
    • 好吧,使用您的输出,我的代码会呈现您输出的正确 DT。你试过吗?
    • 你能用我的 UI 运行我的代码吗?我想知道这是 UI 问题而不是服务器问题吗?还有到底发生了什么?
    猜你喜欢
    • 2021-02-10
    • 1970-01-01
    • 2016-04-15
    • 2021-02-05
    • 1970-01-01
    • 2019-10-03
    • 2017-05-19
    • 2021-03-12
    • 2020-11-13
    相关资源
    最近更新 更多