Reputation: 379
I am trying to create a table where I have some constant values (e.g., in the Cancer_Stage column) and then reactive values that change immediately when the inputs are changed.
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)
I reused this code and made some progress but the table doesn't want to update when I change the inputs. Thanks for helping out a noob!
Upvotes: 0
Views: 51
Reputation: 8117
Does this get at what you are trying to do?
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)
Upvotes: 1