代码在下面.

预期的行为如下:

  1. 用户点击动作按钮run_analysis.
  2. 这触发了input$run_analysisobserveEvent块,该块将无功值run_analysis_clicked更新为TRUE.
  3. 该react 值的变化then应该触发执行observe块,这将更新mainPanelTabs选项卡集中的所选选项卡.

但事实并非如此.由于某些原因,observe块在改变run_analysis_clicked值后没有被触发.

我试着把这个和最低可重复性的样本配对.

library(shiny)

#### USER INTERFACE ####
ui <- fluidPage(
    titlePanel("Bootstrap Sample Size Simulator"),
    sidebarLayout(
        sidebarPanel(
            # Define the primary inputs needed to setup the sample size simulation
            fileInput("file_upload", "Upload a CSV file with sample data to use for sample size optimization:", accept = ".csv"),
            uiOutput("slider_output"),
            uiOutput("action_output")
        ),
        mainPanel(
            tabsetPanel(
                id = "mainPanelTabs",  # Add an ID for easier reference
                tabPanel("instructions", "Instructions content goes here."),
                tabPanel("data", "Data content goes here."),
                tabPanel("results", "Results content goes goes here.")
            )
        )
    )
)

#### SERVER ####
server <- function(input, output, session) {
    # Define reactive values that populate when the file is uploaded
    upload <- reactiveValues(
        isUploaded = FALSE, # flag signifying the file has been uploaded
        data = NULL, # empty placeholder for the data
        sample_max = NULL # initialize as empty
    )
    
    # Define reactive value that identifies when the run_analysis button has been clicked
    run_analysis_clicked <- reactive({
        FALSE # Flag is initially set to FALSE
    })
    
    # Define reactive value that updates the selected tab
    selectedTab <- reactiveVal("instructions")
    
    # Define function for creating the slider UI element for sample size limits selection
    slider_ui <- function() {
        if (upload$isUploaded) {
            sliderInput("sample_range",
                        label = "Range of Sample Sizes to simulate from the uploaded data:",
                        min = 2,
                        max = upload$sample_max,
                        step = 1,
                        value = c(2, upload$sample_max)
            )
        } else {
            NULL
        }
    }
    
    # Define function for creating the run analysis button
    action_ui <- function() {
        if (upload$isUploaded) {
            actionButton("run_analysis",
                         label = "Run the analysis"
            )
        } else {
            NULL
        }
    }
    
    # Update reactive values when file is uploaded
    observeEvent(input$file_upload, {
        upload$isUploaded <- TRUE  # update flag after file is uploaded
        upload$data <- read.csv(input$file_upload$datapath) # read the uploaded CSV file
        upload$sample_max <- nrow(upload$data) # update sample_max after file has been 
        print(paste("upload$sample_max:",upload$sample_max))
    })
    
    # Update reactive values when button is clicked
    observeEvent(input$run_analysis, {
                 run_analysis_clicked <- TRUE # update flag after button clicked
                 print(paste("run_analysis_clicked",run_analysis_clicked))
                 })
    
    # Update selected tab based on upload and button click events
    observe({
        print("observe triggered")
        print(paste("upload$isUploaded:",upload$isUploaded))
        print(paste("run_analysis_clicked:",run_analysis_clicked()))
        if (!upload$isUploaded) {
            selectedTab("instructions")
        } else if (run_analysis_clicked() == FALSE) {
            selectedTab("data")
        } else {
            selectedTab("results")
        }
        
        # Update the selected tab directly in the UI
        updateTabsetPanel(session, "mainPanelTabs", selected = selectedTab())
    })
    
    # Render the slider and action buttons conditionally upon file upload
    output$slider_output <- renderUI({
        slider_ui()
    })
    output$action_output <- renderUI({
        action_ui()
    })
}

#### APP ####
shinyApp(ui = ui, server = server)

下面是控制台输出(添加了解释触发输出的操作的注释):

Listening on http://xxxxxx
# App Initialized: 
[1] "observe triggered"
[1] "upload$isUploaded: FALSE"
[1] "run_analysis_clicked: FALSE"
# File Uploaded:
[1] "upload$sample_max: 15"
[1] "observe triggered"
[1] "upload$isUploaded: TRUE"
[1] "run_analysis_clicked: FALSE"
# Button Clicked:
[1] "run_analysis_clicked TRUE"

对于文件上传,我只是使用一个CSV文件,其中包含了我在Excel中创建的15个随机数字.它看起来是这样的:

Sample Data
0.590024857449706
0.0728674995484038
0.481874325987865
0.960001135837481
0.294927454278591
0.25254752567793
0.460322873384411
0.00237222444026342
0.169595016393134
0.444750644528156
0.033684417887163
0.973733565927954
0.917744500279373
0.264506821656346
0.998370147928976

推荐答案

这里需要理解的一个主要重要的事情是,

run_analysis_clicked <- reactive({
        FALSE # Flag is initially set to FALSE
    })

它不定义一个react 式值,它定义了一个react 式表达式.如果将变量定义为reactive,则不可能在其react 环境之外更改其值.你可以想象,在reactive上你只有"读"权限,而在reactiveVal上你有"读"和"写"权限.

由于您将reactive定义为FALSE,因此在应用程序中任何地方都无法观察到该变量的变化.如何实现这一点的一种可能性是定义对reactiveVal的依赖性,例如,

rv <- reactiveValues(blnRunAnalysisClicked = TRUE)
run_analysis_clicked <- reactive({rv$blnRunAnalysisClicked})

但在你的例子中,我们可以跳过reactive部分,直接依赖reactiveVal,例如,下面的例子(upload$run_analysis_clicked),这将起作用.

library(shiny)

#### USER INTERFACE ####
ui <- fluidPage(
  titlePanel("Bootstrap Sample Size Simulator"),
  sidebarLayout(
    sidebarPanel(
      # Define the primary inputs needed to setup the sample size simulation
      fileInput("file_upload", "Upload a CSV file with sample data to use for sample size optimization:", accept = ".csv"),
      uiOutput("slider_output"),
      uiOutput("action_output")
    ),
    mainPanel(
      tabsetPanel(
        id = "mainPanelTabs",  # Add an ID for easier reference
        tabPanel("instructions", "Instructions content goes here."),
        tabPanel("data", "Data content goes here."),
        tabPanel("results", "Results content goes goes here.")
      )
    )
  )
)

#### SERVER ####
server <- function(input, output, session) {
  # Define reactive values that populate when the file is uploaded
  upload <- reactiveValues(
    isUploaded = FALSE, # flag signifying the file has been uploaded
    data = NULL, # empty placeholder for the data
    sample_max = NULL, # initialize as empty
    run_analysis_clicked = FALSE 
  )
  
  # Define reactive value that updates the selected tab
  selectedTab <- reactiveVal("instructions")
  
  # Define function for creating the slider UI element for sample size limits selection
  slider_ui <- function() {
    if (upload$isUploaded) {
      sliderInput("sample_range",
                  label = "Range of Sample Sizes to simulate from the uploaded data:",
                  min = 2,
                  max = upload$sample_max,
                  step = 1,
                  value = c(2, upload$sample_max)
      )
    } else {
      NULL
    }
  }
  
  # Define function for creating the run analysis button
  action_ui <- function() {
    if (upload$isUploaded) {
      actionButton("run_analysis",
                   label = "Run the analysis"
      )
    } else {
      NULL
    }
  }
  
  # Update reactive values when file is uploaded
  observeEvent(input$file_upload, {
    upload$isUploaded <- TRUE  # update flag after file is uploaded
    upload$data <- read.csv(input$file_upload$datapath) # read the uploaded CSV file
    upload$sample_max <- nrow(upload$data) # update sample_max after file has been 
    print(paste("upload$sample_max:",upload$sample_max))
  })
  
  # Update reactive values when button is clicked
  observeEvent(input$run_analysis, {
    upload$run_analysis_clicked <- TRUE # update flag after button clicked
    print(paste("run_analysis_clicked", upload$run_analysis_clicked))
  })
  
  # Update selected tab based on upload and button click events
  observe({
    print("observe triggered")
    print(paste("upload$isUploaded:",upload$isUploaded))
    print(paste("run_analysis_clicked:",upload$run_analysis_clicked))
    if (!upload$isUploaded) {
      selectedTab("instructions")
    } else if (upload$run_analysis_clicked == FALSE) {
      selectedTab("data")
    } else {
      selectedTab("results")
    }
    
    # Update the selected tab directly in the UI
    updateTabsetPanel(session, "mainPanelTabs", selected = selectedTab())
  })
  
  # Render the slider and action buttons conditionally upon file upload
  output$slider_output <- renderUI({
    slider_ui()
  })
  output$action_output <- renderUI({
    action_ui()
  })
}

#### APP ####
shinyApp(ui = ui, server = server)

R相关问答推荐

将Multilinetring合并到一个线串中,使用sf生成规则间隔的点

更改绘图上的x轴断点,而不影响风险?

如何使用rmarkdown和kableExtra删除包含折叠行的表的第一列的名称

大规模重新标记haven标签数据

如果行和大于值,则过滤

ggplot2中的X轴显示数值,单位为百,而不是十

如何将旋转后的NetCDF转换回正常的纬度/经度网格,并使用R?

Rplotly中的Sankey Diagram:意外连接&

标识R中多个列中缺少的唯一值

有效识别长载体中的高/低命中

如何基于两个条件从一列中提取行

使用R中的dist()迭代ID匹配的欧几里德距离

减go R中列表的所有唯一元素对

为什么我对圆周率图的蒙特卡罗估计是空的?

为什么函数toTitleCase不能处理english(1),而toupper可以?

是否有可能从边界中找到一个点值?

如何构建一个for循环来循环处理动物ID?

R-如何在ggplot2中显示具有不同x轴值(日期)的多行?

使用LAG和dplyr执行计算,以便按行和按组迭代

在具有条件的循环中添加行