简体   繁体   English

在Shiny R中加载图像或文本

[英]Load image or text in Shiny R

I want to make a simple web app using R SHiny where I load a image from my harddrive by giving the path and displaying it on my webpage when a button is clicked. 我想使用R SHiny制作一个简单的Web应用程序,在其中通过提供路径并单击按钮时将其显示在网页上来从硬盘驱动器加载图像。

I started by doing that for text, for example displaying the path, but my button does not react when I click(I mean to say it doesnt print the message). 我首先对文本执行此操作,例如显示路径,但是单击时我的按钮没有反应(我的意思是说它不打印消息)。

server.R: server.R:

shinyServer(function(input, output, session) {
  dt<-reactive({
  output$text1 <- renderText({ 
  paste("You have selected", input$obs)
   })
  }) 
})

ui.R: ui.R:

shinyUI(pageWithSidebar(
  headerPanel("Fruits and vegetables!"),
  sidebarPanel(
    helpText("What do you see below?"),
    #imageOutput(outputId="images/1.png")
    numericInput("obs", "Number of observations to view:", 10),
    actionButton("get", "Get")
),
  mainPanel(textOutput("text1"))
))

With reactives, you must wrap the code that's using your inputs in a reactive block, but you must set the output values outside of it. 对于反应式,必须将使用输入的代码包装在reactive块中,但是必须在其外部设置output值。 In this case, your example should be 在这种情况下,您的示例应为

 shinyUI(pageWithSidebar(
   headerPanel("Fruits and vegetables!"),
   sidebarPanel(
     helpText("What do you see below?"),
     #imageOutput(outputId="images/1.png")
     numericInput("obs", "Number of observations to view:", 10),
     actionButton("get", "Get")
   ),
   mainPanel(textOutput("text"))
 ))

 shinyServer(function(input, output, session) {
   dt <- reactive({
     paste("You have selected", input$obs)
   })
   output$text <- renderText({ dt() })
 })

To use the imageOutput dynamically, you should provide more information about how you want the image URL to be selected from the input. 要动态使用imageOutput ,应提供有关如何从输入中选择图像URL的更多信息。

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

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