【问题标题】:Outtputing several tables on one page in Shiny在 Shiny 的一页上输出多个表格
【发布时间】:2013-09-26 19:24:09
【问题描述】:

我正在尝试构建一个接收数据文件名的 Shiny 接口,然后运行一个生成 4 个表(矩阵)的 .R 脚本,并在 Shiny 中一次将它们全部输出。例如:

ui.R

shinyUI(pageWithSidebar(
    headerPanel("Calculate CDK fingerprints"),
    sidebarPanel(
        textInput("text_input_fingerprints", "Enter smiles file name:"),
        actionButton("runButton", "Run")
    ),
    mainPanel(
        tableOutput("cdk")
    )
   )
  )

服务器.R

shinyServer(function(input,output){

output$cdk <- renderTable({

    input$runButton

    if (input$runButton == 0) {return()}        
    else{
         source('calculate_cdk_fingerprints.R', local = TRUE)
         print(table1)
         print(table2)
         print(table3)
         print(table4)
        }
     })
})

不幸的是,Shiny 只打印最后一个表,即表 4。而且我不能真正拆分 .R 脚本,因为它还会将一些文件输出到本地文件夹。是的,我真的需要使用 actionButton()。

有什么建议吗?提前致谢!

【问题讨论】:

  • 你试过为每个表单独调用 renderTable() 吗?

标签: r shiny


【解决方案1】:

您需要为每个单独的表格输出。或者,您可以使用 verbatimTextSummaryrbind

mainPanel(
    tableOutput("cdk1"),
    tableOutput("cdk2"),
    tableOutput("cdk3")
)

ui.r

shinyUI(pageWithSidebar(
    headerPanel("Calculate CDK fingerprints"),
    sidebarPanel(
        textInput("text_input_fingerprints", "Enter smiles file name:"),
        actionButton("runButton", "Run")
    ),
    mainPanel(
        tableOutput("cdk1"),
        tableOutput("cdk2"),
        tableOutput("cdk3")
    )
   )
  )

server.r

shinyServer(function(input,output){

#----
## eg: 
# source('calculate_cdk_fingerprints.R', local = TRUE)
#-----
## Example: 
table1 <- matrix(1:20, nrow=4)
table2 <- matrix(101:120, nrow=4)
table3 <- matrix(201:220, nrow=4)


output$cdk1 <- renderTable({

    input$runButton

    if (input$runButton == 0) {return()}        
    else{
         table1
        }
     })

output$cdk2 <- renderTable({

    input$runButton

    if (input$runButton == 0) {return()}        
    else{
         table2
        }
     })

output$cdk3 <- renderTable({

    input$runButton

    if (input$runButton == 0) {return()}        
    else{
         table3
        }
     })
})

【讨论】:

  • 这几乎是正确的;但我认为@ssantic 希望在按下操作按钮时(重新)计算数据。需要有一个反应表达式,比如calc,运行source('calculate_cdk_fingerprints.R', local = TRUE) 并返回list(table1, table2, table3, table4)renderTable 调用可以执行calc()[[1]] 等等。 (哦,如果input$runButton == 0,反应表达式应该立即返回NULL,并且renderTable调用应该在访问calc()时检查NULL。)
  • 乔,感谢您的参与!请随时根据需要编辑我的答案
  • @JoeCheng 实际上,我不需要重新计算数据 - .R 脚本计算 4 个表的数据,并将结果输出到 .csv 文件中,另外我需要显示视觉上闪亮的表。编辑:上面的解决方案是好的,但问题是我不能在 4 个 renderTable 函数之前 source() .R 脚本 - 它需要在给出输入并按下 actionButton 后运行......
  • 如果我在例如 output$cdk1 中运行 source() 脚本,然后尝试在 output$cdk2 中打印 table2,R 告诉我找不到对象 'table2'。
猜你喜欢
  • 2014-03-20
  • 1970-01-01
  • 2019-08-17
  • 2021-01-09
  • 1970-01-01
  • 2020-01-18
  • 1970-01-01
  • 2019-10-23
  • 2020-04-27
相关资源
最近更新 更多