繁体   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