4

I am successful in using global.R to pass data to ui.r and server.R while developing in RStudio. However when I migrate my code to server neither ui.R nor server.R are able to read global.R. I am using Shiny Server (not pro). What could be causing this?

My code looks like this (it is not reactive)

#global.R
x = 10

#ui.R
print(x)
> 10 #in RStudio viewer
> Error: object 'x' not found #on Shiny Server

Following sigmabeta's answer below I made changes to server.R and global.R however I am looking for the server to reset x to another value so that it can be read by ui.R. This is what my code is now

#global.R
x = 10
get_x_value <- function (n) {
x = n+1        
return x
}

#server.R
source("./global.R")

shinyServer(function(input, output) {
values <- reactiveValues()
observe ({
    values$x <- get_x_value(5)
})
})

#ui.R
print(x)
> 6 #in RStudio viewer
> 10 #on Shiny Server

This is the actual code in ui.R where I am trying to set the status of the box based on the values already computed in server.R

library(shinydashboard)
dashboardPage(
  Header = dashboardHeader(title = 'Test'), 
  Sidebar = dashboardSidebar
  (
  sidebarMenu
  (
  menuItem("ABC", tabName = "ABC")
  )
  ),
  Body = dashboardBody
  (tabItems
  (
  tabItem(
    tabName = "ABC",
    fluidRow
    (
    box
    (
    status = if (x==6) "info" else "danger" ,
    solidHeader = TRUE
    )
    )
  )
  )
  )
)
earthlink
  • 323
  • 5
  • 15

1 Answers1

4

It seems you have not referenced to your shiny app that it must fetch values (and/or) functions from global.R

You can do this in the server.R file. Example code of server.R:

library(shiny)

source("./global.R")

shinyServer(function(input, output) {
values <- reactiveValues()
observe ({
    values$x <- get_x_value()
})
output$text1 <- renderText({
   values$x
})

})

And then in global.R, you would have the function get_x_value like so:

get_x_value <- function () {
    x = 10
    return x
}

x can also be defined outside, and you may want to do some extra processing if any in the function or write more complex functions.

Update: Adding the code for ui.R

shinyUI(fluidPage(
mainPanel(
    htmlOutput(
        textOutput("text1")
    )
))
Syamanthaka
  • 1,227
  • 2
  • 15
  • 23
  • Thank you. That helped. However with this solution I am not able to update any variable in global.R from server.R – earthlink Sep 29 '16 at 17:18
  • 1
    You need to use reactive values then. I'll edit my answer to explain how you can do this reactively – Syamanthaka Sep 30 '16 at 09:17
  • I am still not able to update x from the server. I edited my question to show what I am looking for. – earthlink Sep 30 '16 at 17:04
  • I've edited to include the ui.R code. I assume you have already read the tutorials for shiny. If not, please see http://shiny.rstudio.com/tutorial/ – Syamanthaka Oct 03 '16 at 05:54
  • Thanks again. However the end use of x is not display it but to affect the status of other objects for example to change the color of infoBox in shinydashboard. – earthlink Oct 03 '16 at 21:27
  • You need to change the logic slightly. Make the "status" variable an input parameter (say Select Input) and then make it hidden, so user cannot see it. Set it with your default value eg 'danger'. Then in server do something like observe({ updateSelectInput()}) and pass the value of x that you get from your function get_value_x(). I hope this gives you enough pointers. – Syamanthaka Oct 06 '16 at 12:16
  • This answer feels wrong -- why is an explicit source(...) required? But it works. – Josiah Yoder Jun 13 '19 at 20:14
  • @JosiahYoder how else do you think we can refer to an external file which has functions that you want to use in the main code? – Syamanthaka Jul 19 '19 at 03:59
  • @sigmabeta My disagreement is not so much with the answer as with the Shiny library. Why is this not documented [in the introduction to Shiny module layouts?](https://shiny.rstudio.com/articles/scoping.html)? I agree the code is self documenting, but wish the linked R page would have said to do this. – Josiah Yoder Jul 24 '19 at 17:41
  • 3
    [This article](https://mraess.netlify.com/2018/07/the-awesomeness-that-is-the-global-r-file-or-how-to-clean-up-your-shiny-app/) actually says the opposite: That sourcing is **not** needed. Perhaps whether the source("global.R") is needed changes from one version of Shiny to another? – Josiah Yoder Jul 24 '19 at 17:51
  • @JosiahYoder, if you have modules written in a separate module file and not include all the module code within server.R or ui.R, how would you refer that file within these scripts? In python, one uses the "import ", in R it is "source ". The OP has in the case just named foo as "global.R". – Syamanthaka Jul 30 '19 at 06:25
  • 3
    You don't need to source `global.R`, it just needs to be in the same directory as `server.R` and `ui.R`. Note that `shinyApp` calls cannot weave `global.R` and `app.R` together; however, that is done automatically through shinyAppDir.. cf. `?shinyApp` and https://stackoverflow.com/questions/54914541/global-r-dont-start/66802176#66802176 – Rafs Mar 25 '21 at 15:05