Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

r - Packages missing in shiny-server

I am trying to create a web application using shiny. It requires me to load a package I have installed on my computer. For example:

## Contents ui.R:
library(shiny)
library(plyr)

shinyUI(pageWithSidebar(

  headerPanel("Hello Shiny!"),

  sidebarPanel(
    sliderInput("obs", 
                "Number of observations:", 
                min = 0, 
                max = 1000, 
                value = 500)
  ),

  mainPanel(
    plotOutput("distPlot")
  )
))

## Contents server.R:
library(shiny)
library(plyr)

shinyServer(function(input, output) {

  output$distPlot <- renderPlot({

    # generate an rnorm distribution and plot it
    dist <- rnorm(input$obs)
    hist(dist)
  })
})

This works fine if I run it locally (using runApp) but when I try to run it via my server (same computer) I get the error that the plyr package (or any other package I try to use this way) is not installed. How could I use extra packages in shiny server?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is that shiny-server cannot find the packages that you install because it runs them as a different user which is called shiny. This user is created upon installation of shiny-server

The easiest (and safest IMHO) way to solve this is to just install the packages as the shiny user, using the following steps.

  1. Set a password for the user using sudo passwd shiny, now enter and confirm a password
  2. Switch to the shiny account using: su - shiny
  3. Call up R using $ R (without sudo)
  4. Install the required packages, in this case: install.packages("plyr")

Note that if you have rstudio-server installed on the same machine then you can perform steps 2-4 using that interface. Simply go the same domain/ip and use :8787 for the rstudio-server interface instead of :3838 for shiny-server.

Adapted from my answer here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...