简体   繁体   English

R Shiny:将表显示为输出

[英]R Shiny : Display tables as an output

I am creating an R Shiny app. 我正在创建一个R Shiny应用程序。 Everything is OK and the code runs (I have put cat() functions all other the code and I see everything works well in R Studio's console), but I don't really know how to display a table in my specific case. 一切正常,代码运行(我已经将cat()函数放到了所有其他代码中,并且我发现在R Studio控制台中一切正常),但是我真的不知道如何在我的特定情况下显示表格。

My Shiny app takes user input and generate 2 datasets. 我的Shiny应用程序接受用户输入并生成2个数据集。 It starts when the user push the button I have putted in interface (eventReactive). 当用户按下我在界面(eventReactive)中放入的按钮时,它开始。

The first one (dataset_1) is generated from API calls and several cleaning steps. 第一个(数据集_1)是通过API调用和几个清理步骤生成的。

The second dataset (dataset_2) is generated from treatments in dataset_1. 第二个数据集(数据集_2)是根据数据集_1中的处理生成的。

How to structure the ui.R and server.R code to display the dataset_1 and dataset_2 (both are tables) as outputs in the user interface ? 如何构造ui.R和server.R代码以在用户界面中将数据集_1和数据集_2(均为表)显示为输出?

Here are my current ui and server files: 这是我当前的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
       })
 })

In server.R file, I don't really know how to manage the eventReactive (see "????????") because I am generating 2 datasets... 在server.R文件中,我真的不知道如何管理eventReactive(请参阅“ ????????”),因为我正在生成2个数据集...

Thanks! 谢谢!

If I'm understanding your question correctly, the problem is that you have one function that generates two datasets. 如果我正确地理解了您的问题,那么问题在于您有一个函数可以生成两个数据集。 While an eventReactive call can only return a single reactive function, that function can return a list containing any number of different objects that can be used independently. 尽管eventReactive调用只能返回一个反应性函数,但该函数可以返回包含可独立使用的任意数量的不同对象的列表。 In your case, join your two datasets into a list and then return that list from your eventReactive . 在您的情况下,将两个数据eventReactive并到一个列表中,然后从eventReactive返回该列表。 Then, when you call that reactive function (for example in a renderPlot or renderTable ), you can choose which list item you want to work with: 然后,当您调用该反应性函数(例如在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]] })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM