如何在R Shiny中访问动态创建的Rhosontable值并进行计算? [英] How to access dynamic created rhandsontable values and do calculations in R Shiny?

查看:63
本文介绍了如何在R Shiny中访问动态创建的Rhosontable值并进行计算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个可以在第一个选项卡(输入"选项卡)中使用表动态创建多个选项卡的应用程序,并使用动态创建的表在第二个选项卡(结果"选项卡)中进行计算.但是,我不确定如何使用hot_to_r函数从输入"选项卡中创建的表中访问值,以在结果"选项卡中进行计算.例如,我要在输入"选项卡的表中将第i + 1列除以第i列,然后在结果"选项卡中显示结果.

I am trying to make an app which can dynamically create multiple tabs with tables in the 1st tab (Input tab), and use the dynamically created tables to do calculations in the 2nd tab (Results tab). However, I am not sure how to use the hot_to_r function to access the values from the tables created in the Input tab to do calculations in the Results tab. As an example, I want to take column i+1 divide by column i in the tables in the Input tab and display the results in the Results tab.

下面是示例代码:

library(shiny)
library(rhandsontable)

ui <- navbarPage("App",
                   
                 tabPanel("Input",
                          numericInput('num_of_table', "Number of sub tabs: ", value = 1, min = 1, max = 10),
                          uiOutput("input")),
                 
                 tabPanel("Results",
                          uiOutput("results"))
                   
)

server <- function(input, output,session) {
  
 ### Input ### 
  
  input_table<- reactive({
    list_of_input_table = list()
    
    for (i in c(1:input$num_of_table)){
      mat <- matrix(c(1:25) * i, ncol = 5, nrow = 5)
      list_of_input_table[[i]] = mat
    }
    
    index = c(1:i)
    list_of_input_table[index]
  })
  
  observeEvent(input$num_of_table, {
    lapply(seq_len(input$num_of_table), function(i) {
      output[[paste0('input_table_', i)]] <- renderRHandsontable({
        
        rhandsontable(input_table()[[i]])
      })
    })
  })
  
  output$input <- renderUI({
    
    nTabs = input$num_of_table
    
    myTabs1 = lapply(seq_len(nTabs), function(x){
      tabPanel(paste("Tab", x),
               column(12,
                      rHandsontableOutput(paste0("input_table_", x))))
    })
    do.call(tabsetPanel, myTabs1)
    
  })
  
  ### Results ###
  
  results_table<- reactive({
    list_of_results_table = list()
    list_of_input_table = list()
    
    for (i in c(1:input$num_of_table)){
      for (j in c(1:5)) {
          
        list_of_input_table[[i]] <- as.matrix(hot_to_r(input[[paste0("input_table_",i)]]))
        list_of_results_table[[i]] <- matrix(as.numeric(NA), ncol = 4, nrow = 5)
        list_of_results_table[[i]][,j] <- list_of_input_table[[i]][,j+1][!is.null(list_of_input_table[[i]][,j+1])] / list_of_input_table[[i]][,j]
      
    }}
    
    index = c(1:i)
    list_of_results_table[index]
  })
  
  
  observeEvent(input$num_of_table, {
    lapply(seq_len(input$num_of_table), function(i) {
      output[[paste0('results_table_', i)]] <- renderRHandsontable({
        
        rhandsontable(results_table()[[i]])
      })
    })
  })
  
  output$results <- renderUI({
    
    nTabs = input$num_of_table
    
    myTabs2 = lapply(seq_len(nTabs), function(x){
      tabPanel(paste("Tab", x),
               column(12,
                      rHandsontableOutput(paste0("results_table_", x))))
    })
    do.call(tabsetPanel, myTabs2)
    
  })
}


shinyApp(ui,server)

请帮助!

推荐答案

在这种情况下,似乎 hot_to_r 不能正确处理矩阵对象.

It seems hot_to_r isn't handling matrix objects correctly in this case.

请改用 data.frame 进行以下检查:

library(shiny)
library(rhandsontable)

ui <- navbarPage("App",
                 
                 tabPanel("Input",
                          numericInput('num_of_table', "Number of sub tabs: ", value = 1, min = 1, max = 10),
                          uiOutput("input")),
                 
                 tabPanel("Results",
                          uiOutput("results"))
                 
)

server <- function(input, output,session) {
  
  ### Input ### 
  input_table <- reactive({
    list_of_input_table = list()
    
    for (i in c(1:input$num_of_table)){
      mat <- matrix(c(1:25) * i, ncol = 5, nrow = 5)
      list_of_input_table[[i]] = as.data.frame(mat)
    }
    
    index = c(1:i)
    list_of_input_table[index]
  })
  
  observeEvent(input$num_of_table, {
    lapply(seq_len(input$num_of_table), function(i) {
      output[[paste0('input_table_', i)]] <- renderRHandsontable({
        rhandsontable(input_table()[[i]])
      })
    })
  })
  
  output$input <- renderUI({
    
    nTabs = input$num_of_table
    
    myTabs1 = lapply(seq_len(nTabs), function(x){
      tabPanel(paste("Tab", x),
               column(12,
                      rHandsontableOutput(paste0("input_table_", x))))
    })
    do.call(tabsetPanel, myTabs1)
    
  })
  
  ### Results ###
  results_table <- reactive({
    
    list_of_results_table = list()
    for (i in c(1:input$num_of_table)){
      req(input[[paste0("input_table_", i)]])
      list_of_results_table[[i]] <- hot_to_r(input[[paste0("input_table_", i)]])[2:5]/hot_to_r(input[[paste0("input_table_", i)]])[1:4]
      }
    return(list_of_results_table)
  })
  
  
  observeEvent(input$num_of_table, {
    lapply(seq_len(input$num_of_table), function(i) {
      output[[paste0('results_table_', i)]] <- renderRHandsontable({
        
        rhandsontable(results_table()[[i]])
      })
    })
  })
  
  output$results <- renderUI({
    
    nTabs = input$num_of_table
    
    myTabs2 = lapply(seq_len(nTabs), function(x){
      tabPanel(paste("Tab", x),
               column(12,
                      rHandsontableOutput(paste0("results_table_", x))))
    })
    do.call(tabsetPanel, myTabs2)
    
  })
}


shinyApp(ui,server)

我在此处提出了问题.

这篇关于如何在R Shiny中访问动态创建的Rhosontable值并进行计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆