Shinyjs hide/show功能异常求助:切换分析后旧文本重复显示
解决Shiny中shinyjs hide/show后旧内容重复显示的问题
我看了你的代码,问题出在两个核心点:一是每次点击vorschau时你会一次性显示所有输出组件,但不同分析方法只需要其中一部分;二是你的render函数在非对应方法时没有清空内容,加上clear按钮只是隐藏组件而非重置内容,导致切换方法时旧内容会再次出现。下面是针对性的解决方案:
核心思路
- 按需显示组件:根据选中的分析方法,只显示对应的输出组件,隐藏其余的
- 清空非对应输出:在每个render函数中添加分支,非当前方法时返回空内容
- 彻底重置clear逻辑:点击clear时不仅隐藏组件,还要清空所有输出内容
修改后的Server代码
# 请确保在UI的fluidPage中添加 shinyjs::useShinyjs(),否则shinyjs功能无法生效 observeEvent(input$clear,{ # 隐藏所有输出组件 shinyjs::hide("plot") shinyjs::hide("txt") shinyjs::hide("txt2") shinyjs::hide("txt3") shinyjs::hide("createvars") # 彻底重置所有输出内容为空白 output$plot <- renderPlot({NULL}) output$txt <- renderText({NULL}) output$txt2 <- renderTable({NULL}) output$txt3 <- renderText({NULL}) output$createvars <- renderUI({NULL}) }) observeEvent(input$vorschau,{ # 先隐藏所有组件,避免旧内容泄露 shinyjs::hide("plot") shinyjs::hide("txt") shinyjs::hide("txt2") shinyjs::hide("txt3") shinyjs::hide("createvars") # 根据选中的方法,只显示需要的组件 if(input$methods %in% c("box","balken","hist","streu","kreis")){ shinyjs::show("plot") shinyjs::show("createvars") } else if(input$methods == "mw"){ shinyjs::show("txt") shinyjs::show("createvars") } else if(input$methods == "anz"){ shinyjs::show("txt2") shinyjs::show("createvars") } else if(input$methods == "reg"){ shinyjs::show("plot") shinyjs::show("txt3") shinyjs::show("createvars") } # 渲染图表 output$plot <- renderPlot({ # 修正了你原来的括号错误:length(input$variables)==1 而非 length(input$variables==1) if(input$methods =="box"){ if(length(input$variables)==1){ boxplot(x = d()[input$variables],xlab=input$variables) } else { boxplot(x = d()[input$variables]) } } else if(input$methods =="balken"){ barplot(table(d()[input$variables]),xlab=input$variables) } else if(input$methods =="hist"){ hist(as.numeric(unlist(d()[input$variables])),xlab=input$variables, ylab="Anzahl", main="") } else if(input$methods =="streu"){ plot(d()[input$variables]) } else if(input$methods =="kreis"){ pie(table(d()[input$variables]),main = input$variables) } else if(input$methods =="reg"){ lmodel <- lm(as.numeric(unlist(d()[input$variables][2]))~as.numeric(unlist(d()[input$variables][1]))) plot(d()[input$variables],xlab = names(d()[input$variables][1]), ylab=names(d()[input$variables][2]), main= paste(names(d()[input$variables][2]),' =', round(coef(lmodel)[[2]],2), ' * ', names(d()[input$variables][1]), '+', round(coef(lmodel)[[1]],2))) abline(lmodel) } else { # 非图表类方法返回空 NULL } }) # 渲染算术平均值文本 output$txt <- renderText({ if(input$methods =="mw"){ paste0("Das arithmetische Mittel von ",input$variables," beträgt: ", round(mean(as.numeric(unlist(d()[input$variables]))),2)) } else { NULL } }) # 渲染频数表格 output$txt2 <- renderTable({ if(input$methods =="anz"){ t<-data.frame(table(d()[input$variables])) col_headings <- c(paste0(input$variables),"Freq") colnames(t)<-col_headings t } else { NULL } }) # 渲染回归提示文本 output$txt3 <- renderText({ if(input$methods =="reg"){ paste0("Geben Sie zuerst die Einflussgröße und dann die Zielgröße im Feld Variablenauswahl an.") } else { NULL } }) })
额外注意事项
- 确保你的UI部分已经添加了
shinyjs::useShinyjs()(一般放在fluidPage的开头),否则shinyjs的hide/show功能无法生效 - 我修正了你原来代码中的一个语法错误:
length(input$variables==1)应该是length(input$variables)==1,这个错误会导致箱线图的逻辑判断不正确
内容的提问来源于stack exchange,提问作者Marina Ar
相关产品推荐
相关产品推荐

