首页 > 解决方案 > 代码不会对 R 中的闪亮变化进行动画处理

问题描述

我试图学习闪亮的结构来发展我的情节。经过几个小时的工作,我得到了感兴趣的情节,但是当我改变值时,我看不到情节有任何变化。

先感谢您!

以下是代码:

library(shiny)

df <- data_frame(x = c("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O"
),
y = c(45,55,66,55,45,45,50,50,50,64,44,60,50,50,45))

ui <- fluidPage(
  
  titlePanel("letters"),
  
  sidebarLayout(
    sidebarPanel(
      tags$b("Data:"),
      textInput("x", "x", value = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O", placeholder = "Enter letter separated by a comma, A,B,C"),
      textInput("y", "y", value = "45,55,66,55,45,45,50,50,50,64,44,60,50,50,45", placeholder = "Enter values separated by a comma with decimals as points, e.g. 50, 55.5, 5, 5.03, etc."),
      
    ),
    
    mainPanel(
      plotOutput("distPlot")
    )
  ))

server<-shinyServer(function(input, output) {
  output$distPlot <- renderPlot({
    df2 <- ave(df$y, df$y, FUN = seq_along)
    plot(df2 ~ df$y, df, pch = df$x, ylim = c(1, 20), ylab = "", axes = FALSE)
    axis(1)
    mu <- mean(df$y)
    abline(v = mu, col = "red")
    text(mu, 20, paste("Avg time:", mu), adj = -0.1, col = "red")
  }) 
})



shinyApp(ui = ui, server = server)

标签: rshiny

解决方案


如果您想更改数据框传递input$xinput$y在服务器内部创建一个数据框以使其对更改做出反应。例如:

library(shiny)
library(stringr)
library(dplyr)


ui <- fluidPage(
  
  titlePanel("letters"),
  
  sidebarLayout(
    sidebarPanel(
      tags$b("Data:"),
      textInput("x", "x", value = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O", placeholder = "Enter letter separated by a comma, A,B,C"),
      textInput("y", "y", value = "45,55,66,55,45,45,50,50,50,64,44,60,50,50,45", placeholder = "Enter values separated by a comma with decimals as points, e.g. 50, 55.5, 5, 5.03, etc."),
      
    ),
    
    mainPanel(
      plotOutput("distPlot"),
      "This is a brief description text"  # can add other elements
    )
  ))

server<-shinyServer(function(input, output) {
  output$distPlot <- renderPlot({
    
    df <- data_frame(x = input$x %>% str_split(",", simplify = TRUE) %>% str_trim(),
                     y = input$y %>% str_split(",", simplify = TRUE) %>% str_trim() %>% as.numeric())
    
    df2 <- ave(df$y, df$y, FUN = seq_along)
    plot(df2 ~ df$y, df, pch = df$x, ylim = c(1, 20), ylab = "", axes = FALSE)
    axis(1)
    mu <- mean(df$y)
    abline(v = mu, col = "red")
    text(mu, 20, paste("Avg time:", mu), adj = -0.1, col = "red")
  }) 
})



shinyApp(ui = ui, server = server)

(这里使用了两个额外的包:dplyrstringr


推荐阅读