英文:
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 <- 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())
})
}
)
}
This is the app's main code:
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)
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 x
are 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 <- 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))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论