在Shiny/R中,当我点击图像链接(actionLink)时,应用程序崩溃。

huangapple go评论64阅读模式
英文:

In Shiny/R when I click on image link (actionLink) the app crashes

问题

我有这个应用程序:

以下是该模块的代码:

library(shiny)
library(shinyWidgets)
library(tidyverse)

years <- 2015:2022

list_actions <- list(
  letters[1:3],
  letters[4:6],
  letters[7:9],
  letters[10:12],
  letters[13:15],
  letters[16:18],
  letters[19:21],
  letters[22:24]
)

list_actions <- list_actions %>% set_names(years)

module_UI <- function(id) {
  ns <- NS(id)
  tagList(
    uiOutput(ns('letter_1'))
  )
}

module_server <- function(id, app_control_input) {
  moduleServer(
    id,
    function(input, output, session) {
      output$letter_1 <- renderUI({
        h1(app_control_input())
      })
    }
  )
}

这是应用程序的主要代码:

ui <- fluidPage(
  radioGroupButtons(
    inputId = 'seasons',
    direction = 'horizontal',
    justified = 'TRUE',
    size = 'sm',
    label = '',
    choices = sort(years, decreasing = TRUE),
    selected = '2022'
  ),

  htmlOutput('actions_icons'),

  module_UI('overview')
)

server <- function(input, output, session) {

  output$actions_icons <- renderUI({
    list_actions[[input$seasons]]  %>% 
      map(~ actionLink(label = .x, inputId = paste0(.x)))
  })

  letter <- reactive({list_actions[[input$seasons]][1]})

  letters %>% map( ~ observeEvent(input[[paste0(.x)]], {
    letter(.x)
  }))

  module_server('overview', app_control_input = letter)
}

shinyApp(ui, server)

当我点击年份时,显示的字母是正确的。所以,如果我点击 2015,则显示的字母是 a b c

我的问题是:当我点击字母时,应用程序崩溃。

例如,当我运行应用程序时,已经选择了 2022。因此,显示字母 v w x,并且字母 w 已经被选择。但当我点击 x 时,应用程序崩溃

我如何避免这种情况?

此外,非常重要的是,每次我选择(点击)年份时,第一个字母会在下方选择/显示,然后我将能够选择其他字母。

有任何帮助吗?

英文:

I have this app here:

Here is the code for the module:

library(shiny)
library(shinyWidgets)
library(tidyverse)

years &lt;- 2015:2022

list_actions &lt;- list(
  letters[1:3],
  letters[4:6],
  letters[7:9],
  letters[10:12],
  letters[13:15],
  letters[16:18],
  letters[19:21],
  letters[22:24]
)

list_actions &lt;- list_actions %&gt;% set_names(years)


module_UI &lt;- function(id) {
  ns &lt;- NS(id)
  tagList(

    uiOutput(ns(&#39;letter_1&#39;))

  )
}

module_server &lt;- function(id, app_control_input) {
  moduleServer(
    id,
    function(input, output, session) {

      output$letter_1 &lt;- renderUI({
        h1(app_control_input())
      })

    }
  )
}

This is the app's main code:

ui &lt;- fluidPage(
  radioGroupButtons(
    inputId = &#39;seasons&#39;,
    direction = &#39;horizontal&#39;,
    justified = &#39;TRUE&#39;,
    size = &#39;sm&#39;,
    label = &#39;&#39;,

    choices = sort(years,decreasing = TRUE),
    selected = &#39;2022&#39;
  ),

  htmlOutput(&#39;actions_icons&#39;),

  module_UI(&#39;overview&#39;)
)

server &lt;- function(input, output, session) {

  output$actions_icons &lt;- renderUI({

    list_actions[[input$seasons]]  %&gt;%
      map(~ actionLink(label = .x,
        inputId = paste0(.x)))

        })


  letter &lt;- reactive({list_actions[[input$seasons]][1]})

  letters %&gt;% map( ~ observeEvent(input[[paste0(.x)]], {
    letter(.x)
  }))

  module_server(&#39;overview&#39;, app_control_input = letter)

}

shinyApp(ui, server)

When I click over the years the letter that are showed to me are correctly. So, if I click on 2015 the letters showed are a b c

My problem is: when I click on the letters the app crashes.

For example when I run the app 2022 is already selected. So the letters v w xare displayed, and the letter w is already choose. But when I click on x the app crashes.

How can I avoid this?

Also, is very important that every time I select (click) the year, the first letter is selected/displayed bellow, and then I will be able to choose others letters as well.

Any help guys?

答案1

得分: 1

也许你正在寻找这个

server <- function(input, output, session) {
  my <- reactiveValues()
  output$actions_icons <- renderUI({
    
    list_actions[[input$seasons]]  %>% 
      map(~ actionLink(label = .x,
                       inputId = paste0(.x)))
    
  })
  
  observe({
    my$letter <- list_actions[[input$seasons]][1]
  })
  
  observeEvent(input$seasons, {
    my_list <- list_actions[[input$seasons]]
    n <- length(list_actions[[input$seasons]])
    lapply(1:n, function(i){
      observeEvent(input[[my_list[i]]], {
        my$letter <- list_actions[[input$seasons]][i]
        print(my$letter)
      })
    })
  })
  
  module_server('overview', app_control_input = reactive(my$letter))
  
}
英文:

Perhaps you are looking for this

server &lt;- function(input, output, session) {
  my &lt;- reactiveValues()
  output$actions_icons &lt;- renderUI({
    
    list_actions[[input$seasons]]  %&gt;%
      map(~ actionLink(label = .x,
                       inputId = paste0(.x)))
    
  })
  
  observe({
    my$letter &lt;- list_actions[[input$seasons]][1]
  })
  
  observeEvent(input$seasons, {
    my_list &lt;- list_actions[[input$seasons]]
    n &lt;- length(list_actions[[input$seasons]])
    lapply(1:n, function(i){
      observeEvent(input[[my_list[i]]], {
        my$letter &lt;- list_actions[[input$seasons]][i]
        print(my$letter)
      })
    })
  })
  
  module_server(&#39;overview&#39;, app_control_input = reactive(my$letter))
  
}

huangapple
  • 本文由 发表于 2023年3月4日 03:46:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631303.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定