[R] Add wrapper to Shiny in R package

Duncan Murdoch murdoch.duncan at gmail.com
Thu Sep 21 18:12:03 CEST 2017


On 21/09/2017 11:30 AM, Axel Urbiz wrote:
> Thank you Thierry. I'm trying to following your suggestion in the example
> below, but getting:
> 
> Error in get("xs", envir = my.env) : object 'my.env' not found.
> 
> 
> library(shiny)
> library(shinydashboard)
> 
> myApp <- function(x, ...) {
> 
>    xs <- scale(x)
> 
>    my.env <- new.env()
>    assign("xs", xs, envir = my.env)
> 
>    shiny::runApp(app)
> 
> }

I don't know Shiny's setup very well, but I don't see how the server 
function could see my.env here.  my.env is local to myApp, and the 
server function is defined outside of that.

So you should move my.env to the global environment where the server can 
see it, or define the app within myApp, where its functions can see 
locals for that function.

Duncan Murdoch

> 
> app = shinyApp(
> 
>    ui = fluidPage(
>      sidebarLayout(
>        sidebarPanel(sliderInput("n", "Bins", 5, 100, 20)),
>        mainPanel(plotOutput("hist"))
>      )
>    ),
>    server = function(input, output) {
> 
>      get("xs", envir = my.env)
> 
>      output$hist <- renderPlot(
>        hist(xs, breaks = input$n,
>             col = "skyblue", border = "white")
>      )
>    }
> )
> 
> myApp(rnorm(100))
> 
> Axel.
> 
> 
> On Thu, Sep 21, 2017 at 11:13 AM, Thierry Onkelinx <thierry.onkelinx at inbo.be
>> wrote:
> 
>> Dear Axel,
>>
>> I've used environment for such problems.
>>
>> assign("xs", xs, envir = my.env) in the myApp function
>> get("xs", envir = my.env) in the server function
>>
>> Best regards,
>>
>>
>> ir. Thierry Onkelinx
>> Statisticus/ Statiscian
>>
>> Vlaamse Overheid / Government of Flanders
>> INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE
>> AND FOREST
>> Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance
>> thierry.onkelinx at inbo.be
>> Kliniekstraat 25, B-1070 Brussel
>> www.inbo.be
>>
>> ////////////////////////////////////////////////////////////
>> ///////////////////////////////
>> To call in the statistician after the experiment is done may be no
>> more than asking him to perform a post-mortem examination: he may be
>> able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher
>> The plural of anecdote is not data. ~ Roger Brinner
>> The combination of some data and an aching desire for an answer does
>> not ensure that a reasonable answer can be extracted from a given body
>> of data. ~ John Tukey
>> ////////////////////////////////////////////////////////////
>> ///////////////////////////////
>>
>>
>> Van 14 tot en met 19 december 2017 verhuizen we uit onze vestiging in
>> Brussel naar het Herman Teirlinckgebouw op de site Thurn & Taxis.
>> Vanaf dan ben je welkom op het nieuwe adres: Havenlaan 88 bus 73, 1000
>> Brussel.
>>
>> ////////////////////////////////////////////////////////////
>> ///////////////////////////////
>>
>>
>>
>> 2017-09-21 15:02 GMT+02:00 Axel Urbiz <axel.urbiz at gmail.com>:
>>> Dear List,
>>>
>>> I'm trying to add a function that calls a Shiny App in my R package. The
>>> issue is that within my function, I'm creating objects that I'd like to
>>> pass to the app. For instance, from the example below, I'm getting
>>> "Error: object
>>> 'xs' not found". How can I pass "xs" explicitly to shinyApp()?
>>>
>>>
>>> *Under R directory:*
>>>
>>> myApp <- function(x, ...) {
>>>    require(shiny)
>>>
>>>    xs <- scale(x)
>>>
>>>    shiny::runApp(appDir = system.file("application", package =
>>> "my_package"), ...)
>>>
>>> }
>>>
>>> *Under inst/application directory a file named app.R with the following
>>> content:*
>>>
>>> shinyApp(
>>>    ui = fluidPage(
>>>      sidebarLayout(
>>>        sidebarPanel(sliderInput("n", "Bins", 5, 100, 20)),
>>>        mainPanel(plotOutput("hist"))
>>>      )
>>>    ),
>>>    server = function(input, output) {
>>>      output$hist <- renderPlot(
>>>        hist(xs, breaks = input$n,
>>>             col = "skyblue", border = "white")
>>>      )
>>>    }
>>> )
>>>
>>> Thank you,
>>> Axel.
>>>
>>>          [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/
>> posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list