Reputation: 45
Is it possible to read the text content of a verbatimTextOutput
element within a Shiny app as a variable?
The idea of the sample app below is that when the user clicks button1
, the content of text2
gets updated and shows whatever is in text1
("Hello world" in this case).
The observeEvent
handler below generates this error : Warning: Error in $.shinyoutput: Reading objects from shinyoutput object not allowed.
but this looked to me the closest I managed to get to a solution.
Any help would be appreciated.
Thanks,
Hugo
library(shiny)
shinyApp(
ui = basicPage(verbatimTextOutput("text1"),
actionButton("button1", "Read & copy text!"),
verbatimTextOutput("text2")),
server = function(input, output, session) {
output$text1 <- renderText("Hello world")
output$text2 <- renderText("Waiting for new text...")
observeEvent(input$button1, {
output$text2 <- output$text1
})
}
)
Upvotes: 0
Views: 4671
Reputation: 286
If you want to read a text on any tag with an id and save it in an input. You can use the pakage shinyjs and some js code to read the text and then use Shiny.onInputChange ()
to create the input. The code below solves your problem
library(shiny)
library(shinyjs)
shinyApp(
ui = basicPage(useShinyjs(),verbatimTextOutput("text1"),
actionButton("button1", "Read & copy text!"),
verbatimTextOutput("text2")),
server = function(input, output, session) {
output$text1 <- renderText("Hello world")
output$text2 <- renderText("Waiting for new text...")
observeEvent(input$button1, {
runjs("var textin1=$('#text1').text();
Shiny.onInputChange('textin1', textin1);
")
output$text2 <- renderText({input$textin1})
})
}
)
Upvotes: 0
Reputation: 469
library(shiny)
shinyApp(
ui = basicPage(textInput("text1","","",placeholder = "Hello World"),
actionButton("button1", "Read & copy text!"),
verbatimTextOutput("text2")),
server = function(input, output, session) {
text2 <- eventReactive(input$button1,{
input$text1
})
output$text2 <- renderText({
validate(
need(input$button1,"Waiting for new text....")
)
text2()
}
)
}
)
But if you want exactly the same way as you did. So, here it is.
library(shiny)
shinyApp(
ui = basicPage(verbatimTextOutput("text1"),
actionButton("button1", "Read & copy text!"),
verbatimTextOutput("text2")),
server = function(input, output, session) {
output$text1 <- renderText("Hello world")
output$text2 <- renderText({
validate(
need(input$button1,"Waiting for new text...")
)
"Hello world"
})
}
)
Upvotes: 0
Reputation: 711
You can store the output of the text1
object to a variable and pass that variable. I would suggest that you use the session$userData
object because it makes a part of your shiny session and you dont have to declare the variable globally. Below is the code
library(shiny)
shinyApp(
ui = basicPage(verbatimTextOutput("text1"),
actionButton("button1", "Read & copy text!"),
verbatimTextOutput("text2")),
server = function(input, output, session) {
output$text1 <- renderText({
session$userData$text1 <- "Hello world"
session$userData$text1})
output$text2 <- renderText("Waiting for new text...")
observeEvent(input$button1, {
output$text2 <- renderText(session$userData$text1)
})
}
)
Upvotes: 1