带有多个React池的闪亮表

p5fdfcr1  于 2022-12-20  发布在  React
关注(0)|答案(1)|浏览(109)

我试图创建一个表,其中包含一些常量值(例如,Cancer_Stage列),然后是React值,当输入发生变化时,React值会立即发生变化。

library(shiny)
library(tidyverse)
library(DT)

df <- dplyr::tibble(Cancer_Stage = c("Localized",
                                     "Regional",
                                     "Distant"), 
                    Costs = c("95",
                               "24",
                               "25"))
ui <- fluidPage(
  
  # App title ----
  titlePanel("Cancer Stage and Costs"),
  
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
    
    # Sidebar panel for inputs ----
    sidebarPanel(
      
      # Input: Cost of treatment ---- 
      shiny::textInput(inputId = "Cost_1", label = "Cost Localized"),
      shiny::textInput(inputId = "Cost_2", label = "Cost Regional"),
      shiny::textInput(inputId = "Cost_3", label = "Cost Distant")
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      # Output: Histogram ----
      DT::DTOutput(outputId = "table")
      
    )
  )
)

# Define server logic ----
server <- function(input, output, session) {
  
  mod_df <- shiny::reactiveValues(x = df)
  
  output$table <- DT::renderDT({
    
    isolate(mod_df$x)
    
  })
  

  reactive({
    
    mod_df$x <- mod_df$x %>%
      dplyr::bind_rows(
        dplyr::tibble(Cancer_Stage = c("Localized",
                                       "Regional",
                                       "Distant"),
                      Weight = c(input$Cost_1,
                                 input$Cost_2,
                                 input$Cost_3))
      )
    
  })

  
  
}
shinyApp(ui, server)

我重用了这段代码并取得了一些进步,但是当我改变输入时,表不想更新。谢谢你的帮助!

k10s72fa

k10s72fa1#

这是否说明了你想做什么?

library(shiny)
library(tidyverse)
library(DT)

df <- dplyr::tibble(Cancer_Stage = c("Localized",
                                     "Regional",
                                     "Distant"), 
                    Costs = c("95",
                              "24",
                              "25"))
ui <- fluidPage(
  
  # App title ----
  titlePanel("Cancer Stage and Costs"),
  
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
    
    # Sidebar panel for inputs ----
    sidebarPanel(
      
      # Input: Cost of treatment ---- 
      shiny::textInput(inputId = "Cost_1", label = "Cost Localized", value = "95"),
      shiny::textInput(inputId = "Cost_2", label = "Cost Regional", value = "24"),
      shiny::textInput(inputId = "Cost_3", label = "Cost Distant", value = "25")
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      # Output: Histogram ----
      DT::DTOutput(outputId = "table")
      
    )
  )
)

# Define server logic ----
server <- function(input, output, session) {
  
  mod_df <- shiny::reactiveValues(x = df)
  
  
  
  
  observe({
    mod_df$x <- dplyr::mutate(mod_df$x, Weight = c(input$Cost_1,
                                 input$Cost_2,
                                 input$Cost_3)
      )
  })
  
  output$table <- DT::renderDT({
    mod_df$x
  })
  
}
shinyApp(ui, server)

相关问题