Chin Lin
Wednesday, October 1, 2014
– 了解App的基本架構(ui.R和server.R)
– 增加控制區參數
– 分享你的App至網路上
– 將控制區隨著使用者設定的參數而改變(如果你上次回家有自己看的話)
函數: conditionalPanel()
– 讓你的App可以接受使用者上傳的檔案
– 簡單的R分析
– 使控制區的參數隨著Server端的狀況而改變
函數: renderUI()
– 我們先練習讀取剛剛下載的範例檔(以下範例使用之路徑為“d:/CLTdata.txt”,請自行變更至適合的位置)
Data=read.table("d:/CLTdata.txt",header=TRUE) #讀取CLTdata.txt,並存成"Data"這個物件(資料表格式)
head(Data,10) #看"Data"這個資料表的前10個row
summary(Data) #看"Data"這個資料表的所有變項的基本資訊
連結失效請點這裡
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Summarize your data"),
sidebarPanel(
fileInput(inputId="files", label=h4("Upload your data file:"), multiple=FALSE, accept="text/plain"),
helpText("Note: you only can upload the .txt file."),
sliderInput("n", h4("Select the observed rows:"), min=5, max=20, value=10)
),
mainPanel(
verbatimTextOutput("summary"),
tableOutput("view")
)
))
library(shiny)
shinyServer(function(input, output) {
output$summary = renderPrint({
if (is.null(input$files)==TRUE) {return("You have to up load your data!!!")}
if (is.null(input$files)==FALSE) {
dat <- read.table(input$files$datapath,header=T)
summary(dat)
}
})
output$view = renderTable({
if (is.null(input$files)==TRUE) {return()}
if (is.null(input$files)==FALSE) {
dat <- read.table(input$files$datapath,header=T)
head(dat,input$n)
}
})
})
– radioButtons() in ui.R(單選題)
– plotOutput() in ui.R(輸出圖形)
– renderPlot() in server.R(處理圖形)
Data=read.table("d:/CLTdata.txt",header=TRUE) #讀取CLTdata.txt,並存成"Data"這個物件(資料表格式)
head(Data,10) #看"Data"這個資料表的前10個row
summary(Data) #看"Data"這個資料表的所有變項的基本資訊
Color="red"
plot(Data,col=Color)
連結失效請點這裡
– 請點這裡下載測試檔案
test1=read.table("d:/ttest.txt",header=TRUE) #讀取ttest.txt,並存成"test1"這個物件(資料表格式)
head(test1,10) #先簡單看下這個資料的樣子
summary(test1) #看"test1"這個資料表的所有變項的基本資訊
– 所以,我們可能要做一個Student t test,或是一個linear regression。
Result1=t.test(Y~X,data=test1,var.equal=TRUE) #Student t test
Result1
Result2=lm(Y~X,data=test1) #linear regression test
Result2
summary(Result2)
test1_0=test1[test1$X==0,]
test1_1=test1[test1$X==1,]
par(mfrow=c(1,2))
hist(test1_0$Y,col="red",xlab="Y",main="Histogram of Group 0")
hist(test1_1$Y,col="blue",xlab="Y",main="Histogram of Group 1")
test1$logY=log(test1$Y)
head(test1,10)
test1_0=test1[test1$X==0,]
test1_1=test1[test1$X==1,]
par(mfrow=c(1,2))
hist(test1_0$logY,col="red",xlab="log of Y",main="Histogram of Group 0")
hist(test1_1$logY,col="blue",xlab="log of Y",main="Histogram of Group 1")
Result1=t.test(logY~X,data=test1,var.equal=TRUE) #Student t test
Result1
Result2=lm(logY~X,data=test1) #linear regression test
Result2
summary(Result2)
Result3=wilcox.test(Y~X,data=test1)
Result3
– 使用者若上傳一個檔案(.txt file),其中若有個變項叫做X,另外個變項叫做Y,就可以進行分析
– 使用者可以有2個以上的選擇,來對資料進行分析
– 如果可以的話,設置一個按鍵功能,讓使用者能在懶惰的時候選擇無母數檢定(這可能需要用到conditionalPanel()函數)
連結失效請點這裡
– 所以,如果可以的話,我們希望左側的控制區可以出現一個選單,列出使用者所上傳的檔案的所有的變項名稱,並且讓使用者能選擇他的自變項&依變項。
– 請點這裡下載測試檔案
test2=read.table("d:/Topic3.txt",header=TRUE) #讀取Topic3.txt,並存成"test2"這個物件(資料表格式)
head(test2,10) #先簡單看下這個資料的樣子
summary(test2) #看"test1"這個資料表的所有變項的基本資訊
– 還記得如何做linear regression吧?
Result=lm(Weight~Height,data=test2) #linear regression test
Result
summary(Result)
– 畫張散佈圖吧!
plot(test2$Height,test2$Weight,pch=19)
abline(lm(Weight~Height,data=test2))
test2=read.table("d:/Topic3.txt",header=TRUE) #讀取Topic3.txt,並存成"test2"這個物件(資料表格式)
colnames(test2) # 列出所有的欄位名稱
colnames(test2)[1] # 列出第一個欄位名稱
colnames(test2)[4] # 列出第四個欄位名稱(依此類推)
Result1=lm(test2[,"Weight"]~test2[,"Height"])
summary(Result1)
連結失效請點這裡
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Linear regression for two continuous variables."),
sidebarPanel(
fileInput(inputId="files", label=h4("Upload your data file:"), multiple=FALSE, accept="text/plain"),
helpText("Note: you only can upload the .txt file."),
uiOutput("choose_columns1"), #這裡是關鍵
uiOutput("choose_columns2") #這裡是關鍵
),
mainPanel(
verbatimTextOutput("summary"),
plotOutput("plot",width = "500px", height = "500px")
)
))
library(shiny)
shinyServer(function(input, output) {
output$choose_columns1 <- renderUI({ #這裡是關鍵
if (is.null(input$files)) {return()}
if (is.null(input$files)==FALSE) {
dat <- read.table(input$files$datapath,header=T)
colnames <- colnames(dat)
radioButtons("Y", h4("Choose a dependence variable:"), choices = colnames)
}
})
output$choose_columns2 <- renderUI({ #這裡是關鍵
if (is.null(input$files)) {return()}
if (is.null(input$files)==FALSE) {
dat <- read.table(input$files$datapath,header=T)
colnames <- colnames(dat)
radioButtons("X", h4("Choose a independence variable:"), choices = colnames)
}
})
output$summary <- renderPrint({
if (is.null(input$files)==TRUE) {return(cat("You have to upload your file."))}
if (is.null(input$files)==FALSE) {
dat <- read.table(input$files$datapath,header=T)
X <- dat[,input$X] #這裡是關鍵
Y <- dat[,input$Y] #這裡是關鍵
Result=lm(Y~X)
return(summary(Result))
}
})
output$plot <- renderPlot({
if (is.null(input$files)==TRUE) {return()}
if (is.null(input$files)==FALSE) {
dat <- read.table(input$files$datapath,header=T)
X <- dat[,input$X] #這裡是關鍵
Y <- dat[,input$Y] #這裡是關鍵
plot(X,Y,pch=19)
abline(lm(Y~X),col="black")
}
})
})
Result1=cor.test(test2[,"Weight"],test2[,"Height"],method="pearson") # Pearson correlation
Result1
Result2=cor.test(test2[,"Weight"],test2[,"Height"],method="spearman") # Spearman correlation
Result2
– 當然,能讓使用者改個顏色也是必須的…
連結失效請點這裡
– 請點這裡下載測試檔案
– 專家版的題目就是設計一個App,讓使用者上傳他的表格後,可以選擇輸出森林圖/漏斗圖!
你可能會用到這些指令:
metadata=read.table("d:/Folic acid.csv",sep=",",header=TRUE)
library(meta)
Result=metabin(metadata[,"ev.trt"],metadata[,"n.trt"],metadata[,"ev.ctrl"],metadata[,"n.ctrl"],
studlab=metadata[,"name"],sm="RR",method.tau="DL")
forest(Result)
funnel(Result)
連結失效請點這裡
– 除此之外,希望你能將你的成果上傳到網路上!