50452

Passing user input in Shiny to a URL query string

Question:

I have a question that I'm having trouble getting to work. I have a Shiny app that I want to accept two user input, send it back from ui.R to server.R, and insert that as a variable into a URL query string to download the file from a database. At first, we hardcoded the values to test the algorithm but we eventually want to make it user defined. I thought that saving as a variable and replacing it in the query string would work, but it didn't... I tried to concatenate pieces of the URL and I'm getting an error "Warning: Error in cat: argument 1 (type 'closure') cannot be handled by 'cat'". I tried to search online but couldn't find a good solution to my problem. Here is my code so far... As you see in the code, what I want to get is at least form the url and showing it as text on the ui just to see that it is being dynamic. Then when I know I can insert the user input, then I can continue on with storing the downloaded file.

#ui.R library(shiny) library(leaflet) library(foreach) library(ape) library(data.table) library(DT) # Choices for the genetic distance model geneticDistanceModel <- c( "raw" = "raw", "JC69" = "JC69", "K80" = "K80", "F81" = "F81", "K81" = "K81", "F84" = "F84", "BH87" = "BH87", "T92" = "T92", "TN93" = "TN93", "GG95" = "GG95", "logdet" = "logdet", "paralin" = "paralin" ) shinyUI(navbarPage("TeMPuЯa", id="nav", position = c("fixed-top"), # needed to keep fixed-top navbar from obscuring content header = tags$style(type = "text/css", "body {padding-top: 70px;}"), collapsible = "true", tabPanel("Tool", h1("Instructions"), p("Placeholder"), sidebarLayout( sidebarPanel( textInput("taxonomy", label = h4("Enter taxonomy group:"), value = "Porifera"), textInput("geography", label = h4("Enter geographical location:"), value = "all"), sliderInput("latitude", label = h4("Latitude difference"), min = 10, max = 30, value = 20), sliderInput("genetic", label = h4("Genetic similarity threshold"), min = 10, max = 20, value = 15), sliderInput("outgroups", label = h4("Select a distance from the outgroup"), min = 1, max = 2, value = 1.3, step = 0.1), selectInput("distanceModels", label = h4("Select a genetic distance model"), geneticDistanceModel, selected = "K80"), submitButton("Submit"), br(), downloadButton("download", label = "Download CSV") ), mainPanel( leafletOutput("worldmap"), br(), div(style='height:300px; width:850px; overflow:scroll', DT::dataTableOutput("url", width = 850)), textOutput("text") ) ) ), tabPanel("Genetic Distance Models Info", h1("Genetic distance models:"), a("Link to more explanation for the distance models used in R", href = "http://svitsrv25.epfl.ch/R-doc/library/ape/html/dist.dna.html"), br(), p(strong("raw:") ,"This is simply the proportion or the number of sites that differ between each pair of sequences. This may be useful to draw 'saturation plots'."), p(strong("JC69:") ,"This model was developed by Jukes and Cantor (1969)."), p(strong("K80:") ,"The distance derived by Kimura (1980), sometimes referred to as 'Kimura's 2-parameters distance'."), p(strong("F81:") ,"Felsenstein (1981) generalized the Jukes-Cantor model."), p(strong("K81:") ,"This model is called the Kimura's 'three substitution types model' (3ST), and is sometimes referred to as 'Kimura's 3-parameters distance'."), p(strong("F84:") ,"This model generalized K80, and was first introduced by Felsenstein in 1984."), p(strong("BH87:") ,"Barry and Hartigan (1987)."), p(strong("T92:") ,"Tamura (1992) generalized the Kimura model."), p(strong("TN93:") ,"Tamura and Nei (1993) model."), p(strong("GG95:") ,"Galtier and Gouy (1995) model."), p(strong("logdet:") ,"The Log-Det distance, developed by Lockhart et al. (1994), is related to BH87. However, this distance is symmetric."), p(strong("paralin:") ,"Lake (1994) developed the paralinear distance which can be viewed as another variant of the Barry-Hartigan distance.") ) )) # server.R library(shiny) library(leaflet) library(foreach) library(ape) library(data.table) library(DT) source("tsvtoDataFrame.R") shinyServer(function(input, output, session) { # Create the map output$worldmap <- renderLeaflet({ leaflet() %>% addTiles() %>% # Add default OpenStreetMap map tiles setView(lng = -93.85, lat = 37.45, zoom = 4) }) textInput <- reactive({ var1 <- "http://www.boldsystems.org/index.php/API_Public/combined?taxon=" var2 <- "&geo=" var3 <- "&format=tsv" paste(c(var1), c(input$taxonomy), c(var2), c(input$geography), c(var3)) }) output$text <- renderText({ textInput }) output$url <- DT::renderDataTable( dfMatchOverallBest, options = list(scrollX = TRUE) ) })

Answer1:

textInput is a reactive, so you should use

output$text <- renderText({ textInput() })

Also you should probably use paste0 instead of paste to get your URL, and the c() command is not necessary.

Recommend

  • Add link panel tabs in Shiny with various top level navigation bars
  • How do I scale the y-axis on a histogram by the x values in R?
  • Yii2 disable highlighting menu item
  • What products support 3-digit region subtags, e.g., es-419 for Latin-American Spanish?
  • UWP navigation (Template10), Pivot control, multiple frames
  • RegEx replacing IMG src attributes
  • Declaration of does not work with Ionic's ion-nav-view in IBM MobileFirst
  • Vim ctags behaves strangely
  • Converting RVML to SVG using XSLT in C#
  • How to get the click event on sIFR embedded tag in IE?
  • How to read JSON-LD data from HTML in Objective-C?
  • Why am I getting an Array of an Array with Doctrine 2 & Symfony 2 Data Transformer?
  • How to fallback to entirely different index page if user has javascript disable?
  • Always require certain dependencies in RequireJS
  • MySQL multiple IN conditions to subquery with same table
  • Reading space separated values file in c++ error
  • Is it possible to get the word under the mouse cursor in a ``?
  • BeautifulSoup difference between findAll and findChildren
  • NHibernate manually control fetching
  • Reading a file into a multidimensional array
  • Object and struct member access and address offset calculation
  • Blackberry - Custom EditField Cursor
  • Hardware Accelerated Image Scaling in windows using C++
  • Magento Fatal error: Maximum execution error solution, on WAMP
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • $wpdb not working in file of WordPress plugin
  • Change multiple background-images with jQuery
  • Algorithm for a smudge tool?
  • Body moving without any force applied? (Box2d)
  • RectangularRangeIndicator format like triangular using dojo
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Can Jackson SerializationFeature be overridden per field or class?
  • jquery mobile loadPage not working
  • Unanticipated behavior
  • Android Studio and gradle
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How to stop GridView from loading again when I press back button?
  • Android Google Maps API OnLocationChanged only called once