简体   繁体   English

基于用户输入闪亮R的动态图像输出

[英]Dynamic image output based on user input shiny R

I have a csv file that I am uploading into a shiny app. 我有一个要上传到有光泽的应用程序中的csv文件。 Amongst other info, the csv file contains a column of names (input) and a column of unique IDs associated with each name. 在其他信息中,csv文件包含一列名称(输入)和一列与每个名称关联的唯一ID。 Basically what I want to happen is each time a user selects a different name the app takes the corresponding ID and inserts it into a URL to show a PNG image as an output. 基本上,我想发生的事情是,每当用户选择一个不同的名称时,应用程序都会获取相应的ID,并将其插入URL中以显示PNG图像作为输出。 For example, say the user selects the name "Joe Smith" and Joe's unique ID is "smithj01". 例如,假设用户选择名称“ Joe Smith”,而Joe的唯一ID为“ smithj01”。 I need the app to insert "smithj01" into a URL that is otherwise always the same to then show Joe's image. 我需要该应用在网址中插入“ smithj01”,否则该网址始终相同,然后显示Joe的图像。 If the user then selects "John Doe" and John's unique ID is "doej01" I need the app to insert "doej01" into the same URL in order to show John's image. 如果用户然后选择“ John Doe”,而John的唯一ID是“ doej01”,则我需要该应用在同一URL中插入“ doej01”,以显示John的图像。

I've tried writing a function that does this in the code below, but it still only shows a blue box with a question mark where the image should display. 我尝试在下面的代码中编写一个执行此操作的函数,但它仍然仅在图像应显示的位置显示一个带有问号的蓝色框。

I've tried again and again to solve this problem on my own, looked at numerous other posts and example code, but cannot, for the life of me, figure this out. 我一次又一次地尝试解决此问题,查看了许多其他文章和示例代码,但就我的一生而言,无法解决这个问题。 Please help! 请帮忙! Thank you so much! 非常感谢!

library(shiny)

injury <- read.csv("injury_app.csv", stringsAsFactors = FALSE)

"player_id" = "ID"

player_photo_url = function(player_id) {
  paste0("http://ssref.net/scripts/image_resize.cgi?min=200&url=https://d2cwpp38twqe55.cloudfront.net/req/201612101/images/players/", player_id,".png")
}

ui <- fluidPage(
  titlePanel("Title Goes Here"),
  sidebarLayout(
    sidebarPanel(
      selectInput("Player", "Player",
                  choices = c("Enter a player..." = "", injury$Player))
    ),
    mainPanel(
      uiOutput("image")
    )
  )
)

server <- function(input, output) {
  current_player = reactive({
    req(input$Player)
  })
  output$image <- renderUI({tags$img(src = player_photo_url(current_player()["player_id"]))
  })
}

shinyApp(ui = ui, server = server)

You've got some strange things going on, but the most concerning thing seems to be your attempts at selecting the ID for the player name selected. 您正在发生一些奇怪的事情,但是最令人担忧的事情似乎是您尝试为所选的玩家名称选择ID。 Assuming the csv injury is read in as a data frame this should work: 假设将csv injury作为数据帧读入,这应该起作用:

library(shiny)

injury <- read.csv("injury_app.csv", stringsAsFactors = FALSE)
uniq_players <- sort(unique(injury$Player))

ui <- fluidPage(
  titlePanel("Title Goes Here"),
  sidebarLayout(
    sidebarPanel(
      selectInput("Player", "Player",
                  choices = c("Enter a player..." = "", uniq_players))
    ),
    mainPanel(
      uiOutput("image")
    )
  )
)

server <- function(input, output) {

  c_id <- reactive({
    shiny::validate(
      shiny::need(input$Player, "Select a player!")
    )
    injury[injury$Player == input$Player, "ID"]
  })

  c_url <- reactive({
    paste0("http://ssref.net/scripts/image_resize.cgi?min=200&url=https://d2cwpp38twqe55.cloudfront.net/req/201612101/images/players/", c_id(), ".png")
  })

  output$image <- renderUI({
    tags$img(src = c_url())
  })
}

shinyApp(ui = ui, server = server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM