更新R Shiny中的变量

ee7vknir  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(111)

我将需要一些帮助与丢失的代码在这里:

selectInput("portfolio",
            "Portfolio:",
            c("p1","p2"))
## missing code:
## if input$portfolio == "p1" do a bunch of calculations and spit out the variable var (a tibble).

# variable var goes into a reactiveVal...
table <- reactiveVal()
table(var)
wgxvkvu9

wgxvkvu91#

在服务器上,您可以将table(不是一个很好的名称,也许可以使用其他名称,如my_table)设置为reactiveValues(),然后观察input$portfolio中的更改

table <- reactiveValues(var=NULL)

observeEvent(input$portfolio, {
  if(input$portfolio == "p1") {
    table$var = <- someFunction()
  }
})

下面是使用mtcars的完整示例

library(shiny)

ui <- fluidPage(
  selectInput("make","Make:", choices = rownames(mtcars)),
  tableOutput("subtable")
)

server <- function(input, output, session) {
  subtable <- reactiveValues(var=NULL)
  
  observeEvent(input$make, {
    makes <- rownames(mtcars)
    subtable$var <- dplyr::filter(cbind(makes,mtcars), makes == input$make)
  })
  
  output$subtable <- renderTable(subtable$var)
}

shinyApp(ui, server)

相关问题