首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Reactive in R shiny

Reactive in R shiny
EN

Stack Overflow用户
提问于 2020-07-24 13:55:22
回答 1查看 158关注 0票数 0

我刚接触R shiny,我要做一个关于PCA分析的闪亮的应用,我想让学校作为我的动态UI元素,这可以用下面的R标准代码来表达,也就是说,这个应用可以在选择不同的学校时显示PC分数,截图,双线图和解释方差的比例/单位。

代码语言:javascript
复制
# Read the data 
temp <- tempfile()
download.file("http://archive.ics.uci.edu/ml/machine-learning-databases/00356/student.zip",temp, mode="wb")
unzip(temp, "student-mat.csv")
math <- read.table("student-mat.csv",sep= ";", header= T)
unlink(temp)
(math <- as_tibble(math))

# Read the data for school "GP"
math.GP <- read.table("student-mat.csv",sep= ";", header= T) %>% filter(school == "GP")



# PCA Scores
PCs <- prcomp(select(math.GP, G3, G1, G2, absences, studytime), 
              center = TRUE, scale = TRUE)
PCs

# Biplot
biplot(PCs, xlabs = rep(".", nrow(math.GP)), cex = 1.2)

# Screeplot
screeplot(PCs, type = "lines")

# Explained Proportion VS Cum. Proportion 
par(mfrow = c(1, 2))
plot(PCs$sdev^2/sum(PCs$sdev^2), xlab = "Principal Component", 
         ylab = "Proportion of Variance Explained", ylim = c(0, 1), type = 'b')
plot(cumsum(PCs$sdev^2/sum(PCs$sdev^2)), xlab = "Principal Component", 
ylab = "Cum. Prop of Variance Explained", ylim = c(0, 1), type = 'b')

# Read the data for school "GP"

math.MS <- read.table("student-mat.csv",sep= ";", header= T) %>% filter(school == "MS")
head(math)
# PC Scores
PCs <- prcomp(select(math.MS, G3, G1, G2, absences, studytime), center = TRUE, scale = TRUE)
PCs


# Biplot
biplot(PCs, xlabs = rep(".", nrow(math.MS)), cex = 1.2)

# Screeplot
screeplot(PCs, type = "lines")
par(mfrow = c(1, 2))
plot(PCs$sdev^2/sum(PCs$sdev^2), xlab = "Principal Component", 
         ylab = "Proportion of Variance Explained", ylim = c(0, 1), type = 'b')
plot(cumsum(PCs$sdev^2/sum(PCs$sdev^2)), xlab = "Principal Component", 
ylab = "Cum. Prop of Variance Explained", ylim = c(0, 1), type = 'b')

这是我的闪亮应用程序代码,错误如下所示。我不知道为什么它在标准R代码中有效,但在闪亮的代码中不起作用。你能帮我修一下吗?非常感谢。

代码语言:javascript
复制
library(shiny)
library(shinythemes)

temp <- tempfile()
download.file("http://archive.ics.uci.edu/ml/machine-learning-databases/00356/student.zip",temp, mode="wb")
unzip(temp, "student-mat.csv")
math <- read.table("student-mat.csv",sep= ";", header= T)
unlink(temp)
(math <- as_tibble(math))


shinyUI(fluidPage(
        headerPanel(h1("PRINCIPLE COMPONENT ANALYSIS")),
        
        # Sidebar with options for the two schools
        sidebarLayout(
          sidebarPanel(
            h3("Select the Schools:"),
            selectizeInput("school", "School", selected = "GP",
                           choices = levels(as.factor(math$school)))
          ),
          mainPanel(
            tabsetPanel(
              
              tabPanel("PC_Scores",verbatimTextOutput("scores")),
              tabPanel("PC_Scree_PLot",plotOutput("screePlot")),
              tabPanel("Bi_Plot",plotOutput("biplot")),
              tabPanel("Proportion of Variance Explained vs 
                               Cum. Proportion of Variance Explained"),
              plotOutput("explain"))
            
          )))
)


library(shiny)
library(dplyr)

temp <- tempfile()
download.file("http://archive.ics.uci.edu/ml/machine-learning-databases/00356/student.zip",temp, mode="wb")
unzip(temp, "student-mat.csv")
math <- read.table("student-mat.csv",sep= ";", header= T)
unlink(temp)
(math <- as_tibble(math))


shinyServer(function(input,output,session) {
  
  math <- reactive({
    newDat <- math %>% filter(school == input$school) %>% 
      select(G1, G2, G3, absences, studytime)
  })
  

  output$scores <- renderPrint({
    X <- math()
    pca <- princomp(X, center = TRUE, scale = TRUE)
    pca$scores
  })
  
  output$screeplot<-renderPlot(
    {
      data <- math()
      dat <- princomp(data, center = TRUE, scale = TRUE)
      screeplot(dat, type = "lines")
    })
   
  output$biplot<-renderPlot(
    {
      data <- math()
      dat <- princomp(data, center = TRUE, scale = TRUE)
      biplot(dat, xlabs = rep(".", nrow(data)), cex = 1.2)
    })
  
  output$explain <- renderPlot(
    {
      data <- math()
      dat <- princomp(data, center = TRUE, scale = TRUE)
      par(mfrow = c(1, 2))
      plot(dat$sdev^2/sum(PCs$sdev^2), xlab = "Principal Component", 
           ylab = "Proportion of Variance Explained", ylim = c(0, 1), type = 'b')
      plot(cumsum(dat$sdev^2/sum(PCs$sdev^2)), xlab = "Principal Component", 
           ylab = "Cum. Prop of Variance Explained", ylim = c(0, 1), type = 'b')
    })
  
  

})

EN

回答 1

Stack Overflow用户

发布于 2020-07-24 15:43:01

我整理了你的应用程序,使它更符合它的外观。我并不热衷于使用在线文件,所以我在这个例子中使用了mtcar。使用req()是个好主意,这样函数在一切准备就绪之前都无法运行。

代码语言:javascript
复制
library(shiny)
library(shinythemes)
library(dplyr)

ui <- shinyUI(fluidPage(
    headerPanel(h1("PRINCIPLE COMPONENT ANALYSIS")),
    
    # Sidebar with options for the two schools
    sidebarLayout(
        sidebarPanel(
            h3("Select the Schools:"),
            selectizeInput("school", "School", choices = NULL)
        ),
        mainPanel(
            tabsetPanel(
                
                tabPanel("PC_Scores", verbatimTextOutput("scores")),
                tabPanel("PC_Scree_PLot", plotOutput("screePlot")),
                tabPanel("Bi_Plot", plotOutput("biplot")),
                tabPanel("Proportion of Variance Explained vs Cum. Proportion of Variance Explained", plotOutput("explain")))
            
        )))
)

server <- shinyServer(function(input,output,session) {
    
    # data import, uses ths to populate drop-down and to sub-set for PCA
    dat <- reactive({ as_tibble(mtcars, rownames = "model") })
    
    math <- reactive({
        
        req(dat())
        
        dat() %>%
            filter(cyl == as.numeric(input$school)) %>%
            select(mpg, disp, hp)
        
    })

    observe({
        updateSelectInput(session, "school",
                          choices = as.character(unique(dat()$cyl)),
                          selected = "4")
    })
    
    
    output$scores <- renderPrint({
        
        req(math())
        
        pca <- princomp(math(), center = TRUE, scale = TRUE)
        pca$scores
    })
    
    output$screePlot<-renderPlot({
        
        req(math())
        
        dat <- princomp(math(), center = TRUE, scale = TRUE)
        screeplot(dat, type = "lines")
    })
    
    output$biplot <- renderPlot({
        
        req(math())
        
        dat <- princomp(math(), center = TRUE, scale = TRUE)
        biplot(dat, xlabs = rep(".", nrow(math())), cex = 1.2)
    })
    
    output$explain <- renderPlot({
        
        req(math())
        
        dat <- princomp(math(), center = TRUE, scale = TRUE)
        par(mfrow = c(1, 2))
        plot(dat$sdev^2/sum(PCs$sdev^2), xlab = "Principal Component", 
             ylab = "Proportion of Variance Explained", ylim = c(0, 1), type = 'b')
        plot(cumsum(dat$sdev^2/sum(PCs$sdev^2)), xlab = "Principal Component", 
             ylab = "Cum. Prop of Variance Explained", ylim = c(0, 1), type = 'b')
    })
    
    
    
})

# Run the application 
shinyApp(ui = ui, server = server)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63067654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档