R语言使用笔记

305 阅读1分钟

Before Install Packages

# Install tidyverse
sudo apt install libcurl4-openssl-dev libssl-dev libxml2-dev zlib1g-dev
# Install RMySQL
sudo apt-get install libmysqlclient-dev
# Install sodium/shinyauthr
sudo apt-get install libsodium-dev
# Install systemfonts 
sudo apt-get libfontconfig1-dev
# Install RODBC
sudo apt-get install unixodbc-dev
# Install postgresql
sudo apt-get install libpq-dev
# Install reticulate
sudo apt install libpng-dev

# Install libssl1.0.0
wget http://security.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.0.0_1.0.2g-1ubuntu4.20_amd64.deb
sudo dpkg -i libssl1.0.0_1.0.2g-1ubuntu4.20_amd64.deb

# Install openssl1.1
wget https://www.openssl.org/source/openssl-1.1.1u.tar.gz
tar -zxvf openssl-1.1.1u.tar.gz
cd openssl-1.1.1u
sudo ./config  --prefix=/usr/local/openssl
sudo make
sudo make install

# Install rstudio-server
wget http://security.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.0.0_1.0.2g-1ubuntu4.20_amd64.deb
sudo dpkg -i libssl1.0.0_1.0.2g-1ubuntu4.20_amd64.deb

lpad

sprintf('%04s',10)

set repos

options(repos='http://cran.rstudio.com/')

edit Rprofile

file.edit(file.path("~", ".Rprofile"))

read_tsv with encoding and default col_types

readr::read_tsv('file',
         locale = locale(encoding = 'gbk'),
         col_types = cols(.default = 'c'))

Connect database with JDBC

# sql server
DatabaseConnector::downloadJdbcDrivers('sql server')
conn <- DatabaseConnectorDriver::connect(
  dbms = "sql server",
  server = "localhost",
  user = "usr",
  password = "pwd"
)

shinyproxy

deploy

# Dockerfile
FROM rocker/shiny
EXPOSE 3838

# build image
docker build -t tv01 /root/shinyApps/t1

# /root/application.yml
- id: 01
      display-name: test_shinyApps
      container-volumes: ["/root/shinyApps/t1:/srv/shiny-server"]
      container-image: tv01
      access-groups: [ scientists, mathematicians ]

get username

as.list(Sys.getenv())$SHINYPROXY_USERNAME

vscode pipe shortcut

[{
    "key": "ctrl+shift+m",
    "command": "editor.action.insertSnippet",
    "when": "editorTextFocus  editorLangId == 'r'",
    "args": {
        "snippet": " |> "
    }
}]

fidelius

  • By rmarkdown
title: "Fidelius Config"
output: 
  fidelius::html_password_protected:
    output_format:
      rmarkdown::html_document:
        toc: true
        toc_float: true
    password: 'pw123'
    preview: false
    style:
      header_text: 'My Report'
      placeholder_text: 'plz input password'
      button_text: 'confirm'
  • By r code
library(fidelius)

charm(
  'app.rmd',
  password = '',
  style = stylize(
    header_text = 'Welcome!'
    placeholder_text = 'Enter your password',
    button_text = 'Confirm',
  ),
  output = 'app.html'
)

connect wps webhook

reticulate::py_run_string(r"(
def connect_wps(token,webhook):
  import http.client
  conn = http.client.HTTPSConnection("www.kdocs.cn")
  payload = "{\"Context\":{\"argv\":{}}}"
  headers = {
      'Content-Type': "application/json",
      'AirScript-Token': token
      }
  conn.request("POST", webhook, payload, headers) 
  res = conn.getresponse()
  data = res.read().decode("utf-8")
  return data
)")

reticulate::py$connect_wps(
  "token",
  "https://www.kdocs.cn/api/v3/ide/file/xx/script/xx/sync_task"
)
readWps <- function(token,webhook){
  http <- reticulate::import('http.client')
  conn = http$HTTPSConnection('www.kdocs.cn')
  payload = "{\"Context\":{\"argv\":{}}}"
  headers = 
    list(
    `Content-Type` = "application/json",
    `AirScript-Token`= token
  )
  
  conn$request('POST',
               webhook,
               payload, headers)
  res = conn$getresponse()
  dt <- jsonlite::fromJSON(
    res$read()$decode('utf-8'))$data$result$abc
  
  dt <- setNames(data.frame(dt[-1,]),dt[1,])
  return(dt)
}

Shiny Server

sudo apt-get install gdebi-core
wget https://download3.rstudio.org/ubuntu-18.04/x86_64/shiny-server-1.5.20.1002-amd64.deb
sudo gdebi shiny-server-1.5.20.1002-amd64.deb

WSL

apg-get install make
apt-get install build-essential gcc

Reactable

library(crosstalk)
library(shiny)

data <- SharedData$new(ggplot2::mpg)

fluidRow(
  column(
    4,
    filter_checkbox("class", "Class", data, ~class),
    filter_slider("displ", "Displ", data, ~displ, width = "100%"),
    filter_select("mfr", "Manufacturer", data, ~manufacturer),
    tags$button(
      tagList(fontawesome::fa("download"), "Download as CSV"),
      onclick = "Reactable.downloadDataCSV('mpg-download-table', 'mpg.csv')"
    ),
  ),
  column(
    8,
    reactable::reactable(data, minRows = 10,elementId = "mpg-download-table")
  )
)

ODBC

if ! [[ "18.04 20.04 22.04 23.04" == *"$(lsb_release -rs)"* ]];
then
    echo "Ubuntu $(lsb_release -rs) is not currently supported.";
    exit;
fi

curl https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc

curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list | sudo tee /etc/apt/sources.list.d/mssql-release.list

sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql18
# optional: for bcp and sqlcmd
sudo ACCEPT_EULA=Y apt-get install -y mssql-tools18
echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bashrc
source ~/.bashrc
# optional: for unixODBC development headers
sudo apt-get install -y unixodbc-dev
odbc::odbcListDrivers()