0
votes

I am trying to do a simple thing using Shiny but I must be missing some fundamental knowledge as I cannot figure out what is wrong.

I have a simple textInput field on the UI side

textInput(inputId = "user_keyword", label = "Enter title", value = "Leon", width = "100%")

Now on the server side, I want to concatenate the user input into a URL, for example

user_url <- reactive({ paste0("https://www.imdb.com/title/", input$user_keyword) })

However, I am either getting the error can only be applied to a 'character vector', not a 'closure' or Operation not allowed without an active reactive context.

But in the docs for textInput it says

Server value A character string of the text input. The default value is "" unless value is provided.

What is wrong?

EDIT: A reproducible code.

library(rvest)
library(httr)
library(shiny)
library(shinydashboard)

user_agent <- user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
                         AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
my_session <- html_session("https://www.imdb.com", user_agent)

get_title_overview <- function(title_link) {
  title_page <- my_session %>% jump_to(title_link)
}

my.ui <- dashboardPage(
  dashboardHeader(title = "MovieTitles"),
  dashboardSidebar(
    textInput(inputId = "user_keyword", label = "Enter title", value = "Leon", width = "100%")
  ),
  dashboardBody()
)

my.server <- function(input, output, session) {
  user_url <- reactive({ paste0("https://www.imdb.com/title/", input$user_keyword) })
  title_details <- data.frame(get_title_overview(user_url()))
}  
  
shinyApp(ui = my.ui, server = my.server)
1

1 Answers

1
votes

Your code regarding the textInput and creating user_url is correct. However, user_url is a reactive (basically a function), so to access its value you need to use user_url(). The errormessage indicates that somewhere in your code you try to access the value with user_url instead. See the code below for a working example:

library(shiny)

ui <- fluidPage(
  textInput(inputId = "user_keyword", label = "Enter title", value = "Leon", width = "100%"),
  
  textOutput("test_out")
)

server <- function(input, output, session) {
  user_url <- reactive({ paste0("https://www.imdb.com/title/", input$user_keyword) })
  
  output$test_out <- renderText({
    user_url()
  })
}

shinyApp(ui, server)