Passing user input in Shiny to a URL query string

最后都变了- 提交于 2020-01-17 03:15:10

问题


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)
  )


})

回答1:


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.



来源:https://stackoverflow.com/questions/36311562/passing-user-input-in-shiny-to-a-url-query-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!