简体   繁体   English

Shiny - 如何根据原始数据第一个表中的用户输入更新 Shiny 中的第二个汇总表?

[英]Shiny - How can you update a second summary table in Shiny based on user input in raw data first table?

I am building my first Shiny App to replace an Excel report.我正在构建我的第一个 Shiny 应用程序来替换 Excel 报告。 One feature that is necessary is the option to be able to edit the data in the table and see summarized values update with the user-inputted value.一项必要的功能是能够编辑表中的数据并查看汇总值随用户输入的值更新的选项。

I am stuck figuring out how to capture that new value and refresh the summary table using that new value.我一直在弄清楚如何捕获该新值并使用该新值刷新汇总表。

If there is another similar question, please share it.如果还有其他类似的问题,请分享。

I have tried using Observe, ObserveEvent and Reactive.我尝试过使用 Observe、ObserveEvent 和 Reactive。 I do not grasp these concepts completely, however, when I have used them as intended, they have worked as I expected.我没有完全掌握这些概念,但是,当我按预期使用它们时,它们会按我的预期工作。

I have tried to replicate the issue using some available R Datasets.我尝试使用一些可用的 R 数据集来复制该问题。 See code below.请参阅下面的代码。

The tableOutput containing the initial data based on user selection is editable.包含基于用户选择的初始数据的 tableOutput 是可编辑的。 Hence, when a new number is placed in that column, I expect the summaryOutput to update and reflect accordingly.因此,当在该列中放置一个新数字时,我希望 summaryOutput 会相应地更新和反映。

Any guidance or input is greatly appreciated.非常感谢任何指导或意见。 This will be a big win over Excel once this works.一旦成功,这将是对 Excel 的巨大胜利。

Thank you!谢谢!

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)
library(DT)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30),
            selectInput("data",
                        "Select Dataset",
                        choices = c( "iris", "mtcars", "faithful"),
                        selected = "faithful")
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot"),
           DT::dataTableOutput("tableOutput"),
           DT::dataTableOutput("summaryOutput"),
           textOutput("text")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
    
    # https://nbisweden.github.io/RaukR-2019/shiny/lab/shiny_lab.html
    getdata <- reactive({ get(input$data, 'package:datasets') })
    getsummary <- reactive({summary(getdata()[2])})

    output$distPlot <- renderPlot({
        
        # generate bins based on input$bins from ui.R
        
        x    <- as.numeric(getdata()[,2])
        xname <- colnames(getdata()[2])
        bins <- seq(min(x), max(x), length.out = input$bins + 1)
        
        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white',
             main = paste("Histogram of" , xname),)
        #hist(x)
    })
    
    output$tableOutput <- DT::renderDataTable({
        DT::datatable(getdata(),
                      editable = TRUE,
                      options = list(orderClasses = TRUE
                                     )
        )
    })
    
    
    output$summaryOutput <- DT::renderDataTable({
        DT::datatable(getsummary())
    })
    
    
    proxy = dataTableProxy('tableOutput')
    
    observeEvent(input$x1_cell_edit, {
        info = input$x1_cell_edit
        str(info)
        i = info$row
        j = info$col
        v = info$value
        getdata()[i, j] <<- DT::coerceValue(v, getdata()[i, j])
        replaceData(proxy, getdata(), resetPaging = FALSE)  # important
    })
    

}

# Run the application 
shinyApp(ui = ui, server = server)


One can't modify the output of a reactive conductor, as you're trying to do like this:无法修改电抗导体的 output,因为您正在尝试这样做:

getdata()[i, j] <- ......

Use reactive values instead:改用反应值:

getdata <- reactiveVal()
getsummary <- reactiveVal()

observeEvent(input$data, {
  dataset <- get(input$data, 'package:datasets')
  getdata(dataset)
})

observeEvent(getdata(), {
  datasummary <- summary(getdata()[2])
  getsummary(datasummary)
})

Then:然后:

observeEvent(input$x1_cell_edit, {
  info <- input$x1_cell_edit
  i <- info$row
  j <- info$col
  v <- info$value
  dataset <- getdata()
  dataset[i, j] <- coerceValue(v, dataset[i, j])
  replaceData(proxy, dataset, resetPaging = FALSE)
  getdata(dataset)
})

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

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