gpt4 book ai didi

r - Shiny 的应用程序 actionButton 单击页面加载

转载 作者:行者123 更新时间:2023-12-03 14:11:57 24 4
gpt4 key购买 nike

我正在使用 navbarPage() 类型构建一个 Shiny 应用程序。我有三个选项卡 - 初始选项卡有一个 textInput() 框,其中定义了默认文本。该页面的 mainPanel() 有一个直方图和一个表格。在页面上加载这些更新并在基于该默认文本启动应用程序时反射(reflect)正确的信息。

第二个选项卡应该显示基于该默认文本的 wordcloud。当我切换到该选项卡时出现错误 - 如果我返回第一个选项卡并输入新文本并点击 actionButton - wordcloud 将更新,但在我执行该操作之前不会更新。

有没有办法让actionButton()或者在页面加载时发生某种提交,以便带有 wordcloud 的选项卡可以更新?或者也许我只需要创建一个全局变量或其他东西。我不知道。我在这方面花了很多时间,但遇到了困难。任何帮助将不胜感激。

界面代码:

tabPanel("Word Cloud Diagram",
fluidRow(
sidebarPanel(
width = 3,
h5("The sentence input:"),
wellPanel(span(h5(textOutput(
'sent'
)), style = "color:red")),
sliderInput(
"maxWC",
h5("Maximum Number of Words:"),
min = 10,
max = 100,
value = 50
),
br(),
#actionButton("update", "Update Word Cloud"),
hr(),
helpText(h5("Help Instruction:")),
helpText(
"Please have a try to make the prediction by using
the dashboard on right side. Specifically, you can:"
),
helpText("1. Type your sentence in the text field", style =
"color:#428ee8"),
helpText(
"2. The value will be passed to the model while you are typing.",
style = "color:#428ee8"
),
helpText("3. Obtain the instant predictions below.", style =
"color:#428ee8"),
hr(),
helpText(h5("Note:")),
helpText(
"The App will be initialized at the first load.
After",
code("100% loading"),
", you will see the prediction
for the default sentence example \"Nice to meet you\"
on the right side."
)
),
mainPanel(
h3("Word Cloud Diagram"),
hr(),
h5(
"A",
code("word cloud"),
"or data cloud is a data display which uses font size and/
or color to indicate numerical values like frequency of words. Please click",
code("Update Word Cloud"),
"button and",
code("Slide Input"),
"in the side bar to update the plot for relevant prediction."
),
plotOutput("wordCloud"),
# wordcloud
br()
)
)),

服务器代码:
wordcloud_rep <- repeatable(wordcloud)
output$wordCloud <- renderPlot({
v <- terms()
wordcloud_rep(
v[, 2],
v[, 1],
max.words = input$maxWC,
scale = c(5, 1.5),
colors = brewer.pal(4, "Dark2")
)
})

另外,我正在使用单个文件应用程序“app.R” - 不确定这是否是有用的信息。同样,在第一个选项卡上,默认文本在第一页加载时显示,我只是希望在页面加载时将其扩展到 wordcloud,以便立即显示绘图,而无需输入和提交新文本。谢谢!

最佳答案

这是一个应该接近您想要的示例。诀窍是使用 submitButton . wordcloud 将具有基于初始输入的默认图,但会在您更改文本并按下提交按钮时更改。

library(shiny)
library(wordcloud)

ui <- shinyUI(fluidPage(

titlePanel("Old Faithful Geyser Data"),

sidebarLayout(
sidebarPanel(
textInput("text", "Input Text", "Random text random text random is no yes"),
submitButton("Submit")
),

mainPanel(
tabsetPanel(
tabPanel("Tab1",
plotOutput("hist"),
tableOutput("hist_table")),
tabPanel("Tab2",
plotOutput("wordcloud"))
)
)
)
))

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

observe({
word_list = strsplit(input$text, " ")
word_table = as.data.frame(table(word_list))

output$hist = renderPlot({
barplot(table(word_list))
})
output$hist_table = renderTable({
word_table
})
output$wordcloud = renderPlot({
wordcloud(word_table[,1], word_table[,2])
})
})

})

shinyApp(ui = ui, server = server)

关于r - Shiny 的应用程序 actionButton 单击页面加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36723660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com