我一直在开发一个闪亮的应用程序,并且运行正常(请参见https://ostaski.shinyapps.io/NextWordPredictR/)。
以下是相关代码:
# 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’s postmaster, told ... <strong>me.</strong>'),
HTML('<br /><br />"The Voice," NBC’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. ☺')
)
)
)
)
# 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 == "")
...
})
})
我正在尝试在页面加载时隐藏主题词和按钮,然后在将文字输入到文本区域时显示它们。 ui.R 中的shinyjs::hidden()
确实隐藏了测试中的热门单词,但 server.R 中的shinyjs::toggleState()
却没有显示热门单词文本区域中的文本。
我已经待了几天,所以也许这很明显……也许只是添加到shinyjs::toggleState()
上的条件?
谢谢!
答案 0 :(得分:1)
我设法弄清楚了。我没有在 server.R 中使用Shinyjs :: toggleState(),而是将其更改为Shinyjs :: toggle(),现在我得到了预期的效果。
不过,似乎增加了两个单词,而不只是一个单词。
我会继续努力。