R 如何在闪亮应用程序的ggplot2散点图中添加和删除geom_smooth()趋势线

R 如何在闪亮应用程序的ggplot2散点图中添加和删除geom_smooth()趋势线,r,ggplot2,shiny,R,Ggplot2,Shiny,您好,我有一个简单的闪亮应用程序,它创建了一个作为输入的mtcars'变量散点图。我想要实现的是让用户可以选择显示和隐藏使用geom_smooth()创建的趋势线。我尝试了if语句,如下所示,但没有结果。有什么建议吗 #ui.r library(shiny) library(ggplot2) library(plotly) library(dplyr) fluidPage( # App title ---- titlePanel(div("CROSS CORRELATION",sty

您好,我有一个简单的闪亮应用程序,它创建了一个作为输入的
mtcars
'变量散点图。我想要实现的是让用户可以选择显示和隐藏使用
geom_smooth()
创建的趋势线。我尝试了
if
语句,如下所示,但没有结果。有什么建议吗

#ui.r
library(shiny)
library(ggplot2)
library(plotly)
library(dplyr)

fluidPage(

  # App title ----
  titlePanel(div("CROSS CORRELATION",style = "color:blue")),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(


    ),
    # Main panel for displaying outputs ----
    mainPanel(

      tabsetPanel(type = "tabs",

                  tabPanel("Correlation Plot",

                           fluidRow(
                             column(3, uiOutput("lx1")),
                           column(3,uiOutput("lx2"))),
                           hr(),
                           fluidRow(
                             tags$style(type="text/css",
                                        ".shiny-output-error { visibility: hidden; }",
                                        ".shiny-output-error:before { visibility: hidden; }"
                             ),
                           column(3,uiOutput("td"))
                           ),
                           fluidRow(
                           plotlyOutput("sc"))
      )

      )
  )))
#server.r
function(input, output) {




  output$lx1<-renderUI({
    selectInput("lx1", label = h4("Select 1st Expression Profile"), 
                choices = colnames(mtcars[,2:5]), 
                selected = "Lex1")
  })
  output$lx2<-renderUI({
    selectInput("lx2", label = h4("Select 2nd Expression Profile"), 
                choices = colnames(mtcars[,2:5]), 
                selected = "Lex2")
  })


  output$td<-renderUI({
    radioButtons("td", label = h4("Trendline"),
                 choices = list("Add Trendline" = "lm", "Remove Trendline" = ""), 
                 selected = "")
  })





  # 1. create reactive values
  vals <- reactiveValues()
  # 2. create df to store clicks
  vals$click_all <- data.frame(x = numeric(),
                               y = numeric(),
                               label = character())
  # 3. add points upon plot click
  observe({
    # get clicked point
    click_data <- event_data("plotly_click", source = "select")
    # get data for current point
    label_data <- data.frame(x = click_data[["x"]],
                             y = click_data[["y"]],
                             label = click_data[["key"]],
                             stringsAsFactors = FALSE)
    # add current point to df of all clicks
    vals$click_all <- merge(vals$click_all,
                            label_data, 
                            all = TRUE)
  }) 

 output$sc<-renderPlotly({

   mtcars$car <- row.names(mtcars)
   p1 <- ggplot(mtcars, aes_string(x = input$lx1, y = input$lx2,key="car",group='car'))+
     # Change the point options in geom_point
     geom_point(color = "darkblue") +

     # Change the title of the plot (can change axis titles
     # in this option as well and add subtitle)
     labs(title = "Cross Correlation") 
     # Change where the tick marks are

     # Change how the text looks for each element



     if(input$td=="lm"){

       geom_smooth(aes(group = 1))+
         # 4. add labels for clicked points
         geom_text(data = vals$click_all,
                   aes(x = x, y = y, label = label),
                   inherit.aes = FALSE, nudge_x = 0.25)
     }
    else{
     # 4. add labels for clicked points
     geom_text(data = vals$click_all,
               aes(x = x, y = y, label = label),
               inherit.aes = FALSE, nudge_x = 0.25)
   }




   ggplotly(p1,source = "select", tooltip = c("key")) 

 }) 




}
#ui.r
图书馆(闪亮)
图书馆(GG2)
图书馆(绘本)
图书馆(dplyr)
流动摄影(
#应用程序标题----
标题板(div(“互相关”,style=“color:blue”),
#带有输入和输出定义的侧栏布局----
侧边栏布局(
#输入侧栏面板----
侧栏面板(
),
#用于显示输出的主面板----
主面板(
tabsetPanel(type=“tabs”,
tabPanel(“相关图”,
fluidRow(
列(3,uiOutput(“lx1”),
列(3,uiOutput(“lx2”),
hr(),
fluidRow(
标记$style(type=“text/css”,
“.Shining输出错误{可见性:隐藏;}”,
“.Shining输出错误:在{可见性:隐藏;}之前”
),
列(3,uiOutput(“td”))
),
fluidRow(
打印输出(“sc”))
)
)
)))
#服务器.r
功能(输入、输出){

根据上述评论,输出$lx1:

if(input$td=="lm"){

       p1 <- p1+geom_smooth(aes(group = 1))+
         # 4. add labels for clicked points
         geom_text(data = vals$click_all,
                   aes(x = x, y = y, label = label),
                   inherit.aes = FALSE, nudge_x = 0.25)
     }
   else{
     # 4. add labels for clicked points
     p1 <- p1+geom_text(data = vals$click_all,
               aes(x = x, y = y, label = label),
               inherit.aes = FALSE, nudge_x = 0.25)
   } 
if(输入$td==“lm”){

p1基于上述评论:

if(input$td=="lm"){

       p1 <- p1+geom_smooth(aes(group = 1))+
         # 4. add labels for clicked points
         geom_text(data = vals$click_all,
                   aes(x = x, y = y, label = label),
                   inherit.aes = FALSE, nudge_x = 0.25)
     }
   else{
     # 4. add labels for clicked points
     p1 <- p1+geom_text(data = vals$click_all,
               aes(x = x, y = y, label = label),
               inherit.aes = FALSE, nudge_x = 0.25)
   } 
if(输入$td==“lm”){

p1你需要
p1谢谢就是这样!你需要
p1谢谢就是这样!