سؤال

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.

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:

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

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top