【问题标题】:R Shiny : Display tables as an outputR Shiny:将表格显示为输出
【发布时间】:2018-09-30 10:29:08
【问题描述】:

我正在创建一个 R Shiny 应用程序。一切正常,代码运行(我已将 cat() 函数放在所有其他代码中,我看到 R Studio 的控制台中一切正常),但我真的不知道如何在我的特定情况下显示表格。

我的 Shiny 应用接受用户输入并生成 2 个数据集。当用户按下我在界面(eventReactive)中放置的按钮时,它开始。

第一个(dataset_1)是通过 API 调用和几个清理步骤生成的。

第二个数据集(dataset_2)是从 dataset_1 中的治疗生成的。

如何构造 ui.R 和 server.R 代码以在用户界面中将 dataset_1 和 dataset_2(均为表格)显示为输出?

这是我当前的 ui 和服务器文件:

ui.R

shinyUI(fluidPage(
   sidebarLayout(

     sidebarPanel(
        textInput("myfirstinput"),
        textInput("mysecondinput"),
        actionButton("button")
     ),
     mainPanel(
        ???????????
     )
   )
))

server.R

shinyServer(function(input, output) {
   ???????? <- eventReactive(input$button, {
       input1 <- input$myfirstinput
       input2 <- input$mysecondinput
       #function Make dataset_1 from api call (based on input1 & input2)
       dataset_1
       #function clean dataset_1
       #function Make dataset_2 from treatments in dataset_1
       dataset_2
       })
 })

在 server.R 文件中,我真的不知道如何管理 eventReactive(请参阅“????????”),因为我正在生成 2 个数据集...

谢谢!

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    如果我正确理解了您的问题,那么问题在于您有一个函数可以生成两个数据集。虽然eventReactive 调用只能返回一个反应函数,但该函数可以返回一个列表,其中包含可以独立使用的任意数量的不同对象。在您的情况下,将您的两个数据集加入一个列表,然后从您的eventReactive 返回该列表。然后,当您调用该反应函数时(例如在 renderPlotrenderTable 中),您可以选择要使用的列表项:

    shinyUI(fluidPage(
        sidebarLayout(
    
            sidebarPanel(
                textInput("myfirstinput"),
                textInput("mysecondinput"),
                actionButton("button")
            ),
            mainPanel(
                tableOutput("table1"),
                tableOutput("table2")
            )
        )
    )
    
    shinyServer(function(input, output) {
        plots.dfs <- eventReactive(input$button, {
            # Make dataset_1
            # Make dataset_2
            return(list(dataset_1, dataset_2)
        })
    
        output$table1 <- renderTable({ plots.dfs()[[1]] })
        output$table2 <- renderTable({ plots.dfs()[[2]] })
    })
    

    【讨论】:

    • 感谢您的回答,但这并不是我所需要的。我不想绘制任何东西,我想在用户界面中显示一个表(dataset_2)
    • 如果要显示表格,只需使用tableOutputrenderTable 函数即可。还是你根本不关心dataset_1
    猜你喜欢
    • 1970-01-01
    • 2018-02-27
    • 2016-05-10
    • 2018-07-08
    • 2022-01-15
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    相关资源
    最近更新 更多