簡體   English   中英

如何使ui.R和server.R從其他文件導入功能和數據並在ShinyApp中繪圖

[英]How to make ui.R and server.R to import function and data from other file and plot in ShinyApp

我有三個要用於制作Shiny應用程序的文件。

酷圖

這是存儲繪圖功能的代碼。

library(ggplot2)
library(tidyverse)
library(ggrepel)

#-------------------------
# Function 
#-------------------------
plotit <- function (dat, x_thres, y_thres) {
  dat["Significant"] <- ifelse((dat$wt > x_thres | 
                                  dat$mpg > y_thres ), 'NotSignif','Signif')
  p <- ggplot(dat, aes(wt, mpg)) + 
       geom_point(alpha=0.8,size=2.75, aes(color=Significant)) +
       scale_color_manual(values=c("#B94024","#7D8D87")) +
       geom_vline(xintercept= x_thres, colour = '#B94024') + 
       geom_hline(yintercept=y_thres, colour = '#B94024') +
       geom_text_repel(data=subset(dat, wt > x_thres | mpg > y_thres),
                       aes(wt,mpg,label=model),
                       box.padding = unit(0.35, "lines"),
                       point.padding = unit(0.3, "lines") 
                       )  + 
       theme(legend.position="none")  

  return(p)

}

#-------------------------
# Begin main code
#-------------------------
# I literally want to use file as input not 
# default mtcars variable
infile <- "https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv"
dat <- read_delim(infile,delim=",", col_types = cols())  
y_thres <- 25
x_thres <- 3
plotit(dat, x_thres, y_thres)

該函數基本上采用x閾值和y閾值並進行如下繪制:

在此處輸入圖片說明

然后,我嘗試構建Shiny應用程序,該應用程序允許用戶基於相同的輸入數據並調用plotit函數, plotit在x閾值和y閾值之間滑動。 當它們滑動垂直,水平紅線時,標簽點將相應地更改。

我的Shiny應用程序文件為:

服務器

library(shiny)

# Define server logic for slider examples
function(input, output) {

  # Reactive expression to compose a data frame containing all of
  # the values
  sliderValues <- reactive({

    # Compose data frame
    data.frame(
      Name = c("X-threshold", 
               "Y-threshold"
               ),
      Value = as.character(c(input$integer, 
                             input$integer
                             )), 
      stringsAsFactors=FALSE)
  }) 

  # Show the values using an HTML table
  output$values <- renderTable({
    sliderValues()
  })
}

用戶界面

library(shiny)

# Define UI for slider demo application
fluidPage(

  #  Application title
  titlePanel("Sliders"),

  # Sidebar with sliders that demonstrate various available
  # options
  sidebarLayout(
    sidebarPanel(
      # Simple integer interval
      sliderInput("integer", "X-threshold",
                  min=3, max=10, value=1),

      # Simple integer interval
      sliderInput("integer", "Y-threshold",
                  min=10, max=35, value=1)

    ),

    # Show a table summarizing the values entered
    mainPanel(
      tableOutput("values")
      # How can I output the plot from coolplot.R here????
    )
  )
)

我的問題是如何從plot.R制作ui.R導入函數並顯示圖?

目前,在RStudio中,Shiny看起來像這樣(減去我的評論)。

在此處輸入圖片說明

是你想要的嗎? 請確保您的滑塊具有唯一的名稱

library(ggplot2)
library(tidyverse)
library(ggrepel)
library(shiny)
#-------------------------
# Function 
#-------------------------
plotit <- function (dat, x_thres, y_thres) {
  dat["Significant"] <- ifelse((dat$wt > x_thres | 
                                  dat$mpg > y_thres ), 'NotSignif','Signif')
  p <- ggplot(dat, aes(wt, mpg)) + 
    geom_point(alpha=0.8,size=2.75, aes(color=Significant)) +
    scale_color_manual(values=c("#B94024","#7D8D87")) +
    geom_vline(xintercept= x_thres, colour = '#B94024') + 
    geom_hline(yintercept=y_thres, colour = '#B94024') +
    geom_text_repel(data=subset(dat, wt > x_thres | mpg > y_thres),
                    aes(wt,mpg,label=model),
                    box.padding = unit(0.35, "lines"),
                    point.padding = unit(0.3, "lines") 
    )  + 
    theme(legend.position="none")  

  return(p)

}

#-------------------------
# Begin main code
#-------------------------
# I literally want to use file as input not 
# default mtcars variable
infile <- "https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv"
dat <- read_delim(infile,delim=",", col_types = cols())  
y_thres <- 25
x_thres <- 3
plotit(dat, x_thres, y_thres)



ui <- shinyUI(
  fluidPage(

    #  Application title
    titlePanel("Sliders"),

    # Sidebar with sliders that demonstrate various available
    # options
    sidebarLayout(
      sidebarPanel(
        # Simple integer interval
        sliderInput("integer", "X-threshold",
                    min=3, max=10, value=1),

        # Simple integer interval
        sliderInput("integer2", "Y-threshold",
                    min=10, max=35, value=1)

      ),

      # Show a table summarizing the values entered
      mainPanel(
        tableOutput("values"),
        plotOutput("myplot")
        # How can I output the plot from coolplot.R here????
      )
    )
  )
)

server <- shinyServer(function(input, output, session) {
  # Reactive expression to compose a data frame containing all of
  # the values

  sliderValues <- reactive({

    # Compose data frame
    data.frame(Name = c("X-threshold", "Y-threshold"),
      Value = as.character(c(input$integer, input$integer2)), 
      stringsAsFactors=FALSE)
  }) 

  # Show the values using an HTML table
  output$values <- renderTable({
    sliderValues()
  })

  output$myplot <- renderPlot({

    plotit(dat, input$integer, input$integer2)
  })

})

shinyApp(ui = ui, server = server)

在此處輸入圖片說明

編輯:從文件加載繪圖功能

酷圖

library(ggplot2)
library(tidyverse)
library(ggrepel)

#-------------------------
# Function 
#-------------------------
#-------------------------
# Begin main code
#-------------------------
# I literally want to use file as input not 
# default mtcars variable
infile <- "https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv"
dat <- read_delim(infile,delim=",", col_types = cols())  

plotit <- function (dat, x_thres, y_thres) {
  dat["Significant"] <- ifelse((dat$wt > x_thres | 
                                  dat$mpg > y_thres ), 'NotSignif','Signif')
  p <- ggplot(dat, aes(wt, mpg)) + 
    geom_point(alpha=0.8,size=2.75, aes(color=Significant)) +
    scale_color_manual(values=c("#B94024","#7D8D87")) +
    geom_vline(xintercept= x_thres, colour = '#B94024') + 
    geom_hline(yintercept=y_thres, colour = '#B94024') +
    geom_text_repel(data=subset(dat, wt > x_thres | mpg > y_thres),
                    aes(wt,mpg,label=model),
                    box.padding = unit(0.35, "lines"),
                    point.padding = unit(0.3, "lines") 
    )  + 
    theme(legend.position="none")  

  return(p)
}

閃亮的部分

   library(shiny)
    source("coolplot.R",local = TRUE)$value

    ui <- shinyUI(
      fluidPage(

        #  Application title
        titlePanel("Sliders"),

        # Sidebar with sliders that demonstrate various available
        # options
        sidebarLayout(
          sidebarPanel(
            # Simple integer interval
            sliderInput("integer", "X-threshold",
                        min=3, max=10, value=1),

            # Simple integer interval
            sliderInput("integer2", "Y-threshold",
                        min=10, max=35, value=1)

          ),

          # Show a table summarizing the values entered
          mainPanel(
            tableOutput("values"),
            plotOutput("myplot")
            # How can I output the plot from coolplot.R here????
          )
        )
      )
    )

    server <- shinyServer(function(input, output, session) {
      # Reactive expression to compose a data frame containing all of
      # the values

      # Add the source file of the plot if necessary

      sliderValues <- reactive({

        # Compose data frame
        data.frame(Name = c("X-threshold", "Y-threshold"),
          Value = as.character(c(input$integer, input$integer2)), 
          stringsAsFactors=FALSE)
      }) 

      # Show the values using an HTML table
      output$values <- renderTable({
        sliderValues()
      })

      output$myplot <- renderPlot({

        plotit(dat, input$integer, input$integer2)
      })

    })

    shinyApp(ui = ui, server = server)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM