首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用shinyjs隐藏/显示ui元素

使用shinyjs隐藏/显示ui元素
EN

Stack Overflow用户
提问于 2018-07-13 21:39:21
回答 1查看 1.7K关注 0票数 2

我一直在开发一个闪闪发亮的应用程序,它运行得很好(参见https://ostaski.shinyapps.io/NextWordPredictR/)。

以下是相关代码:

代码语言:javascript
复制
    # ui.R
library(shiny)
library(shinyjs)

shinyUI(fluidPage(
  useShinyjs(),  # Set up shinyjs
  titlePanel("Next Word PredictR"),
  sidebarLayout(
    sidebarPanel("Top Next Words",
                 br(" "),
                 shinyjs::hidden # this hides topWords, but toggleState in server.R doesn't toggle!?!
                 (
                     tableOutput('topWords')
                 )
    ),

    mainPanel("Enter word(s) in the box below or click a button", # this works
    br(" "),
      tags$head(
        tags$style(HTML("
                          #button1, #button2, #button3, #button4
                          {
                            color: #FFFFFF;
                            font-weight: bold;
                            background-color: #00AEAE;
                            border-color: #FFFFFF;
                          }
                          #text
                          {
                            width: 82%;
                          }
                        "))
        ),
      tags$div(
          tags$textarea(id = 'text', label = 'Enter word(s) in the box below or click a button', rows = 2, class='form-control', "")), # label is not working
          br(" "),
          htmlOutput("firstWord", inline = T),
          htmlOutput("secondWord", inline = T),
          htmlOutput("thirdWord", inline = T),
          htmlOutput("fourthWord", inline = T),
          br(" "),
          p('Below are five sentences drawn from the English news corpus you can copy/paste into the box above. HINT: do not copy/paste the ellipses (...) or the next words (in bold).'),
          HTML('Another strong month of hiring makes it less likely that the Federal Reserve will take additional steps to boost the economy at its meeting next ... <strong>week.</strong>'),
          HTML('<br /><br />When Junior walked into the memorial service Sunday, "it was a surprise to everyone," Doug Smith, Oceanside&#8217;s postmaster, told ... <strong>me.</strong>'),
          HTML('<br /><br />&quot;The Voice,&quot; NBC&#8217;s upstart singing competition, is back for its second season Sunday, and the network is kicking it off in prime-time style -- positioning it right after the Super ... <strong>Bowl.</strong>'),
          HTML('<br /><br />The main health issue that caused Meyer to resign at UF was a sick program he left on life ... <strong>support.</strong>'),
          HTML('<br /><br />Scarlett Johansson filmed scenes at an old warehouse on Ashland Road near Longfellow Avenue, off Cedar Road near the Norfolk Southern railroad ... <strong>tracks.</strong>'),
          HTML('<br /><br /><strong>DISCLAIMER:</strong>  Yes, of course I cherry-picked these sentences. The grand majority of sentences I tested failed miserably. &#9786;')
        )
      )
      )
    )
    # server.R
    library(shiny)
    library(shinyjs)
    library(tm)
    library(qdap)
    library(dplyr)

    allGrams <- readRDS("allGrams.rds")

    predict <- function (inputString, allGrams) 
    {
    ...
    }

    shinyServer(function(input, output, session) {

    observe({
    ...
            shinyjs::toggleState("topWords", !is.null(input$text) && input$text != "") # these don't work either
#            shinyjs::toggleState("topWords", is.null(input$text) || input$text == "")
    ...

       })
    })

当页面加载时,我试图隐藏顶部单词和按钮,然后在文本区域中输入单词时显示它们。shinyjs::hidden() in ui.R确实隐藏了测试中的顶级单词,但是服务器中的shinyjs::toggleState()没有在文本区域中输入一些文本时显示topwords。

我已经干了几天了,也许这是显而易见的.也许只是添加到shinyjs::toggleState()中的条件

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2018-07-15 19:43:15

我设法弄明白了。我没有在server.R中使用shinyjs::toggleState(),而是将其更改为shinyjs::toggle(),现在我得到了所需的效果。

不过,它似乎改变了预测,只增加了两个单词,而不是一个。

我会继续努力的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51333133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档