1

Fresh to shiny...
1. I want to create an ui.R using dashboardPage and define a selectInput box (Done!)
2. As soon as the user will selects one of the fields in the selectInput, a Schools_Info.R file would be sourced. I have tried source("Schools_Info.R") but I want some function which will run Schools_Info.R in backgroud.
How you do that?
3. Schools_Info.R contains values that I want to use as the min and max of a sliderInput.
How can I define sliderInput that will adjust its limits(min and max) automatically according to whatever the user selects from selectInput box?

ui.R

library(shiny)
options(shiny.trace=TRUE)
library(shinydashboard)

locations <- c("Select your location",
           paste0("\tLocation1"),
           paste0("\tLocation2"),               
           paste0("\tLocation3")
           )


ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
    dashboardSidebar(
          selectInput("selectedLoc",choices = locations),
          source("Schools_Info.R"),
          sliderInput("slider", "Number of observations:", 1, min, max)),
    dashboardBody(
      fluidRow(

      box(
        title = "Controls",
      )
    )
  )
)
flamenco
  • 2,702
  • 5
  • 30
  • 46
  • Try creating a file `global.R` and define the values – Keniajin Mar 02 '15 at 05:29
  • I've done some reading and it seems that this kind of application needs to be done using `reactive programming` functions. Now, I'm at the point where the user can select a field from `selectInput` box but I do not know how to run a script from `shiny` server . Also, I need to pass the variable to that file as , based in the that variable, the script should return some constants . – flamenco Mar 02 '15 at 05:46
  • to run a shiny app from the server you can do `runApp("~\nameofFolder")` . – Keniajin Mar 02 '15 at 05:50
  • no! I need to run a .R file not a shiny application – flamenco Mar 02 '15 at 05:53
  • 1
    is there a function in your source file or does would it produce an output independently from the user input? – NicE Mar 02 '15 at 17:00

1 Answers1

0

In Schools_Info.R, make sure you have functions defined. Running that file should do nothing but define functions for later use. Source Schools_Info.R at the beginning of server.R, even before shinyServer(. Then have the functions run reactively using whatever the user picks from the selectInput widget. You could have separate functions to grab the max and min values, or you could use this method to return two values from a single function at the same time. Then use updateSliderInput to set the values of the min and max on the slider.

Schools_Info.r

GetMax = function(locality){15}
GetMin = function(locality){1}

server.R

max = reactive(GetMax(selectedLoc))
min = reactive(GetMax(selectedLoc))
observe({
  updateSliderInput(session, "slider", "Number of observations:", c(max, min))
)}

Obviously, I don't know what Schools_Info.R does, so the functions I put in are pretty trivial. Also, I'm not totally clear on what observe does, so it might not be necessary.

Community
  • 1
  • 1
Paul de Barros
  • 1,170
  • 8
  • 22