繁体   English   中英

R Shiny修改反应式表格中的特定元素

[英]R shiny modify a specific element in a reactive table

我有一个小型的闪亮应用程序,旨在读取数据集,然后计算每个元素的平均时间(按类别分组)。 我的数据集是以下版本的更复杂版本:

item time category
apple 13 cat1
bobcat 6 cat2
cement 15 cat1
doomsday 15 cat1
elephant 7 cat2

读完该文件后,我生成了一个表格,该表格描述了归因于每个类别的总时间百分比。 这部分工作得很漂亮。

类别| 时间百分比

cat1 | 76.79

cat2 | 23.21

为了使事情复杂一点,我有一个输入字段,用户可以在其中估计他们花在特定类别上的时间的百分比-旨在捕获未在数据集中表示的时间估计。 然后,我生成第二个表,该表将重新计算分配给每个类别的时间百分比,并根据估计属于该类别的时间百分比进行调整 如果我们输入到cat2的时间的50%的时间未在数据中表示,那么我希望将表修改为:

类别| 时间百分比

cat1 | 38.39

cat2 | 61.61

其中61.61 = 11.61 + 50

目前,我能够生成一个表格,其中包含每个类别中正确的时间百分比,但不包括用户输入百分比。 所以我得到的加起来少于100%:

类别| 时间百分比

cat1 | 38.39

cat2 | 11.61

我想通过将输入值添加到计算出的百分比时间来调整cat2的一个特定PercentTime元素,因此它是正确的数字(61.61)。

闪亮的应用代码:

library(shiny)
library(readxl)
library(dplyr)
library(ggplot2)

ui = fluidPage(
  sidebarPanel(
    fileInput('file1', 'inputtext'),
    numericInput('UnknownPercent', 'labeltext', 50)),

  mainPanel(
    tabsetPanel(type = "tabs",
                tabPanel("tab1", 
                         tableOutput("data_only"),
                         tableOutput("data_inc_unknown_percent"))
    )
  )
)

server = function(input, output) {

  G2T <- reactive({
    if(input$file1 == 0){return()}
    inFile <- input$file1
    if (is.null(inFile)){return(NULL)}

    isolate({ 
      G2T_raw <- read.csv(inFile$datapath)
    })
    G2T_raw
  })

  TotalTime <- reactive({
    if(input$file1 == 0){return()}
    inFile <- input$file1
    if (is.null(inFile)){return(NULL)}

    isolate({ 
      TotalTime <- G2T() %>%
        summarise(TotalTime = sum(time))
      })
    TotalTime
  })

  CatTable <- reactive({
    if(input$file1 == 0){return()}
    inFile <- input$file1
    if (is.null(inFile)){return(NULL)}

    isolate({ 
      TotalTime <- G2T() %>%
        summarise(TotalTime = sum(time))
      CatTable <- G2T() %>%
        group_by(category) %>%
        summarise(PercentTime = sum(time) / TotalTime * 100)
    })
    CatTable
  })

  CatTablePlus <- reactive({
    if(input$file1 == 0){return()}
    inFile <- input$file1
    if (is.null(inFile)){return(NULL)}

    isolate({ 
      TotalTime <- G2T() %>%
        summarise(TotalTime = sum(time))
      CatTablePlus <- G2T() %>%
        group_by(category) %>%
        summarise(PercentTime = ((sum(time) / TotalTime()) * (100 - input$UnknownPercent[1])))
      CatTablePlus <- as.data.frame(CatTablePlus)
    })
    CatTablePlus
  })



  output$data_only <- renderTable({
    req(input$file1)
    CatTable()  
  }) 

  output$data_inc_unknown_percent <- renderTable({
    req(input$file1) 
    CatTablePlus()
  }) 
}
runApp(list(ui = ui, server = server))

我一辈子都无法弄清楚如何将用户输入百分比添加到表的[category = cat2,time]元素中。 我努力了:

  • 将表操作逻辑移动到renderTable部分或将其保留在反应部分的不同位置
  • 使用类似CatTablePlus[CatTablePlus$category == "cat1", "PercentTime"] <- CatTablePlus[CatTablePlus$category == "cat1", "PercentTime"] + input$UnknownPercent[1]的命令将生成的表视为具有可修改元素的东西CatTablePlus[CatTablePlus$category == "cat1", "PercentTime"] <- CatTablePlus[CatTablePlus$category == "cat1", "PercentTime"] + input$UnknownPercent[1] 根据我的处理方式,我要么执行将UnknownPercent [1]添加到所有PercentTime元素的操作,要么得到类似Error: 'row.names' should specify one of the variables在输出中Error: 'row.names' should specify one of the variables之一。
  • 在反应性表对象的位置创建新的局部变量
  • 将我的反应式表强制转换为不同类型,例如data.frame

当用户调整numericInput值时,如何反应地调整Shiny renderTable对象中的单个元素?

我对您的代码进行了一些更改,并注释掉了您的代码中的某些部分。

  1. 使用eventReactive跟踪输入numericInput更改并相应地渲染数据numericInput
  2. 假设您只有两个类别,则对百分比转换逻辑进行了硬编码。

更新的代码:

    library(shiny)
library(readxl)
library(dplyr)
library(ggplot2)

ui = fluidPage(
  sidebarPanel(
    fileInput('file1', 'inputtext'),
    numericInput('UnknownPercent', 'labeltext', 50)),

  mainPanel(
    tabsetPanel(type = "tabs",
                tabPanel("tab1", 
                         tableOutput("data_only"),
                         tableOutput("data_inc_unknown_percent"))
    )
  )
)

server = function(input, output) {

  G2T <- reactive({
    if(input$file1 == 0){return()}
    inFile <- input$file1
    if (is.null(inFile)){return(NULL)}

    isolate({ 
      G2T_raw <<- read.csv(inFile$datapath)
    })
    G2T_raw
  })

  TotalTime <- reactive({
    if(input$file1 == 0){return()}
    inFile <- input$file1
    if (is.null(inFile)){return(NULL)}

    isolate({ 
      TotalTime <- G2T() %>%
        summarise(TotalTime = sum(time))
    })
    TotalTime
  })

  CatTable <- reactive({
    if(input$file1 == 0){return()}
    inFile <- input$file1
    if (is.null(inFile)){return(NULL)}

    isolate({  
      CatTable <- G2T() %>% group_by(category) %>%
        summarise(time = sum(time)) %>% ungroup(category) %>% mutate(time = (time / sum(time))*100)
    })
    CatTable
  })

  CatTablePlus <- eventReactive(input$UnknownPercent,{ 


     # TotalTime <-  G2T() %>%
      #  summarise(TotalTime = sum(time))

      # G2T() %>%
      #  group_by(category) %>%
       # summarise(PercentTime = ((sum(time) / TotalTime) * (100 - input$UnknownPercent[1])))

    unknownpct_table <- CatTable() 

    #unknownpct_table$time[unknownpct_table$category == 'cat2'] <- 100 -  unknownpct_table$time[unknownpct_table$category == 'cat1']

    unknownpct_table$time[unknownpct_table$category == 'cat1'] <- unknownpct_table$time[unknownpct_table$category == 'cat1'] -  unknownpct_table$time[unknownpct_table$category == 'cat1'] * (input$UnknownPercent[1] /100)
    unknownpct_table$time[unknownpct_table$category == 'cat2'] <- 100 -  unknownpct_table$time[unknownpct_table$category == 'cat1']

    unknownpct_table

  })



  output$data_only <- renderTable({
    req(input$file1)
    CatTable()
  }) 

  output$data_inc_unknown_percent <- renderTable({
    req(input$file1) 
    CatTablePlus()
  }) 
}
runApp(list(ui = ui, server = server))

暂无
暂无

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

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