KittenBurr
KittenBurr

Reputation: 43

Use readlines(prompt = ) in Shiny

I have a code that takes inputs using the readlines(prompt = ) function. Could you tell me which input function in Shiny will be adequate to adapt this code to a Shiny app?

I need an interactive function, I can't use a simple input with selectInput() because I have a lot of readlines(prompt = ) statements.

Something similar to this question: Include interactive function in shiny to highlight "readlines" and "print"

Upvotes: 4

Views: 934

Answers (2)

GyD
GyD

Reputation: 4072

Florian's answer is nice and easy to use, I would definitely recommend that! But in case you are keen on using prompts for inputs I am adding another solution, using javaScript:

This one shows a prompt when the user presses an actionButton and stores it in an input variable. (it doesn't necessarily have to be after a button press)

library(shiny)

ui <- fluidPage(
  tags$head(tags$script("
    $(document).on('shiny:inputchanged', function(event) {
      if (event.name === 'go') {
        var text = prompt('Write me something nice:');
        Shiny.onInputChange('mytext', text);
      }
    });"
  )),
  actionButton("go", "Click for prompt"),
  textOutput("txt")
)

server <- function(input, output, session) {
  output$txt <- renderText( {
    input$mytext
  })
}

shinyApp(ui, server)

Upvotes: 2

Florian
Florian

Reputation: 25385

Maybe you could use textArea for this purpose. Working example below, hope this helps!


enter image description here


library(shiny)

ui <- fluidPage(
  tags$textarea(id="text", rows=4, cols=40),
  htmlOutput('val')
)

server <- function(input,output)
{
  output$val <- renderText({
    text = gsub('\n','<br>',input$text)
    text
    })
}

shinyApp(ui,server)

Upvotes: 2

Related Questions