Add wrapper to Shiny in R package

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Add wrapper to Shiny in R package

Axel Urbiz
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Add wrapper to Shiny in R package

Thierry Onkelinx
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
[hidden email]
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 <[hidden email]>:

> 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Add wrapper to Shiny in R package

Axel Urbiz
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)

}

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 <[hidden email]
> 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
> [hidden email]
> 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 <[hidden email]>:
> > 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]]
> >
> > ______________________________________________
> > [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Add wrapper to Shiny in R package

jdnewmil-2
In reply to this post by Thierry Onkelinx
... which begs the question... how does the my.env variable get from the myApp function into the server function?

Perhaps read [1]?

[1] https://shiny.rstudio.com/articles/function.html
--
Sent from my phone. Please excuse my brevity.

On September 21, 2017 8:13:15 AM PDT, Thierry Onkelinx <[hidden email]> 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
>[hidden email]
>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 <[hidden email]>:
>> 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]]
>>
>> ______________________________________________
>> [hidden email] 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.
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Add wrapper to Shiny in R package

Duncan Murdoch-2
In reply to this post by Axel Urbiz
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 <[hidden email]
>> 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
>> [hidden email]
>> 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 <[hidden email]>:
>>> 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]]
>>>
>>> ______________________________________________
>>> [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.