Host your learnr tutorial on your own shiny server

Congrats! Now you have configured the shiny server on your virtual machine. To host your learnr tutorial, you need to place the tutorial file (.Rmd) in the correct folder via the command scp that transfers your file securely to the virtual machine.

Step 1: log onto your virtual machine

To log onto your virtual machine, open a terminal and type the following

ssh [remote_username]@[host_address]
  • [remote_username]: Your username on the virtual machine
  • [host_address]: The host address of your virtual machine

If this is your first login, you will be prompted to confirm that you want to continue establishing the connection (type Y) and enter your password (if you have a public key setup, you don’t need a password.)

Once you have successfully logged in, you will see the base directory in your terminal is changed to [remote_username]@[host_address]

Step 2: make a folder

Now let’s make a folder to host your learnr tutorial. Run the following in your terminal.

cd /srv/shiny-server/
mkdir [remote_folder]
cd [remote_folder]
chmod -R 777 ./

The [remote_folder] is where you want to save your learnr tutorial. Also, depending on your setup, you may have to use add sudo at the beginning to allow changes on the root.

Example

If I want to create a tutorial01 folder under my name, I would type:

cd /srv/shiny-server/
mkdir sshan/tutorial01
cd sshan/tutorial01
chmod -R 777 ./

The cd command is short-handed for change directory. It allows you to go into the folder /srv/shiny-server/. All your shiny apps or learnr tutorials should be saved here (although they can be placed in different subfolders). We make a new folder called [remote_folder] by mkdir and cd into the new folder. The sudo chmod command allows you to authorize the entire folder, so that the shiny-server can later make changes and build the tutorial into .html file in the same folder.

In general, it is recommended to create a separate folder for each learnr tutorial/shiny app.

Step 3: copy your tutorial from your local machine to the remote server

Let’s first log out from the remote server by typing in the terminal

exit

Say you’ve already made a tutorial called [local_file]. Then we use the scp to transfer the [local_file] to the folder /srv/shiny-server/[remote_folder] on the remote server [remote_username]@[host_address].

scp [local_file] [remote_username]@[host_address]:/srv/shiny-server/[remote_folder]

To use this code, change the following:

  • [local_file]: The path and the name of the file we want to copy
  • [remote_username]: Your username on the virtual machine
  • [host_address]: The host address of your virtual machine
  • [remote_folder]: The directory you want to copy the file into

If you are unsure about the path to the [local_file], I found this trick works each time: use cd to get into the directory (on the local machine) that contains the tutorial, and type pwd to show the full path.

Depending on your setup, you may have to use add sudo at the beginning to allow changes on the root.

Example

sudo scp /Users/sshan/tutorial.Rmd sshan@rshiny.mtholyoke.edu:/srv/shiny-server/sshan/

Your tutorial is now up in the air

Viola! (Sorry violin-gang.) You have just uploaded and deployed your first learnr tutorial on the shiny server. You can view it by going to that folder’s URL listened on the IP address 138.110.84.108 with port 3838.

http://http://138.110.84.108:3838/[remote_folder]/tutorial.Rmd

If you forgot what your IP address is, you can find out by the hostname -I command.

FAQ

Q-01: I logged onto the webpage, but I got an error message saying “the application failed to start”.

A: You can get more information about the error by checking the shiny server log at /var/log/shiny-server. The folder contains a list of outputs for each instantiation of the shiny server.

Q-02: The error log says “Unable to write prerendered HTML file to /srv/shiny-server/[remote_folder]/index.html”. What should I do?

A: If you have this error, it means the shiny server has not had the permission to change in the remote_folder. To grant the permission, run the following code in your command line.

cd /srv/shiny-server/[remote_folder]
chmod -R 777 ./

The chmod command allows you to authorize the entire folder.

Q-03: The error log says “Error in library(tidyverse) : there is no package called ‘tidyverse’”.

A: The error means the R pacakge has not been installed on the remote server. To install, run the following code in your command line.

sudo su - -c "R -e \"install.packages('tidyverse', repos='http://cran.rstudio.com/')\""

Summary of useful commands

  • Connect to your virtual machine: ssh [username]@[host_address]
  • Change the current directory: cd [new_directory]
  • Made a new folder : mkdir [new_directory]
  • Authorise an entire folder: chmod -R 777 ./
Written on August 15, 2020