如何在 selectizeInput 加载所有选择之前添加微调器? [闪亮的]

2024-04-25

我想制作一个应用程序 2actionButtons: 1) 在加载之前提交更改selectizeInput2)绘制绘图。

我知道如何添加spinner单击后actionButton但大多数情况是当你想展示情节时添加的。 但是,是否可以添加一个spinner不显示任何情节? 在这种特殊情况下,我想在单击“提交”后显示一个微调器,直到selectizeInput从“选择选项卡”加载。正如您所看到的我附加的示例,加载所有选项需要一些时间(因为文件有 25000 行)。

单击第二个后我已经有了一个微调器actionButton(显示情节)但我还需要一个。

我创建了一个示例,但由于某种原因,该图未显示在闪亮的应用程序中,而是出现在 R 的窗口中(我不知道为什么,但我添加该图只是为了向您展示如何放置第二个微调器。我想要一个类似的,但与第一个actionButton.).

library(shiny)
library(shinycssloaders)


ui <- fluidPage(

      titlePanel("My app"),
      
      sidebarLayout(
        sidebarPanel(
          tabsetPanel(
            
            tabPanel("Submit",
                     checkboxInput("log2", "Log2 transformation", value = FALSE),
                     actionButton("submit", "Submit")
            ),
      
      
            tabPanel("Selection",
                     br(),
                     selectizeInput(inputId = "numbers", label = "Choose one number:", choices=character(0)),
                     actionButton("show_plot", "Show the plot")
            ))
    ),
    mainPanel(
      conditionalPanel(
        condition = "input.show_plot > 0",
        style = "display: none;",
        withSpinner( plotOutput("hist"),
                    type = 5, color = "#0dc5c1", size = 1))

    )
  )
)

server <- function(input, output, session) {
  
  data <- reactive({
    data = read.csv("https://people.sc.fsu.edu/~jburkardt/data/csv/hw_25000.csv")
    data[,1] <- as.character(data[,1])
    
    if(input$log2 == TRUE){
      cols <- sapply(data, is.numeric)
      data[cols] <- lapply(data[cols], function(x) log2(x+1))
    }

    return(data)
  })
  
  mylist <- reactive({
    req(data())
    data <- data()
    data <- data[,1]
    return(data)
  })
  
  # This is to generate the choices (gene list) depending on the user's input.
  observeEvent(input$submit, {
    updateSelectizeInput(
      session = session, 
      inputId = "numbers", 
      choices = mylist(), options=list(maxOptions = length(mylist()))
    )
  })
  
  v <- reactiveValues()
  observeEvent(input$show_plot, {
    data <- data()
    v$plot <- plot(x=data[,1], y=data[,2])
  })
  
  
  # If the user didn't choose to see the plot, it won't appear.
  output$hist <- renderPlot({
    req(data())
    if (is.null(v$plot)) return()
    
    if(input$show_plot > 0){
      v$plot
    }

  })
}

请问有人知道如何帮助我吗?

非常感谢


这有点棘手。

首先我会更新selectizeInput在服务器端,如警告所示:

警告:选择输入“数字”包含大量 选项;考虑使用服务器端 selectize 来大幅改进 表现。请参阅 ?selectizeInput 帮助的详细信息部分 话题。

此外我切换到ggplot2关于plotOutput- 请参见这个相关帖子 https://stackoverflow.com/questions/29583849/save-a-plot-in-an-object.

显示微调器selectizeInput在更新选择时我们需要知道更新需要多长时间。这些信息可以通过收集Shiny的JS事件 https://shiny.rstudio.com/articles/js-events.html- 另请参阅本文 https://shiny.rstudio.com/articles/communicating-with-js.html.

最后,我们可以显示不存在的输出的微调器,因此我们能够控制微调器的显示时间(请参阅uiOutput("dummyid")):

library(shiny)
library(shinycssloaders)
library(ggplot2)

ui <- fluidPage(
  titlePanel("My app"),
  tags$script(HTML(
    "
     $(document).on('shiny:inputchanged', function(event) {
       if (event.target.id === 'numbers') {
         Shiny.setInputValue('selectizeupdate', true, {priority: 'event'});
       }
     });
     $(document).on('shiny:updateinput', function(event) {
       if (event.target.id === 'numbers') {
         Shiny.setInputValue('selectizeupdate', false, {priority: 'event'});
       }
     });
    
    "
  )),
  sidebarLayout(
    sidebarPanel(
      tabsetPanel(
        tabPanel("Submit",
                 checkboxInput("log2", "Log2 transformation", value = FALSE),
                 actionButton("submit", "Submit")
        ),
        tabPanel("Selection",
                 br(),
                 selectizeInput(inputId = "numbers", label = "Choose one number:", choices=NULL),
                 actionButton("show_plot", "Show the plot")
        ))
    ),
    mainPanel(
      uiOutput("plotProxy")
    )
  )
)

server <- function(input, output, session) {
  
  previousEvent <- reactiveVal(FALSE)
  choicesReady <- reactiveVal(FALSE)
  submittingData <- reactiveVal(FALSE)
  
  observeEvent(input$selectizeupdate, {
    if(previousEvent() && input$selectizeupdate){
      choicesReady(TRUE)
      submittingData(FALSE)
    } else {
      choicesReady(FALSE)
    }
    previousEvent(input$selectizeupdate)
  })
  
  data <- reactive({
    data = read.csv("https://people.sc.fsu.edu/~jburkardt/data/csv/hw_25000.csv")
    
    if(input$log2 == TRUE){
      cols <- sapply(data, is.numeric)
      data[cols] <- lapply(data[cols], function(x) log2(x+1))
    }
    return(data)
  })
  
  mylist <- reactive({
    req(data()[,1])
  })
  
  observeEvent(input$submit, {
    submittingData(TRUE)
    reactivePlotObject(NULL) # reset
    updateSelectizeInput(
      session = session, 
      inputId = "numbers", 
      choices = mylist(), options=list(maxOptions = length(mylist())),
      server = TRUE
    )
  })
  
  reactivePlotObject <- reactiveVal(NULL)
  observeEvent(input$show_plot, {
    reactivePlotObject(ggplot(data(), aes_string(x = names(data())[1], y = names(data())[2])) + geom_point())
  })
  
  output$hist <- renderPlot({
    reactivePlotObject()
  })
  
  output$plotProxy <- renderUI({
    if(submittingData() && !choicesReady()){
      withSpinner(uiOutput("dummyid"), type = 5, color = "#0dc5c1", size = 1)
    } else {
      conditionalPanel(condition = "input.show_plot > 0", withSpinner(plotOutput("hist"), type = 5, color = "#0dc5c1", size = 1), style = "display: none;")
    }
  })
}

shinyApp(ui, server)

示例数据的前 100 行(dput(head(data, 100))- 您的链接有一天可能会离线):

structure(list(Index = 1:100, Height.Inches. = c(65.78331, 71.51521, 
69.39874, 68.2166, 67.78781, 68.69784, 69.80204, 70.01472, 67.90265, 
66.78236, 66.48769, 67.62333, 68.30248, 67.11656, 68.27967, 71.0916, 
66.461, 68.64927, 71.23033, 67.13118, 67.83379, 68.87881, 63.48115, 
68.42187, 67.62804, 67.20864, 70.84235, 67.49434, 66.53401, 65.44098, 
69.5233, 65.8132, 67.8163, 70.59505, 71.80484, 69.20613, 66.80368, 
67.65893, 67.80701, 64.04535, 68.57463, 65.18357, 69.65814, 67.96731, 
65.98088, 68.67249, 66.88088, 67.69868, 69.82117, 69.08817, 69.91479, 
67.33182, 70.26939, 69.10344, 65.38356, 70.18447, 70.40617, 66.54376, 
66.36418, 67.537, 66.50418, 68.99958, 68.30355, 67.01255, 70.80592, 
68.21951, 69.05914, 67.73103, 67.21568, 67.36763, 65.27033, 70.84278, 
69.92442, 64.28508, 68.2452, 66.35708, 68.36275, 65.4769, 69.71947, 
67.72554, 68.63941, 66.78405, 70.05147, 66.27848, 69.20198, 69.13481, 
67.36436, 70.09297, 70.1766, 68.22556, 68.12932, 70.24256, 71.48752, 
69.20477, 70.06306, 70.55703, 66.28644, 63.42577, 66.76711, 68.88741
), Weight.Pounds. = c(112.9925, 136.4873, 153.0269, 142.3354, 
144.2971, 123.3024, 141.4947, 136.4623, 112.3723, 120.6672, 127.4516, 
114.143, 125.6107, 122.4618, 116.0866, 139.9975, 129.5023, 142.9733, 
137.9025, 124.0449, 141.2807, 143.5392, 97.90191, 129.5027, 141.8501, 
129.7244, 142.4235, 131.5502, 108.3324, 113.8922, 103.3016, 120.7536, 
125.7886, 136.2225, 140.1015, 128.7487, 141.7994, 121.2319, 131.3478, 
106.7115, 124.3598, 124.8591, 139.6711, 137.3696, 106.4499, 128.7639, 
145.6837, 116.819, 143.6215, 134.9325, 147.0219, 126.3285, 125.4839, 
115.7084, 123.4892, 147.8926, 155.8987, 128.0742, 119.3701, 133.8148, 
128.7325, 137.5453, 129.7604, 128.824, 135.3165, 109.6113, 142.4684, 
132.749, 103.5275, 124.7299, 129.3137, 134.0175, 140.3969, 102.8351, 
128.5214, 120.2991, 138.6036, 132.9574, 115.6233, 122.524, 134.6254, 
121.8986, 155.3767, 128.9418, 129.1013, 139.4733, 140.8901, 131.5916, 
121.1232, 131.5127, 136.5479, 141.4896, 140.6104, 112.1413, 133.457, 
131.8001, 120.0285, 123.0972, 128.1432, 115.4759)), row.names = c(NA, 
100L), class = "data.frame")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 selectizeInput 加载所有选择之前添加微调器? [闪亮的] 的相关文章

  • R 的 dplyr 切片中的奇怪行为

    打电话时slice df i 在 R 的 dplyr 包中 如果我要求的行索引不存在 nrows lt i 它似乎返回除组中的第一行之外的所有行 就像我调用的那样slice df 1 例如 library dplyr c1 lt c a b
  • 使 matplotlib 图形默认看起来像 R?

    Is there a way to make matplotlib behave identically to R or almost like R in terms of plotting defaults For example R t
  • 以计数矩阵作为响应的多项式

    根据帮助multinom 包裹nnet 响应应该是一个因子或具有 K 列的矩阵 它将被解释为每个 K 类的计数 我尝试在第二种情况下使用此函数 但出现错误 这是我所做的示例代码 response lt matrix round runif
  • R igraph - 保存布局?

    我想知道是否可以 保存 igraph 网络的布局 以便其他人能够重现相同的图表 目前 Fruchterman Reingold 算法总是创建一个新的网络 par mfrow c 1 2 g lt erdos renyi game 100 1
  • 创建后修改 ggplot 对象

    有没有首选的修改方式ggplot创建后的对象 例如 我建议我的学生将 r 对象与 pdf 文件一起保存以供以后更改 library ggplot2 graph lt ggplot mtcars aes x mpg y qsec fill c
  • R 中的 Mapdeck 包 - add_grid 似乎未渲染任何内容

    Problem The add gridR 中的函数mapdeck包很精彩 然而 遵循CRAN 文档 https cran r project org web packages mapdeck mapdeck pdf 我似乎无法获得任何数据
  • R中的等值线绘图问题

    编辑 我已经意识到问题的根源 我只有我有数据的县的计数信息 该信息少于我正在绘制的区域中的县数量 按理说 问题代码行在这里 mapnames lt map county plot FALSE 4 names colorsmatched lt
  • 粘贴两个 data.table 列

    dt lt data table L 1 5 A letters 7 11 B letters 12 16 L A B 1 1 g l 2 2 h m 3 3 i n 4 4 j o 5 5 k p 现在我想粘贴列 A 和 B 以获得一个新
  • 如何在knitr中安装软件包?

    到目前为止 我一直在使用这段代码来加载 R 包并编写 R 文件 但我正在尝试使用knitr rm list ls all TRUE kpacks lt c ggplot2 install github devtools mapdata ne
  • plot xts if (on == "years") { 中的错误:缺少 TRUE/FALSE 需要的值

    我正在尝试绘制 xts 对象 但出现有关年份的错误 xts 对象只有一个数值和一个 POSIXct 索引 下面的代码显示了 xts 和尝试绘图时的错误 关于需要对 xts 对象做什么才能正确绘制的任何想法 xTest lt as xts 3
  • svyby比例的置信区间

    是否存在创建置信区间的现有函数 从一个svyby比例对象 在我的例子中 是一个二进制项目的交叉表survey包裹 我经常比较各组之间的比例 如果有一个可以提取置信区间的函数 使用调查函数svyciprop而不是confint 下面的示例显示
  • 如何缩放(标准化)每列内的 ggplot2 stat_bin2d 值(按 X 轴)

    我有一个 ggplot stat bin2d 热图 library ggplot2 value lt rep 1 5 1000 df lt as data frame value df group lt rep 1 7 len 5000 d
  • dplyr 总结小计

    Excel 中数据透视表的一大优点是它们会自动提供小计 首先 我想知道 dplyr 中是否已经创建了任何可以实现此目的的东西 如果没有 实现它的最简单方法是什么 在下面的示例中 我按气缸和化油器的数量显示了平均排量 对于每组气缸 4 6 8
  • 删除绘图轴值

    我只是想知道是否有一种方法可以消除 r 图中的轴值 分别是 x 轴或 y 轴 我知道axes false将摆脱整个轴 但我只想摆脱编号 删除 x 轴或 y 轴上的编号 plot 1 10 xaxt n plot 1 10 yaxt n 如果
  • R 编程中的字符串分割

    目前 下面的脚本将组合的项目代码拆分为特定的项目代码 rule2 lt c MR df 1 lt test grep paste rule2 sep collapse test Name y SpaceName 1 lt function
  • 在 R 中捕获段错误

    我得到了caught segfault每次我尝试从以下位置运行任何绘图函数时都会出错ggplot2包 1 0 0 我已经尝试过这个qplot geom dotplot geom histogram等来自包的数据 例如diamonds or
  • 枚举所有可能的二元组星座

    我正在寻找一种方法来枚举 n 个成员的所有可能的两人组星座 例如 对于 n 4 个成员 以下 3 个独特的组星座是可能的 请注意 组内成员的顺序和组顺序都不重要 1 2 3 4 1 3 2 4 1 4 2 3 例如 对于 n 6 个成员 可
  • R 中的输出,避免写“[1]”

    I use print从 R 中的函数输出 例如 print blah blah blah 这输出 1 blah blah blah 到控制台 我怎样才能避免 1 和引号 Use cat Your string type cat查看帮助页面
  • 读取多个 CSV 文件,并在文件开头跳过不同行数

    我必须阅读大约 300 个单独的 CSV 我已经成功使用循环和结构化 CSV 名称来自动化该过程 然而 每个 CSV 在开头都有 14 17 行垃圾 并且随机变化 因此在 read table 命令中硬编码 skip 参数将不起作用 每个
  • R 在读取文件时添加额外的数字

    我一直在尝试读取一个包含日期字段和数字字段的文件 我的数据在 Excel 工作表中 如下所示 Date X 1 25 2008 0 0023456 12 23 2008 0 001987 当我在 R 中使用readxl read xlsx函

随机推荐