R twitterwidget 包 - 更新闪亮输入时未清除推文
R twitterwidget package - tweet not cleared when shiny input is updated
每次我对 renderTwitterwidget()
的输入发生变化时,都会在 Shiny UI 上的原始推文下方打印一条新推文。是否可以在每次将新的 twitterwidget
对象传递给 renderTwitterwidget
时清除旧推文并打印一条新推文?
代码无法使用 RStudio 查看器正确呈现内容,因此请使用在浏览器中查看应用程序来检查结果。
这是闪亮应用的代码:
# Libraries ---------------------------------------------------------------
library(shiny)
library(twitterwidget)
library(tidyverse)
library(rtweet)
# UI ----------------------------------------------------------------------
ui <- fluidPage(
# Input functions
textInput(inputId = 'search_term',
label = 'Search tweets from the last 6-9 days:',
#value = 'covid',
placeholder = '#covid-19',
width = '90%'
),
actionButton(inputId = 'submit_button',
label = 'Submit'
),
# Output functions
twitterwidgetOutput('twitter', width = "100%", height = "400px")
)
# Server ------------------------------------------------------------------
server <- function(input, output) {
# Get data
tweet_id <- eventReactive(input$submit_button, {search_tweets(q = input$search_term, n = 1, lang = 'en') %>% pull(status_id)})
# Twitter widget
output$twitter <- renderTwitterwidget(twitterwidget(tweet_id()))
}
shinyApp(ui, server)
没有 Twitter 帐户的代码:
# Libraries ---------------------------------------------------------------
library(shiny)
library(twitterwidget)
library(tidyverse)
# Vector of tweet status_id's ---------------------------------------------
v_ids <- c("1434550715905724416", "1434404394133594112", "1434270392533889039", "1432293982541946884", "1432396306698354688",
"1434187565633003526", "1433095963338551297", "1432339458309955585", "1431909314600574977","1432064144040202241",
"1434268168231477248", "1431912031100227589", "1434188285103022086", "1432066801588920325", "1434184994579230720",
"1434100772069908485", "1433887544274563075", "1433801716202344460", "1433799487592468492", "1433754869429592078",
"1433120347176386561", "1432410673259057161", "1433115345938784263", "1433093342456389639", "1432961860731478017",
"1432828362280476673", "1432812324709179394", "1432806681793159168", "1432721292403281920", "1432707208211537930",
"1432700115299586053", "1432658875665289216", "1432623112747638785", "1432614035141312513", "1432612768717451266")
# UI ----------------------------------------------------------------------
ui <- fluidPage(
# Input functions
textInput(inputId = 'search_term',
label = 'Search tweets from the last 6-9 days:',
#value = 'covid',
placeholder = '#covid-19',
width = '90%'
),
actionButton(inputId = 'submit_button',
label = 'Submit'
),
# Output functions
twitterwidgetOutput('twitter', width = "100%", height = "400px")
)
# Server ------------------------------------------------------------------
server <- function(input, output) {
# Get data
tweet_id <- eventReactive(input$submit_button, {sample(v_ids, 1)})
# Twitter widget
output$twitter <- renderTwitterwidget(twitterwidget(tweet_id()))
}
shinyApp(ui, server)
奇怪的功能,不确定是软件包问题还是什么。您也可以只使用 shinyjs
并编写一个用户定义的 javascript
函数来清除 div
.
的内容
下面是一个应该有效的简短实现:
# Libraries ---------------------------------------------------------------
library(shiny)
library(twitterwidget)
library(tidyverse)
library(rtweet)
# UI ----------------------------------------------------------------------
ui <- fluidPage(
shinyjs::useShinyjs(),
shinyjs::extendShinyjs(
text = "shinyjs.refresh = function() {
var div = document.getElementById('twitter_output');
while(div.firstChild){
div.removeChild(div.firstChild);
}
}",
functions = c("refresh")
),
# Input functions
textInput(
inputId = 'search_term',
label = 'Search tweets from the last 6-9 days:',
#value = 'covid',
placeholder = '#covid-19',
width = '90%'
),
actionButton(inputId = 'submit_button',
label = 'Submit'),
twitterwidgetOutput("twitter_output")
)
# Server ------------------------------------------------------------------
server <- function(input, output) {
observeEvent(input$submit_button, {
# Obtain tweet ID
tweet_id <-
search_tweets(q = input$search_term,
n = 1,
lang = 'en') %>%
pull(status_id)
# Refresh the `twitter_output` div
shinyjs::js$refresh()
# Render the tweet again.
output$twitter_output <-
renderTwitterwidget(twitterwidget(tweet_id))
})
}
shinyApp(ui, server)
每次我对 renderTwitterwidget()
的输入发生变化时,都会在 Shiny UI 上的原始推文下方打印一条新推文。是否可以在每次将新的 twitterwidget
对象传递给 renderTwitterwidget
时清除旧推文并打印一条新推文?
代码无法使用 RStudio 查看器正确呈现内容,因此请使用在浏览器中查看应用程序来检查结果。
这是闪亮应用的代码:
# Libraries ---------------------------------------------------------------
library(shiny)
library(twitterwidget)
library(tidyverse)
library(rtweet)
# UI ----------------------------------------------------------------------
ui <- fluidPage(
# Input functions
textInput(inputId = 'search_term',
label = 'Search tweets from the last 6-9 days:',
#value = 'covid',
placeholder = '#covid-19',
width = '90%'
),
actionButton(inputId = 'submit_button',
label = 'Submit'
),
# Output functions
twitterwidgetOutput('twitter', width = "100%", height = "400px")
)
# Server ------------------------------------------------------------------
server <- function(input, output) {
# Get data
tweet_id <- eventReactive(input$submit_button, {search_tweets(q = input$search_term, n = 1, lang = 'en') %>% pull(status_id)})
# Twitter widget
output$twitter <- renderTwitterwidget(twitterwidget(tweet_id()))
}
shinyApp(ui, server)
没有 Twitter 帐户的代码:
# Libraries ---------------------------------------------------------------
library(shiny)
library(twitterwidget)
library(tidyverse)
# Vector of tweet status_id's ---------------------------------------------
v_ids <- c("1434550715905724416", "1434404394133594112", "1434270392533889039", "1432293982541946884", "1432396306698354688",
"1434187565633003526", "1433095963338551297", "1432339458309955585", "1431909314600574977","1432064144040202241",
"1434268168231477248", "1431912031100227589", "1434188285103022086", "1432066801588920325", "1434184994579230720",
"1434100772069908485", "1433887544274563075", "1433801716202344460", "1433799487592468492", "1433754869429592078",
"1433120347176386561", "1432410673259057161", "1433115345938784263", "1433093342456389639", "1432961860731478017",
"1432828362280476673", "1432812324709179394", "1432806681793159168", "1432721292403281920", "1432707208211537930",
"1432700115299586053", "1432658875665289216", "1432623112747638785", "1432614035141312513", "1432612768717451266")
# UI ----------------------------------------------------------------------
ui <- fluidPage(
# Input functions
textInput(inputId = 'search_term',
label = 'Search tweets from the last 6-9 days:',
#value = 'covid',
placeholder = '#covid-19',
width = '90%'
),
actionButton(inputId = 'submit_button',
label = 'Submit'
),
# Output functions
twitterwidgetOutput('twitter', width = "100%", height = "400px")
)
# Server ------------------------------------------------------------------
server <- function(input, output) {
# Get data
tweet_id <- eventReactive(input$submit_button, {sample(v_ids, 1)})
# Twitter widget
output$twitter <- renderTwitterwidget(twitterwidget(tweet_id()))
}
shinyApp(ui, server)
奇怪的功能,不确定是软件包问题还是什么。您也可以只使用 shinyjs
并编写一个用户定义的 javascript
函数来清除 div
.
下面是一个应该有效的简短实现:
# Libraries ---------------------------------------------------------------
library(shiny)
library(twitterwidget)
library(tidyverse)
library(rtweet)
# UI ----------------------------------------------------------------------
ui <- fluidPage(
shinyjs::useShinyjs(),
shinyjs::extendShinyjs(
text = "shinyjs.refresh = function() {
var div = document.getElementById('twitter_output');
while(div.firstChild){
div.removeChild(div.firstChild);
}
}",
functions = c("refresh")
),
# Input functions
textInput(
inputId = 'search_term',
label = 'Search tweets from the last 6-9 days:',
#value = 'covid',
placeholder = '#covid-19',
width = '90%'
),
actionButton(inputId = 'submit_button',
label = 'Submit'),
twitterwidgetOutput("twitter_output")
)
# Server ------------------------------------------------------------------
server <- function(input, output) {
observeEvent(input$submit_button, {
# Obtain tweet ID
tweet_id <-
search_tweets(q = input$search_term,
n = 1,
lang = 'en') %>%
pull(status_id)
# Refresh the `twitter_output` div
shinyjs::js$refresh()
# Render the tweet again.
output$twitter_output <-
renderTwitterwidget(twitterwidget(tweet_id))
})
}
shinyApp(ui, server)